FBB::DateTime(3bobcat)

Date and Time
(libbobcat-dev_4.08.03-x.tar.gz)

2005-2018

NAME

FBB::DateTime - Performs Date and Time Computations

SYNOPSIS

#include <bobcat/datetime>
Linking option: -lbobcat

DESCRIPTION

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.

NAMESPACE

FBB
All constructors, members, operators and manipulators, mentioned in this man-page, are defined in the namespace FBB.

INHERITS FROM

-

ENUMS defined in DateTime

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:

STANDARD TEXTUAL TIME REPRESENTATIONS

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.

CONSTRUCTORS

The following constructors ignore the DST, day of the year, and day of the week fields of the struct tm passed to the constructors:

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.

OVERLOADED OPERATORS

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 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.

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:

MEMBER FUNCTIONS

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.

Whenever a set...() member is used in such a way that the resulting date would be invalid the original DateTime object's value is unaltered.

EXAMPLE

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.

FILES

bobcat/datetime defines the class interface.

SEE ALSO

bobcat(7), Exception(3bobcat), asctime_r(3), gmtime_r(3), localtime_r(3), time(2), mktime(3),

BUGS

The class DateTime assumes that time(2) returns the time in UTC.
English is used / expected when specifying named date components.

DISTRIBUTION FILES

BOBCAT

Bobcat is an acronym of `Brokken's Own Base Classes And Templates'.

COPYRIGHT

This is free software, distributed under the terms of the GNU General Public License (GPL).

AUTHOR

Frank B. Brokken (f.b.brokken@rug.nl).