Does not inherit
#include rwsf/core/DateTime.h
Class rwsf::DateTime represents a date and time, stored in milliseconds, from January 1, 1901 00:00:00:000 UTC. Milliseconds are stored in a 64-bit integral type, which is represented by the global typedef rwsfint64.
rwsf::DateTime supports the ISO 8601 international standard for the formatting of dates and times.
rwsf::DateTime is based on the Gregorian Calendar. The Gregorian calendar was introduced by Pope Gregory XIII in 1582, and was adopted in various places at various times. It was adopted by England on September 14, 1752, and thus came to the United States. rwsf::DateTime was designed for use with dates after September 14, 1752.
Four public member functions, all starting with julian, let you manipulate the date via Modified Julian days (MJD). Note that the Modified Julian day number is not the same as a date according to the Julian calendar. The Modified Julian day number is calculated using Algorithm 199 from Communications of the ACM, Volume 6, No. 8, (Aug. 1963), p. 444 and is valid for any valid Gregorian date in the Gregorian calendar.
Note
The modern Modified Julian Day (MJD) was introduced in the 1950's and starts at midnight, as opposed to the historic Julian Day (JD), which began at 12 noon. It is defined as
MJD = JD - 2400000.5.
The half day is subtracted so that the day starts at midnight in conformance with civil time reckoning.
Modified Julian days in the rwsf::DateTime function correctly from Modified Julian day 0, but can only be converted to month, date, year for Modified Julian days that fall within the Gregorian Calendar.
Note that two-digit year specifiers assume the century 1900, so you will want to create programs that use four-digit year specifiers.
rwsf::DateTimes can be converted to and from the Standard C Library type structtm defined in <time.h>, and listed here in Table 2.
inttm_sec; |
seconds after the minute - [0,59] |
inttm_min; |
minutes after the hour - [0,59] |
inttm_hour; |
hours since midnight - [0,23] |
inttm_mday; |
day of the month - [1,31] |
inttm_mon; |
months since January - [0,11] |
inttm_year; |
years since 1900 |
inttm_wday; |
days since Sunday - [0,6] |
inttm_yday; |
days since January 1 - [0,365] |
inttm_isdst; |
daylight savings time flag |
The default constructor for this class creates an instance with an invalid date and time, which facilitates creating a large array, as follows:
// Creates an array with 5000 undefined dates and times rwsf::DateTime v[5000];
rwsf::DateTime objects can hold a variety of sentinel values that do not refer to actual dates. Consequently it is important to know whether an object holds an actual date before performing many operations. The isSentinel() function returns true when an object it is applied to hold a sentinel value.
Note that the use of the isValid() function does not determine whether a member function can be successfully applied to an object since the null sentinel value represents a valid rwsf::DateTime whose value is not yet determined.
Format { iso8601, http }
Specifies the format to be used for converting between std::string and rwsf::DateTime using either the ISO 8601 standard, or HTTP specification date formats.
InitialState { invalid, null, setCurrentTime }
Specifies whether the constructor should construct an invalid, null, or current rwsf::DateTime. Default is to set an invalidrwsf::DateTime, with an undefined date and time.
SetType { setDate, setTime, setBoth }
Specifies whether the constructor should set just the date, just the time or both. Default is to set both.
DateTime(InitialState init_state = invalid);
Default constructor. Constructs an rwsf::DateTime with undefined date and time. If init_state is set to DateTime::setCurrentTime, an instance with the current date and time is created. If init_state is set to DateTime::null, an instance set to null is created.
DateTime(const DateTime & dt);
Copy constructor.
DateTime(rwsfint64 msec);
Constructs an rwsf::DateTime from the number of milliseconds since 1 January 1901 00:00:00:000 UTC. rwsfint64 is at least a 64 bit integral type. msec may be negative for representing times prior to midnight 01/01/1901.
For example, here is how to construct an rwsf::DateTime representing a time 230,375 milliseconds after midnight UTC on 01/01/1901:
Note
This constructor must be passed an instance of a rwsfint64 to ensure unambiguous construction.
rwsf::DateTime dt(rwsfint64(230375));
DateTime(const struct tm *, unsigned ms = 0, const rwsf::TimeZone & = rwsf::TimeZone::local());
Constructs an rwsf::DateTime from the tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec components of the structtm argument with the milliseconds portion of the rwsf::DateTime as specified by ms. The components are understood to be relative to the time zone passed in, which defaults to local time. Note that the numbering of the months and years in a structtm differs from that used in rwsf::DateTime arguments, which start at the number 1. The arguments included in structtm are shown above in Table 2.
DateTime(unsigned, const char * month, unsigned year, unsigned h, unsigned m, unsigned s, unsigned ms, const rwsf::Locale & loc = rwsf::Locale::global(), const rwsf::TimeZone & zone = rwsf::TimeZone::local());
Constructs an rwsf::DateTime using the specified day, month, year, hour, minute, second, and millisecond, all relative to the time zone specified, which defaults to local time. The optional locale argument is used to convert the month name.
DateTime(unsigned d, unsigned mo, unsigned y, unsigned h = 0, unsigned m = 0, unsigned s = 0, unsigned ms = 0, const rwsf::TimeZone & = rwsf::TimeZone::local());
Constructs an rwsf::DateTime using the specified day, month, year, hour, minute, second, and millisecond, all relative to the time zone specified, which defaults to local time.
DateTime(const std::string & str, SetType set_type = setBoth, const rwsf::Locale & loc = rwsf::Locale::global(), const rwsf::TimeZone & zone = rwsf::TimeZone::local());
Converts the string str to a date and/or time as indicated by set_type, all relative to the time zone specified, which defaults to local time. By default, both the date and time are set using loc locale. The constructor expects the date and time portions of the std::string to be separated by a semicolon when both date and time are being set. The locale argument is used to convert the month name. The member function isValid() must be used to test whether the results are a valid date. Because rwsf::Locale cannot rigorously check date input, dates created this way should also be reconfirmed by the user.
Example:
rwsf::DateTime("April 4, 1998; 10:00 am", rwsf::DateTime::setBoth, rwsf::Locale::global()); // sets date and time rwsf::DateTime("April 4, 1998", rwsf::DateTime::setDate, rwsf::Locale::global()); // sets date and // initializes time to 00:00:00 rwsf::DateTime("10:00 am", rwsf::DateTime::setTime, rwsf::Locale::global()); // initializes date to // today at time 10:00
DateTime(const std::string & str, Format f, const rwsf::Locale & loc = rwsf::Locale::global(), const rwsf::TimeZone & zone = rwsf::TimeZone::local());
Constructs an rwsf::DateTime from the string str using the format specified by f. rwsf::DateTime currently supports the ISO 8601 format which is numeric and locale-neutral; as such the rwsf::Locale argument is ignored for ISO 8601, but is reserved for future use. The constructor uses the rwsf::TimeZone argument unless the string specifies the UTC time zone or an offset from UTC, in which case the rwsf::TimeZone argument is ignored in favor of UTC.
Two Examples:
// Construct an rwsf::DateTime of 11:59:59 PM on December // 24th,2001 in the rwsf::TimeZone::local() time zone. rwsf::DateTime("2001-12-24T23:59:59",rwsf::DateTime::iso8601); // Construct an rwsf::DateTime of 8 AM, April 12th, 1985 in // the GMT time zone rwsf::DateTime("1985-04-12T08:00:00Z", rwsf::DateTime::iso8601);
For a date or time supplied singly (for example, "2001-12-24" or "23:59:59"), rwsf::DateTime calls the system to retrieve the current date or time.
DateTime beginDST(unsigned year, const rwsf::TimeZone & zone = rwsf::TimeZone::local());
Returns the start of daylight saving time (DST) for the given year, in the given time zone. Returns an "invalid" time if DST is not observed in that year and zone.
unsigned dayOfWeek(const char * day, const rwsf::Locale & loc = rwsf::Locale::global());
Returns the number of the day of the week corresponding to the given day name, where Monday = 1, ..., Sunday = 7. Names are interpreted according to the optional locale argument. The function defaults to using the rwsf::Locale global locale.
unsigned daysInMonthYear(unsigned month, unsigned year);
Returns the number of days in a given month and year. Returns 0 if month is not between 1 and 12 inclusive.
unsigned daysInYear(unsigned year);
Returns the number of days in a given year.
bool dayWithinMonth(unsigned mon, unsigned day, unsigned yr);
Returns true if a day (1-31) is within a given month in a given year.
DateTime endDST(unsigned year, const rwsf::TimeZone & = rwsf::TimeZone::local());
Returns the end of daylight saving time for the given year, in the given time zone. Returns an "invalid time" if DST is not observed in that year and zone.
unsigned hash(const DateTime & dt);
Returns the hash value of dt as returned by dt.hash().
unsigned indexOfMonth(const char * month, const rwsf::Locale & loc = rwsf::Locale::global());
Returns a number between 1 and 12 corresponding to the given month name. Returns 0 for no match. Months are interpreted by the optional locale argument. The function defaults to using the rwsf::Locale global locale.
bool leapYear(unsigned year);
Returns true if a given year is a leap year.
std::string nameOfMonth(unsigned monthNum, const rwsf::Locale & loc = rwsf::Locale::global());
Returns the name of month, where January = 1, ..., December = 12. Months are interpreted by the optional locale argument. The function defaults to using the rwsf::Locale global locale.
DateTime now();
Returns current date and time.
DateTime userSentinel(int i);
Returns user-defined sentinel number i. Throws rwsf::OutOfBoundsException if the argument is outside the range 0 to 127.
std::string weekDayName(unsigned dayNum, const rwsf::Locale & loc = rwsf::Locale::global());
Returns the name of the day of the week dayNum where Monday = 1, ..., Sunday = 7. Days are interpreted by the optional locale argument. The function defaults to using the rwsf::Locale global locale.
std::string asString(char format, const rwsf::Locale & loc = rwsf::Locale::global(), const rwsf::TimeZone & = rwsf::TimeZone::local()) const;
Returns the date and time as a string, formatted by the optional locale argument. The function defaults to using the rwsf::Locale global locale.
Formats are as defined in the pre-1999 Standard C Library function strftime(), explained in the description for rwsf::Locale.
std::string asString(const char * format, const rwsf::Locale & loc = rwsf::Locale::global(), const rwsf::TimeZone & = rwsf::TimeZone::local()) const;
Returns the date and time as a string, formatted by the optional locale argument. The function defaults to using the rwsf::Locale global locale.
Formats are as defined in the pre-1999 Standard C Library function strftime(), explained in the description for rwsf::Locale.
std::string asString(Format f, const rwsf::Locale & loc = rwsf::Locale::global(), const rwsf::TimeZone & = rwsf::TimeZone::local()) const;
Returns the date and time as a string, using the specified format.
Currently supports the ISO 8601 format. This format is numeric and locale-neutral, and as such does not use the rwsf::Locale argument.
bool between(const DateTime & a, const DateTime & b) const;
Returns true if self is between a and b, inclusive.
int compareTo(const DateTime * d) const;
Compares self to the rwsf::DateTime pointed to by d and returns:
0 if self == *d; 1 if self > *d; -1 if self < *d.
int compareTo(const DateTime & d) const;
Compares self to the rwsf::DateTime referenced by d and returns:
0 if self == d; 1 if self > d; -1 if self < d.
unsigned correctedJulian() const;
Returns the value of the Julian day number. This value is one day less than the value of DateTime::julian() if the time is before noon UTC (GMT).
unsigned day(const rwsf::TimeZone & = rwsf::TimeZone::local()) const;
Returns the day of the year (1-366) for this date. Default for optional time zone argument is local.
unsigned dayGMT() const;
Returns the day of the year (1-366) for this date relative to UTC (GMT).
unsigned dayOfMonth(const rwsf::TimeZone & = rwsf::TimeZone::local()) const;
Returns the day of the month (1-31) for this date. Default for optional time zone argument is local.
unsigned dayOfMonthGMT() const;
Returns the day of the month (1-31) for this date relative to UTC (GMT).
DateTime & decrementHour(const rwsfint64 hour);
Decrements the class's milliseconds data member by hour*DateTimemillisecsInHour, then returns self.
Note
If this operation results in crossing a DST boundary in the instance's time zone, outputting the date will present with a DST offset as well.
DateTime & decrementMillisecond(const rwsfint64 millisecond);
Decrements the classes' milliseconds data member by millisecond, then returns self.
Note
If this operation results in crossing a DST boundary in the instance's time zone, outputting the date will present with a DST offset as well.
DateTime & decrementMinute(const rwsfint64 minute);
Decrements the classes' milliseconds data member by minute*DateTimemillisecsInMin, then returns self.
Note
If this operation results in crossing a DST boundary in the instance's time zone, outputting the date will present with a DST offset as well.
DateTime & decrementSecond(const rwsfint64 second);
Decrements the classes' milliseconds data member by second*DateTimemillisecsInSec, then returns self.
Note
if this operation results in crossing a DST boundary in the instance's time zone, outputting the date will present with a DST offset as well.
void extract(struct tm * tmbuf, const rwsf::TimeZone & zone = rwsf::TimeZone::local()) const;
Returns with the structtm argument filled out completely. Note that the encoding for months and days of the week used in structtm differs from that used elsewhere in rwsf::DateTime. If the date is invalid, all fields are set to -1. Default for optional time zone argument is local.
void extractGMT(struct tm * tmbuf) const;
Returns with the structtm argument filled out completely, relative to UTC (GMT). Note that the encoding for months and days of the week used in structtm differs from that used elsewhere in rwsf::DateTime. If the date is invalid, all fields are set to -1.
unsigned firstDayOfMonth(unsigned mon, const rwsf::TimeZone & = rwsf::TimeZone::local()) const;
Returns the day of the year (1-366) corresponding to the first day of mon in this rwsf::DateTime's year. Default for optional time zone argument is local.
unsigned firstDayOfMonth(const rwsf::TimeZone & zone = rwsf::TimeZone::local()) const;
Returns the day of the year (1-366) corresponding to the first day of this rwsf::DateTime's month and year.
unsigned hash() const;
Returns a suitable hashing value.
unsigned hour(const rwsf::TimeZone & = rwsf::TimeZone::local()) const;
Returns the hour relative to the time zone specified. Default for optional time zone argument is local.
unsigned hourGMT() const;
Returns the hour relative to UTC (GMT).
DateTime & incrementHour(const rwsfint64 hour);
Increments the class' milliseconds data member by hour*DateTimemillisecsInHour, then returns self.
Note
If this operation results in crossing a DST boundary in the instance's time zone, outputting the date will present with a DST offset as well.
DateTime & incrementMillisecond(const rwsfint64 millisecond);
Increments the class' milliseconds data member by millisecond, then returns self.
Note
If this operation results in crossing a DST boundary in the instance's time zone, outputting the date will present with a DST offset as well.
DateTime & incrementMinute(const rwsfint64 minute);
Increments the classes' milliseconds data member by minute*DateTimemillisecsInMin, then returns self.
Note
If this operation results in crossing a DST boundary in the instance's time zone, outputting the date will present with a DST offset as well.
DateTime & incrementSecond(const rwsfint64 second);
Increments the classes' milliseconds data member by second*DateTimemillisecsInSec, then returns self.
Note
If this operation results in crossing a DST boundary in the instance's time zone, outputting the date will present with a DST offset as well.
bool isDST(const rwsf::TimeZone & zone = rwsf::TimeZone::local()) const;
Returns true if self is during daylight saving time in the time zone given by zone, false otherwise.
bool isFuture() const;
Returns true if self is a future sentinel.
bool isInvalid() const;
Returns true if self is an invalid sentinel.
bool isNull() const;
Returns true if self is a null sentinel.
bool isPast() const;
Returns true if self is a past sentinel.
bool isSentinel() const;
Returns true if self is a sentinel of any kind.
bool isValid() const;
Returns true if self represents an actual time or is the null sentinel, false otherwise.
unsigned julian() const;
Returns the value of the Modified Julian day number.
void julian(unsigned j);
Sets the value of the Modified Julian day number to j.
double julianDay() const;
Returns the value of the current date and time as a Modified Julian day number. The integer/whole number part of the double indicates the day, and the fractional part indicates the time. For example, the Modified Julian day number for 1/1/1901 is 2415386. Since Modified Julian days start at midnight GMT, 12 a.m. is 2415386.5 and 6 p.m. is 2415386.75 (these values are computed for Greenwich meridian - a different time zone modifies the result of julianDay).
void julianDay(double j);
Sets the value of the date and time to the Modified Julian day number j. The integer/whole number part of the double indicates the day, and the fractional part indicates the time. For example, the Julian day number for 1/1/1901 is 2415386. Since Julian days start at midnight GMT, 12 a.m. is 2415386.5 and 6 p.m. is 2415386.75 (these values are interpreted differently in rwsf::DateTime objects that have different time zones).
bool leap(const rwsf::TimeZone & zone = rwsf::TimeZone::local()) const;
Returns true if self is a leap year relative to the rwsf::TimeZone argument.
bool leapGMT() const;
Returns true if self is a leap year relative to UTC (GMT).
DateTime max(const DateTime & t) const;
Returns the later rwsf::DateTime of self or t.
unsigned milliSecond() const;
Returns the millisecond part of self.
rwsfint64 milliSeconds() const;
Returns the number of milliseconds since 00:00:00:000 January 1, 1901 UTC.
DateTime min(const DateTime & t) const;
Returns the earlier rwsf::DateTime of self or t.
unsigned minute(const rwsf::TimeZone & = rwsf::TimeZone::local()) const;
Returns the minute, adjusted to the time zone specified.
unsigned minuteGMT() const;
Returns the minute relative to UTC (GMT).
unsigned month(const rwsf::TimeZone & = rwsf::TimeZone::local()) const;
Returns the month (1-12) for this date. Default for optional time zone argument is local.
unsigned monthGMT() const;
Returns the month (1-12) for this date relative to UTC (GMT).
std::string monthName(const rwsf::Locale & loc = rwsf::Locale::global(), const rwsf::TimeZone & = rwsf::TimeZone::local()) const;
Returns the name of the month for this date, according to the optional locale argument. The function defaults to using the rwsf::Locale global locale.
DateTime next(unsigned dayNum, const rwsf::TimeZone & = rwsf::TimeZone::local()) const;
Returns the date of the next day of the week, where Monday = 1, ..., Sunday = 7. The variable dayNum must be between 1 and 7, inclusive. Default for optional time zone argument is local.
DateTime next(const char * dayName, const rwsf::Locale & loc = rwsf::Locale::global(), const rwsf::TimeZone & = rwsf::TimeZone::local()) const;
Returns the date of the next dayName (for example, the date of the next Monday). The weekday name is interpreted according to the optional locale argument.
DateTime previous(unsigned dayNum, const rwsf::TimeZone & = rwsf::TimeZone::local()) const;
Returns the date of the previous numbered day of the week, where Monday = 1, ..., Sunday = 7. The variable dayNum must be between 1and 7, inclusive. Default for optional time zone argument is local.
DateTime previous(const char * dayName, const rwsf::Locale & loc = rwsf::Locale::global(), const rwsf::TimeZone & = rwsf::TimeZone::local()) const;
Returns the day of the previous dayName; for example, the date of the previous Monday. The weekday name is interpreted according to the optional locale argument. The function defaults to using the rwsf::Locale global locale.
unsigned second() const;
Returns the second; local time or UTC (GMT).
unsigned weekDay(const rwsf::TimeZone & = rwsf::TimeZone::local()) const;
Returns the number of the day of the week for this date, where Monday = 1, ..., Sunday = 7.
unsigned weekDayGMT() const;
Returns the number of the day of the week for this date relative to UTC (GMT).
std::string weekDayName(const rwsf::Locale & loc = rwsf::Locale::global(), const rwsf::TimeZone & = rwsf::TimeZone::local()) const;
Returns the name of the day of the week for this date, according to the optional locale argument. The function defaults to using the rwsf::Locale global locale.
unsigned year(const rwsf::TimeZone & = rwsf::TimeZone::local()) const;
Returns the year of self. Default for optional time zone argument is local.
unsigned yearGMT() const;
Returns the year of self relative to UTC (GMT).
DateTime & operator=(const DateTime & dt);
Assignment operator.
© Copyright Rogue Wave Software, Inc. All Rights Reserved. All Rights Reserved. Rogue Wave is a registered trademark of Rogue Wave Software, Inc. in the United States and other countries. HydraExpress is a trademark of Rogue Wave Software, Inc. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.