This class allows the programmer to manipulate date and time values. Individual time fields can be requested or modified, returning `sanitized' times (e.g., a date like march 33 or a time like 56 hours will never be returned). Times may be specified in local time or in Universal Time Coordinated (UTC) values. It is also possible to add or subtract seconds or struct tm structures to or from DateTime objects. This operation keeps the current time zone (UTC, local or another time zone). Conversions between time zones and UTC are also supported.
The class DateTime supports various ways to initialize objects. Time may be specified in UTC, as local time or using any other offset from UTC. When an explicit time offset is requested it is specified as an int value representing the time offset in minutes, with time zones time zones West of Greenwich using negative time offsets and East of Greenwich using positive time offsets. Time zone offsets are truncated to multiples of 30 minutes and are always computed modulo 12 * 60, as no time zone has a shift exceeding the (absolute) shift of 12 * 60. Daylight saving times are in effect in many time zones. Except for the local time zone DateTime may not be able to show the correct daylight saving time correction.
There are various ways to construct DateTime objects: time in seconds since the beginning of the `era' (midnight Jan 1, 1970 UTC), a struct tm, or a textual time representations may be used. These values may themselves be corrected using display zone shifts. A display zone shift determines the difference between the UTC time and the local time zone to be used when displaying time or returning time fields. Sometimes a UTC zone shift may be provided correcting a provided local time to UTC.
If a display zone shift is explicitly specified no additional daylight saving time (DST) zone shift is added to the display time. If the actual local time is requested (specified by the TimeType value LOCALTIME) a DST correction is automatically applied when appropriate.
Members of the class DateTime should only be used if operator bool() returns true. The member error() can also be used if operator bool() returns false.
Handling time is complex. The C function time(2) returns the time in seconds. This time is normally represented in UTC. The function gmtime(3) when provided with time()'s output returns the broken down time in a struct tm. Remarkably (and confusingly), when this struct tm is then passed to the mktime(3) function the latter function does not return the UTC-time in seconds, but a time that differs from the time in UTC by the current local time shift. E.g., the program
#include <ctime> #include <iostream> using namespace std; int main() { time_t utc = time(0); struct tm *ts; time_t local = mktime(ts = gmtime(&utc)); cout << ts->tm_hour << ' ' << utc - local << endl; return 0; }displays the current UTC clock's hour setting, but reports the difference in seconds between the local time and the UTC time (e.g., the difference between CET and UTC is one hour, and the program displays 3600).
To obtain the time in UTC-seconds from mktime(3) the function localtime(3) must be used to obtain the struct tm values:
#include <ctime> #include <iostream> using namespace std; int main() { time_t utc = time(0); struct tm *ts; time_t local = mktime(ts = localtime(&utc)); cout << ts->tm_hour << ' ' << utc - local << endl; return 0; }The above program displays the local clock's hour value, but a difference of 0 for the recomputed time in seconds.
The class DateTime assumes that the time() function returns the UTC time in seconds, which is the way computers should have configured their hardware clock.
DateTime::Month
This enumeration has the following values which are ordered using the
default C++ enum values:
DateTime::Relative
This enumeration is used with the setMonth() member (see below).
It has the following values:
DateTime::TimeFields
This enumeration has the following values which can be bit_or-ed
when calling the member setFields():
DateTime::TimeType
This enumeration has the following values:
DateTime::TriVal
This enumeration has the following values, returned by the dst()
member (see below):
DateTime::Weekday
This enumeration has the following values which are ordered using the
default C++ enum values:
DateTime objects may be initialized using textual time-representations. Also, the time represented by a DateTime object may be altered using text which can be extracted from a stream using the extraction operator.
Time specifications may be formatted as follows:
The time zone time shift specifications (+0100, +01:00) are required as they are part of the rfc specifications but are ignored for the actual local time construction as the DateTime object determines the time zone specification from the computer's current time zone setting.
The following constructors ignore the DST, day of the year, and day of the week fields of the struct tm passed to the constructors:
struct tm { int tm_sec; // seconds 0..59, or 60: leap second int tm_min; // minutes 0..59 int tm_hour; // hours 0..23 int tm_mday; // day of the month 1..31 int tm_mon; // month 0..11 int tm_year; // year since 1900 int tm_wday; // day of the week 0..6 int tm_yday; // day in the year 0..365 int tm_isdst; // daylight saving time // > 0: yes, 0: no, < 0: unknown };Values outside of these ranges may sometimes be used (with various set..() members, see below) to compute a point in time in the future or in the past. E.g., by specifying 30 for the hour-setting DateTime objects a point in time in the next day will be used.
The final constructors convert textual time specifications formatted as described in section STANDARD TEXTUAL TIME REPRESENTATIONS (the day of the week specification is ignored by the time conversion).
The copy constructor is available.
All class-less overloaded operators are defined in the FBB namespace, except for the overloaded insertion operator, which is defined in the std namespace.
The istream from which the time is extracted must contain time formatted as described in section STANDARD TEXTUAL TIME REPRESENTATIONS. As documented in that section, time shift and time zone specifications (+0100, +01:00, CET) are ignored and may be omitted. They are ignored when specified. The object will merely interpret the date/time specification as a specification in the object's currently active time zone.
If the time could not be determined from a textual string representing the time (cf. section CONSTRUCTORS) then errno() returns 0, operator bool() returns false, and the time stored in the object remains unchanged.
The following overloaded operators modify the time as stored in UTC seconds within objects. Note that the time as displayed by the object will be corrected for any display zone shift that may have been defined for those objects.
time_t seconds = time(0); tm timeStruct = *gmtime(&seconds); DateTime tmp(timeStruct); cout << tmp << endl; --timeStruct.tm_mday; // days start at 1: subtract 1 less than // the current day number to get '01' timeStruct.tm_year -= (1970 - 1900); // era starts at 1970, tm_year // is relative to 1900. tmp -= timeStruct; cout << tmp << endl;
The following overloaded operators can be used to compare the UTC time as represented by DateTime objects. Note that these comparisons are independent of any display zone shift that may have been defined for the objects.
Additional overloaded operators:
All members returning a time-element do so according to the latest time-representation (i.e., UTC, LOCALTIME, or using an explicitly set display zone shift value). All members returning numerical values use 0 as their smallest return value, except for the ...Nr() members, which start at 1.
Mon, 17 Dec 2007 13:49:10 +0100
2008-11-02 13:29:11+01:00
An extensive example illustrating the use of all of DateTime's members is provided in the file bobcat/datetime/driver/driver.cc found in the source archive.