The classes dealing with date and those dealing with time have many similarities. These classes descend from more abstract classes implementing the notion of value (TIME_VALUE , DATE_VALUE , DATE_TIME_VALUE ). From this notion come two kinds of heirs which are the absolute notion of time (classes DATE , TIME and DATE_TIME ) and the notion of duration (classes DATE_DURATION , TIME_DURATION , DATE_TIME_DURATION ).
DATE , TIME and DATE_TIME inherit from the deferred class ABSOLUTE. These classes model absolute temporal values, i.e., specific times and dates. Because ABSOLUTE inherits from COMPARABLE, the ordering functions
>= are available on instances of ABSOLUTE and its descendants.
TIME models times of day, supporting queries: hour, minute, and second. It is possible to use more precision for time. However, this section deals with precision only to the second. See More precision for additional information on higher precision.
There are three ways to create an instance of the class TIME: by choosing the time (make), by getting the time from the system (make_now), or by choosing the number of seconds elapsed from the origin (make_by_seconds). The arguments of make and make_by_seconds have to respect the range of a day (see preconditions).
Origin and cyclic representation
The origin for instances of TIME is 0 hour 0 minute and 0 second. The notion of time is relative to a day, and has a cyclic representation. So, days begin at 0:0:0 and end at 23:59:59. If a second is added to 23:59:59 then the result will be 0:0:0. Subtracting a minute from 0:0:0 will yield 23:59:0.
Instances of TIME may be compared. Functions
>= are available for ordering instances. Function is_equal (or ~) can be used to test object equality, whereas = will compare references.
The query duration applied to an instance of TIME returns an instance of TIME_DURATION. It is the duration from the origin until the current time.
The query seconds returns the total number of seconds since the origin. This query may be useful to get the number of seconds between two events.
The feature - returns an Interval between two instances of TIME. The duration of this interval is given by the function duration. However, this duration is non-canonical (See Duration for a definition of canonical form). In TIME, the feature relative_duration returns the same duration, but more efficiently and in canonical form.
- Set values for hour, minute, and second with set_hour, set_minute, and set_second. Arguments must satisfy the rules of creation.
- Add hours, minutes, and seconds with features hour_add, minute_add, and second_add. Features add and + take an instance of TIME_DURATION as an argument and add it to the current time.
- Adjust an instance of TIME to the next or the previous hour, minute, or second with features hour_forth, hour_back, minute_forth, minute_back, second_forth, and second_back. It is more efficient to use these features rather than the addition commands listed above (e.g., hour_back will outperform hour_add (-1) ).
DATE models dates, and supports queries year, month and day. Working with dates is more complicated than working with times of day because of the irregularities in elements of dates. Months, for example, have varying numbers of days. The number of days in a year varies between non-leap years and leap years. The only limit to magnitude for dates comes from INTEGER representation. If, as in most cases, INTEGER size is 32 bits, the range for a date is -2^31 to 2^31 days, or about 5.8 million years from the origin. So, unless you're trying to determine if the "big bang" occurred on a Tuesday, this should probably be adequate.
There are three ways to create an instance of the class DATE: by choosing the date (make, make_month_day_year, make_day_month_year), by getting the date from the system (make_now), or by choosing the number of days elapsed from the origin (make_by_days). The arguments of each creation procedure, when considered together, must represent a valid date. For example, a month of February and day of 29 will be invalid if the value for the year is not a leap year.
The origin for instances of DATE is January 1, 1600.
Instances of DATE may be compared. Functions
>= are available for ordering instances by value. Function is_equal (or ~) can be used to test object equality, while = will compare references.
Each instance of DATE has a function (duration) which returns the duration since the origin until the current date (it is an instance of DATE_DURATION). This duration is definite, i.e. it contains only days (See Duration). However, it may be useful to deal directly with days (no need of DATE_DURATION). In this case, the function days of DATE yields the number of days since origin.
You can obtain information about instances from status reporting queries. Some examples are:
- leap_year is True if the instance is a leap year.
- year_day returns the number of days from the beginning of the year to this instance. So, for example, for the date January 31, year_day would return 31. For February 1, year_day would return 32.
- day_of_the_week returns the number of days the instance is from the beginning of the week. Values range from 1 (Sunday) through 7 (Saturday).
DATE operations look much like those of TIME:
- Set year, month, and day with set_year, set_month, and set_day. Arguments must satisfy the rules of creation. These rules are more complicated than those of TIME. For example you cannot set day to 31 if the current month is April, whereas you can if the month is January. These restrictions also apply to make. Similarly for years: you cannot set year to a non-leap year if the current date is February 29th. However, two features are available to set month and year even if day is too large: set_month_cut_days and set_year_cut_days will cut day down to the largest value allowed.
- Add years, months and days with features year_add, month_add, and day_add. There is no restriction on adding a year or a month. However, these features have to return a correct result, i.e., the day is checked before each addition. Adding one month to August 31st will yield September 30th. 31 is cut to 30 since there are only 30 days in September. Features add and + take an instance of DATE_DURATION as an argument and add it to the instance of date. It is written so that years and months are added first, the days last.(see DATE_DURATION below)
- Move to the next or the previous year, month or day with features year_forth, year_back, month_forth, month_back, day_forth, and day_back. It more efficient to use these features than the addition commands (e.g., year_back performs better than year_add (-1) ).
- Features relative_duration and definite_duration return the duration between the current date and the argument. relative_duration returns a result which is canonical (See Duration), while definite_duration returns a definite result which may be not canonical.For example, suppose date1 is April 20th and date2 is May 28th. Both features will yield instances of DURATION; however, relative_duration will yield 1 month and 8 days whereas definite_duration will yield 38 days.
DATE_TIME provides a combined date and time. DATE_TIME is client of both TIME and DATE. Some features from DATE and TIME are offered directly as features of DATE_TIME. Other features of DATE and TIME may be called indirectly with the correct DATE_TIME attribute (time or date).
There are several ways to create an instance:
- Choose values for each of the attributes of the date and the time (make).
- Get the current date and time from the system (make_now).
- Associate an instance of DATE with an instance of TIME (make_by_date_time).
Caution: The creation procedure make_by_date_time copies only the references of its arguments, so that if the time (or the date) is changed, the instance previously initialized will be also changed. If this effect has to be avoided, using twin's of the arguments is required.
- Encapsulate an instance of DATE (make_by_date). The attribute time is set to the origin, i.e. 0:0:0. The attribute date is set with the same reference as the argument (See caution just mentioned above).
The origin for instances of DATE_TIME is 0:0:0 on January 1, 1600.
An instance of DATE_TIME has attributes which are instances of classes TIME and DATE, respectively. As a convenience, some features of TIME and DATE have been made available directly as features of DATE_TIME (and passed through to time and date). These include days, seconds, duration, date_duration, and time_duration.
Instances of DATE_TIME can be compared. Functions
>= are available for ordering instance by value. Function is_equal (or ~) is used to test object equality, while = compares references.
duration returns an instance of DATE_TIME_DURATION which represents the duration of time between the instance and the origin.
It is possible to change reference of time and date with the features set_time and set_date. To change only one element (for example hour), features from TIME or DATE have to be used.
Add hours, minutes, and seconds with features hour_add, minute_add, and second_add.
Caution: Using the addition features from TIME on the time attribute is also possible but the date will not be modified in the case time makes a cycle. So, for example use: my_date_time.hour_add (more_hours) instead of: my_date_time.time.hour_add (more_hours)
day_add is available on instances of DATE_TIME to add days.
Feature add (or +) takes an instance of DATE_TIME_DURATION as an argument. Internally, add first adds the the date duration, and then the time duration.
Feature relative_duration and definite_duration return the duration between the current date (with time) and the argument. relative_duration returns a result which is canonical (see Duration), while definite_duration returns a result which is definite but may be not canonical.
Obtaining a DATE from a DATE_TIME and vice versa
Obtaining an instance of DATE which represents the date portion of an instance of DATE_TIME can be done by applying the query date to the instance of DATE_TIME.
You can ask for a new instance of DATE_TIME from an instance of DATE by using the query to_date_time. The new instance will have the same date as the target, and have its time set to the origin (0:0:0). A DATE_TIME instance can be initialized with a specific DATE by using DATE_TIME's creation procedure make_by_date.