A clock is an object which knows about a particular point on the time axis known as now. All times before that point are considered in the past and times beyond it are in the future.
DT_CLOCK is a deferred class offering features such as time_now, date_now and date_time_now which can provide today's date and the current time according to the clock's criterion. Three other features, set_time_to_now, set_date_to_now and set_date_time_to_now, allow times, dates and date/times to be reset to the clock's current time.
Typically programs need to access the current time of the computer they are running on. That's what class DT_SYSTEM_CLOCK is for. This class is an heir of class DT_CLOCK and implements its deferred features so that the current local time of the system is returned. The typical way to invoke the system clock is to inherit from DT_SHARED_SYSTEM_CLOCK and call the once function system_clock. Alternatively, class DT_UTC_SYSTEM_CLOCK provides the current UTC (Coordinated Universal Time) time of the system. It is available through the once function utc_system_clock in class DT_SHARED_SYSTEM_CLOCK.
DT_SYSTEM_CLOCK and DT_UTC_SYSTEM_CLOCK are implemented using the time facilities provided by each supported Eiffel compiler.
There is an example using system clock in $GOBO/library/time/example/clock.
With the design of the clock classes, it is quite easy to write user-customized clocks by inheriting from DT_CLOCK and giving an implementation to the deferred features set_time_to_now, set_date_to_now and set_date_time_to_now. An example of a possible customizable clock is a clock which could run twice faster than the regular system clock for instance. This kind of clock can be useful in order to run simulation tests driven by time events. If the clock runs much faster than the system clock then tests which may normally last several hours or days will finish in a reasonable amount of time. Another application of customizable clocks is a clock which runs at the same speed as the system clock, but returns dates and times in the past or in the future. This could have been used for example to check that a program was Y2K compliant just by readjusting the clock to just before midnight on 31 December 1999. There are certainly many other examples where customizable clocks can be useful.
Copyright © 2001-2019, Eric
Last Updated: 15 March 2019