FBB::DateTime(3bobcat)

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

2005-2019

NAME

FBB::DateTime - Performs Date and Time Computations

SYNOPSIS

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

DESCRIPTION

FBB::DateTime is used 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; instead the next month or day is returned). Times may be specified in local time, in Universal Time Coordinated (UTC) values, or as a time in a time zone differing a fixed number of (half) hours from UTC, which time zones may or may not support Daylight Saving Time (DST) date-intervals.

Dates/times represented by DateTime objects may be modified by adding, subtracting, or setting seconds (and by implication: minutes, hours, and days), (fields of) struct tm values, or std::chrono::duration values.

These operations are always performed relative to the DateTime object's current timezone (which may be UTC, local or another timezone). Conversions between time zones (including the UTC `time zone') are supported.

DateTime offers quite a few ways for initializing objects. Times may be specified as UTC time, as local time, or other offsets from UTC may be specified. Explicit time offsets are specified as int values (representing time offsets in minutes). Negative time offsets specify timezones West of Greenwich, and positive offsets specify time zones East of Greenwich. Timezone offsets are truncated to multiples of 30 minutes and are always computed modulo 12 * 60, as timezones may at most differ 12 hours from UTC.

When constructing DateTime objects time in seconds since the beginning of the `epoch' (midnight Jan 1, 1970 UTC); std::tm structs; std::chrono::time_points; or several textual time representations may be used to define the object's date/time. Except for UTC-times DST time shifts for any specified date-interval may be defined. When DST is active, its time shift is automatically applied to the represented time. E.g., when a DateTime object's time equals 12:00 hr., and DST becomes active tomorrow using a standard +1 hour shift, then the object's time will show 13:00 hr. after adding 24 hours to its time.

Handling time is complex. The C function time(2) returns the time in seconds since the beginning of the epoch. The function gmtime(3), when provided with time's output returns the broken down time in a struct tm. When this struct tm is thereupon 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;
    }
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 retrieve 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;
    }
The above program displays the local clock's hour value, while the time value returned by mktime equals the value returned by the function time.

The class DateTime assumes that the function time returns the UTC time in seconds since the beginning of the epoch.

NAMESPACE

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

INHERITS FROM

-

ENUMS defined by the class DateTime

DateTime::Month
This enumeration has the following values which are ordered using the default C++ enum values:

The standard 3-letter abbreviations can also be used (e.g., DateTime::Jul):

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:

The standard 3-letter abbreviations can also be used (e.g., DateTime::Wed):

STANDARD TEXTUAL TIME REPRESENTATIONS

DateTime objects may also be defined from textual time-representations. In addition, the date/time represented by a DateTime object may be altered using textual time representations extracted from istreams.

The following time formats are recognized:

When using standard textual time representations and requesting DateTime::LOCALTIME timezone shift specifications (+0100, +01:00) are used to determine UTC time, followed by a correction for the local time using the computer's current timezone setting. E.g., if the computer's local time zone equals +1 hour, then specifying 2018-12-03 13:29:11+04:00 and requesting LOCALTIME results in 13 - 4 + 1 = 10 hr local time.

TIME ZONES AND DAYLIGHT SAVING TIMES

Time zones and Daylight Saving Time (DST) may be configured in many ways. Here is an overview:

For specific time zone shifts DST may be configured by passing DateTime::DSTSpec objects to DateTime constructors. DSTSpec objects can be defined using the following constructors:

Time zone names may be encountered in standard textual time representations. The characteristics of time zone names must have been defined before they can be used. Time zone names and their characteristics may defined on file which is read by the static member function DateTime::readZones, or the static member DateTime::addZone can be used to add individual time zone specifications to the set of available named time zones (see section STATIC MEMBERS below for their descriptions).

CONSTRUCTORS

If a DateTime construction fails an FBB::Exception is thrown.

The following constructors expect const-references to struct tm data. The DST, day of the year, and day of the week fields of struct tm data are ignored by the constructors. The struct tm is defined as:


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 be used to compute points in time in the future or in the past. E.g., specifying 30 hours results in 06 hours the next day.

The following constructors convert textual time representations (cf. section STANDARD TEXTUAL TIME REPRESENTATIONS). These constructors do not interpret weekday names that may be specified by textual time representations.

Names of time zones used by time specifications (cf. the second standard textual time representation) must have been defined by the static members readZones or addZone (cf. section STATIC MEMBERS) or an exception is thrown.

DateTime objects may alse be initialized using information extracted from istreams. The information extracted from those streams must be formatted as standard textual time representations. In addition to the constructors expecting lvalue references to std::istream objects constructors expecting rvalue references to std::istream objects are also available.

Copy and move constructors are available.

OVERLOADED OPERATORS

All class-less overloaded operators are defined in the FBB namespace. All overloaded operators modifying DateTime objects support `commit or roll-back': if the operation cannot be performed an exception is thrown, without modifying the destination object.

Copy and move assignment operators are available.

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 numeric values use 0 as their smallest return value, except for the ...Nr() members, which start at 1.

STATIC MEMBERS

OBSOLETED

The following members are kept for backward compatibility reasons, but do not inspect or modify their DateTime objects anymore. They are removed in a future Bobcat release.

EXAMPLES

Many examples illustrating the use of DateTime objects are provided in the source archive's bobcat/datetime/driver/demos directory. Here is an example:

#include <iostream>

#include "../../datetime"

using namespace std;
using namespace FBB;

int main()
{
    DateTime local{ DateTime::LOCALTIME };
    DateTime copy{ local };

    cout << "local time: " << local << ", DST = " << local.dst() << "\n\n";

    local += 5 * 30 * 24 * 3600;        // add +/- 5 months;
    cout << "+/- 5 months. later: " << local << ", DST = " << local.dst() <<
                                                                    "\n\n"; 

    local = copy;
    cout << "Back to the initial time time: " << local << "\n\n";

    local += chrono::hours(-2);      // subtract two hours;
    cout << "2 hrs. earlier: " << local << ", DST = " << local.dst() <<
                                                                    "\n\n"; 

    local = copy;
    cout << "Back to the initial time time: " << local << "\n\n";

    local += tm {
                    +10,    // sec
                    +10,    // min
                     -5,    // hr
                      0,    // mday
                      5,    // month
                      1,    // year
                };

    cout << "10 sec, 10 min, 5 months 1 year later, 5 hrs earlier:\n" << 
            local << ", DST = " << local.dst() << "\n\n";

}

FILES

bobcat/datetime defines the class interface.

SEE ALSO

bobcat(7), Exception(3bobcat), gmtime_r(3), localtime_r(3), time(2), mktime(3), https://www.timeanddate.com/time/dst/events.html

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