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:
- JANUARY,
- FEBRUARY,
- MARCH,
- APRIL,
- MAY,
- JUNE,
- JULY,
- AUGUST,
- SEPTEMBER,
- OCTOBER,
- NOVEMBER,
- DECEMBER.
The standard 3-letter abbreviations can also be used (e.g.,
DateTime::Jul):
- Jan,
- Feb,
- Mar,
- Apr,
- May ,
- Jun,
- Jul,
- Aug,
- Sep,
- Oct,
- Nov,
- Dec.
DateTime::Relative
This enumeration is used with the setMonth() member (see below).
It has the following values:
- THIS_WEEK,
- THIS_YEAR,
- LAST,
- NEXT
DateTime::TimeFields
This enumeration has the following values which can be bit_or-ed
when calling the member setFields():
- SECONDS
- MINUTES
- HOURS
- MONTHDAY
- MONTH
- YEAR
DateTime::TimeType
This enumeration has the following values:
- LOCALTIME: the time is broken down as the local time,
- UTC: the time is broken down as Universal Time Coordinated.
DateTime::TriVal
This enumeration has the following values, returned by the dst()
member (see below):
- UNKNOWN, returned when no information about the Daylight
Saving Time is available (starting with Bobcat 4.09.00 this value not
used anymore);
- NO, returned when Daylight Saving Time is not active;
- YES, returned when Daylight Saving Time is active.
DateTime::Weekday
This enumeration has the following values which are ordered using the
default C++ enum values:
- SUNDAY,
- MONDAY,
- TUESDAY,
- WEDNESDAY,
- THURSDAY,
- FRIDAY,
- SATURDAY.
The standard 3-letter abbreviations can also be used (e.g.,
DateTime::Wed):
- Sun,
- Mon,
- Tue,
- Wed,
- Thu,
- Fri,
- Sat.
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:
- Mon Dec 3 13:29:11 2018, as displayed by put_time(..., "%c");
- Mon Dec 3 13:29:11 CET 2018, as displayed by the date(1) program;
- Mon, 03 Dec 2018 13:29:11 +0100, as displayed by the date -R
command (and the rfc2822() member, see below);
- 2018-12-03 13:29:11+01:00, as displayed by the date
--rfc-3339=seconds command.
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:
- When requesting UTC DST is not used, and no zone shift is applied.
- When requesting LOCALTIME the computer's time configuration
defines how DST is used. It defines the DST begin- and end-dates and a
standard DST shift of +1 hour is used. Also, the computer's idea of
its local time zone is used.
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:
- DSTSpec(bool useDST, dstMinutes = 60):
When useDST is false no DST is used, and dstMinutes is
ignored. When useDST is true the computer's DST date-interval
is used. By default using the standard 1 hour DST time shift. Other
(positive or negative) DST shifst (in minutes) can be specified as
optional 2nd argument;
- DSTSpec(int dstMinutes):
The argument is used to specify (positive or negative) DST shifst (in
minutes), which is applied during the computer's DST date-interval;
- DSTSpec(std::string const &begin,
std::string const &end, int dstMinutes = 60):
Here the begin- and end-dates of the DST are specified. Begin and
end are std::string parameters to avoid numbers starting with
0 from being interpreted as octal values. They should be specified as
"mmdd" where mm represents the month number ("01"
representing January, "12" representing December) and dd the
day number within the specified month. The DST is applied throughout
the date-interval, so including the end date specification.
Within the DST date-interval the standard 1 hour DST time shift is
applied. Other (positive or negative) DST shifst (in minutes) can be
specified as optional 3nd argument.
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.
- DateTime(TimeType type = UTC):
The object's date/time is initialized to the current date and time.
The argument specifies how the time is stored and displayed by the
DateTime object. By default as UTC time (no zone-shift or dst
correction) or as local time (LOCALTIME), using
the computer's current timezone shift and DST correction;
- DateTime(int zoneMinutes):
The object's date/time is initialized to a local time at UTC +
zoneMinutes (in minutes). DST is not used;
- DateTime(int zoneMinutes, DSTSpec const &dstSpec):
The object's date/time is initialized as in the previous constructor.
Then the dstSpec object defines how DST is handled;
- DateTime(time_t time, TimeType type):
The object's date/time is initialized to time seconds
since the beginning of the epoch. When LOCALTIME is specified the
computer's current time zone and DST handling correction is applied as
well;
- DateTime(time_t time, int zoneMinutes):
The object's date/time is initialized to a local time of time
seconds since the beginning of the epoch to which a zone shift of
zoneMinutes is added. DST is not used;
- DateTime(time_t time, int zoneMinutes, DSTSpec const &dstSpec):
The object's date/time is initialized as in the previous constructor.
Then the dstSpec object defines how DST is handled;
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.
- DateTime(tm const &ts, TimeType type = UTC):
The object's date/time is initialized to either UTC or LOCALTIME using
ts's fields, representing the broken down time in
either UTC (type == UTC) or local time (type == LOCALTIME).
- DateTime(tm const &ts, int zoneMinutes):
The object's date/time is initialized to a local time using ts's
fields, representing a UTC time point, to which a time shift of
zoneMinutes minutes is added. DST is not used;
- DateTime(tm const &ts, int zoneMinutes, DSTSpec const &dstSpec):
The object's date/time is initialized as in the previous constructor.
Then the dstSpec object defines how DST is handled;
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(std::string const &timeStr, TimeType type = UTC):
The object's date/time is initialized to either UTC or LOCALTIME using
timeStr's standard textual time representation. Time
representations without explicit time zone shifts (cf. the first
standard textual time representation) are interpreted as UTC times
(type == UTC) or as local times (type == LOCALTIME).
- DateTime(std::string const &timeStr, int zoneMinutes):
The object's date/time is initialized to a local time based on the
standard time representation encountered in timeStr. Time
representations without explicit time zone shifts (cf. the first
standard textual time representation) are interpreted as UTC
times. Other time representations use their time zone shift
information to determine their equivalent UTC time. Once the UTC time
is available a time shift of zoneMinutes minutes is added to
determine the object's local time. DST is not used;
- DateTime(std::string const &timeStr, int zoneMinutes):
The object's date/time is initialized as in the previous constructor.
Then the dstSpec object defines how DST is handled;
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.
- DateTime(std::istream &in, TimeType type = UTC):
The object's date/time is initialized to either UTC or LOCALTIME using
standard textual time representations extracted from in. Time
representations without explicit time zone shifts (cf. the first
standard textual time representation) are interpreted as UTC times
(type == UTC) or as local times (type == LOCALTIME).
- DateTime(std::istream &in, int zoneMinutes):
The object's date/time is initialized to a local time based using
standard time representations extracted from in. Time
representations without explicit time zone shifts (cf. the first
standard textual time representation) are interpreted as UTC
times. Other time representations use their time zone shift
information to determine their equivalent UTC times. Once the UTC time
is available a time shift of zoneMinutes minutes is added to
determine the object's local time. DST is not used;
- DateTime(std::istream &in, int zoneMinutes):
The object's date/time is initialized as in the previous constructor.
Then the dstSpec object defines how DST is handled;
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.
- DayTime::TriVal dst() const:
Returns YES if the current object's time includes a DST shift,
otherwise NO is returned. The value UNKNOWN is not used
anymore;
- size_t hours() const:
Returns the number of hours represented by the current object's time
(0-23);
- DateTime localTime() const:
Returns a copy of the current object representing its local
time. If the current object represents UTC time then a DateTime
object representing the local time using the computer's default time
zone is returned;
- size_t minutes() const:
Returns the number of minutes represented by the current object (0-59);
- DateTime::Month month() const:
Returns the Month represented by the current object;
- size_t monthDayNr() const:
Returns the day number of the month represented by the current
object's date (1-31);
- string rfc2822() const:
Returns the current object's date/time displayed according to the
format specified in RFC 2822. This format is used, e.g., by the
date -R command (cf. date(1)). For example:
Mon, 3 Dec 2018 13:49:10 +0100
- string rfc3339() const:
Returns the current object's date/time displayed according to the the
format specified in RFC 3339. This format is used, e.g., by the
date --rfc-3339=seconds command (cf. date(1)). For example:
2018-12-03 13:29:11+01:00
- size_t seconds() const:
Returns the number of seconds represented by the current object's time
(0-59, but 60 and 61 may occur due to possible leap seconds);
- bool setDay(int dayNr):
Assigns dayNr to the day number of the object's current
month. Since day numbers start at 1, passing 0 or negative values to
setDay results in resetting the objects date to an earlier month.
For backward compatibility reasons true is returned;
- bool setFields(tm const &ts, DateTime::TimeFields fields):
Assigns the fields of ts specified by a bit_or combination of
TimeFields values in fields to the corresponding current
object's date/time tm fields.
For backward compatibility reasons true is returned;
- bool setHours(int hours):
Assigns hours to the number of hours of the current object's
time.
For backward compatibility reasons true is returned;
- bool setMinutes(int minutes):
Assigns minutes to the number of minutes of the current object's
time.
For backward compatibility reasons true is returned;
- bool setMonth(int month):
Assigns month to the current object's month. January is
represented by 0, December by 11. Smaller or larger values refer to
previous or future years.
For backward compatibility reasons true is returned;
- bool setMonth(DateTime::Month month):
Assigns month to the current object's month.
For backward compatibility reasons true is returned;
- bool setMonth(DateTime::Month month, DateTime::Relative where =
THIS_YEAR):
Assigns month to the current object's month. By default the month
of the current year is updated (where == THIS_YEAR). Use
LAST to ensure that the month is set before the current
object's month (e.g., if the current month is JUNE, then
requesting AUGUST, LAST will decrement the object's year, but
requesting MAY, LAST won't). Analogously, when specifying
DateTime::NEXT the resulting month is set after the current
object's month.
For backward compatibility reasons true is returned;
Caveat: If the day number of the current month exceeds the number of
days in the requested month, the object's month and day number will be
updated to the next month. E.g., if the current day number equals 31,
and NOVEMBER is requested, then the object's date is updated to
December 1.
- bool setSeconds(int seconds):
Assigns seconds to the number of seconds of the current object's
time.
For backward compatibility reasons true is returned;
- bool setTime(time_t time):
Assigns time as the number of seconds since the epoch
to the current object's UTC time.
For backward compatibility reasons true is returned;
- bool setWeekday(Weekday day, Relative where = NEXT):
Assigns day to the current object's day of the week. By default
(where == NEXT) the day will be in the future (maybe the current
week, maybe in the next week: if the current day equals Monday and
Friday is specified, the current week is used. If the current day
equals Friday and Monday is requested the next week is used). By
specifying LAST for where the day will be in the past
(likewise, if the current day equals Friday and Monday is requested,
the current week is used). By specifying where = THIS_WEEK then
day will be in the current week.
For backward compatibility reasons true is returned;
- bool setYear(size_t year):
Assigns year to the current object's date. Note that year is
the actual calendar year, and not, e.g., the year relative to the
beginning of the epoch.
For backward compatibility reasons true is returned;
- void swap(DateTime &other):
Swaps the current and other DateTime objects;
- tm const *timeStruct() const:
Returns a pointer to the object's current tm representing its
object's broken down time elements. If the object holds a local time
the tm struct represents the local time (if applicable: including
a DST shift), otherwise it represents UTC;
- DateTime to(DateTime::TimeType type) const:
Returns a copy of the current object representing its UTC time if
DateTime::UTC was specified, If LOCALTIME is specified and the
current object's type is LOCALTIME then a copy of the current
object is returned. If the current object's type is UTC then a
copy of the current object is returned as a LOCALTIME object,
using the computer's default time zone;
- DateTime utc() const:
Returns a copy of the current object representing its UTC time;
- time_t utcSeconds() const:
Returns the current object's (UTC) time in seconds since the epoch;
- DateTime::Weekday weekday() const:
Returns the current object's Weekday value;
- size_t weekNr() const:
Returns the week number of the current object's date. Week numbers are
numbers of complete weeks. If Jan 1st is a Sunday then the week
numbers of Jan 1st through Jan 6th are returned as 1, otherwise the
week numbers of Jan 1st through the date of the first Saturday of the
year (which could very well be Jan 1st) are returned as 0;
- size_t year() const:
Returns the year of the current object's date (note: this is the actual
calendar year, not the year since the epoch);
Returns the day in the year of the current object's date. January 1
is returned as 0;
- size_t yearDayNr() const:
Returns the day in the year of the current object's date. January 1
is returned as 1;
- int zoneMinutes() const:
Returns the current object's time zone shift in minutes relative to
UTC.
STATIC MEMBERS
- void addZone(std::string const &name, int zoneMinutes,
DSTSpec const &spec):
Defines time zone name, shifted zoneMinutes from UTC, using
DST specification spec. If a zone by that name already exists
its existing definition is replaced by the one provided by
addZone. The time zone CET is
predefined using specification 60, DSTSpec{ true };
- int defaultZoneMinutes():
Returns the computer's idea of its current time zone shift in minutes;
- int minutes(std::string const &zoneName):
Returns the time zone shift in minutes of time zone zoneName. If no
such zone has been defined an exception is thrown;
- void readZones(std::string const &fname):
The file fname contains time zone names and their
specifications. Empty lines and lines beginning with # are
ignored. The following time zone specification formats are recognized:
# zone NAME: zone shift 60 min. wrt UTC and does not use DST
NAME: 60 false
# zone NAME: same zone shift, uses standard DST dates
NAME: 60 true
# zone NAME: same as above, DST shift is 30 minutes
NAME: 60 true 30
# zone NAME: same zone shift, std DST shift from May 1 thru Sep 30
NAME: 60 0501 0930
# zone NAME: same as above, DST shift is 30 minutes backward
NAME: 60 0501 0930 -30
- void tm2cout(char const *label, TM const &ts):
This static member is used for debugging purposes. It displays the
values of ts's fields, preceded by label and ": ";
- std::string zoneTxt(int zoneMinutes):
The zone minutes are converted to a hh:mm string representation/
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.
- operator bool() const:
Returns true;
- size_t error() const:
Returns 0;
- void setValid():
Performs no actions;
- bool setFields(struct tm const &timeStruct, int fields):
Use setFields(struct tm const &timeStruct, TimeFields fields);
- bool set...:
The set-members (e.g., setSeconds) return true for backward
compatibility. Starting with Bobcat version 5 these members will
return void;
- time_t time() const:
Use utcSeconds();
- bool valid() const:
Returns true;
- int displayZoneShift() const:
Use displayZoneMinutes().
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_4.09.00-x.dsc: detached signature;
- bobcat_4.09.00-x.tar.gz: source archive;
- bobcat_4.09.00-x_i386.changes: change log;
- libbobcat1_4.09.00-x_*.deb: debian package holding the
libraries;
- libbobcat1-dev_4.09.00-x_*.deb: debian package holding the
libraries, headers and manual pages;
- http://sourceforge.net/projects/bobcat: public archive location;
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).