Rudiments
|
#include <datetime.h>
Inherits object.
Static Public Member Functions | |
static const char *const * | getTimeZoneAbbreviations () |
static const int32_t * | getTimeZoneOffsets () |
static bool | isValidDateTime (const char *string) |
static char * | getString (time_t seconds) |
static char * | getString (time_t seconds, time_t microseconds) |
static char * | getString (const void *tmstruct) |
static time_t | getEpoch (const char *datestring) |
static time_t | getEpoch (const void *tmstruct) |
static bool | parse (const char *datetime, bool ddmm, bool yyyyddmm, const char *datedelimiters, int16_t *year, int16_t *month, int16_t *day, int16_t *hour, int16_t *minute, int16_t *second, int32_t *microsecond, bool *isnegative) |
static char * | formatAs (const char *format, int16_t year, int16_t month, int16_t day, int16_t hour, int16_t minute, int16_t second, int32_t microsecond, bool isnegative) |
static void | setMutex (threadmutex *mtx) |
The datetime class represents a date/time combination and provides methods for getting and setting dates and times, converting date/time formats and accessing various date/time values.
datetime::datetime | ( | ) |
Creates an instance of the datetime class.
datetime::datetime | ( | datetime & | d | ) |
Creates an instance of the datetime class that is a copy of "d".
datetime::~datetime | ( | ) |
Destroys this instance of the datetime class.
bool datetime::addDays | ( | int32_t | days | ) |
Adds "days" to the date/time represented in this instance. "days" may be negative.
bool datetime::addHours | ( | int32_t | hours | ) |
Adds "hours" to the date/time represented in this instance. "hours" may be negative.
bool datetime::addMicroseconds | ( | int32_t | microseconds | ) |
Adds "microseconds" to the date/time represented in this instance. "microseconds" may be negative.
bool datetime::addMinutes | ( | int32_t | minutes | ) |
Adds "minutes" to the date/time represented in this instance. "minutes" may be negative.
bool datetime::addMonths | ( | int32_t | months | ) |
Adds "months" to the date/time represented in this instance. "months" may be negative.
bool datetime::addSeconds | ( | int32_t | seconds | ) |
Adds "seconds" to the date/time represented in this instance. "seconds" may be negative.
bool datetime::addYears | ( | int32_t | years | ) |
Adds "years" to the date/time represented in this instance. "years" may be negative.
|
static |
Allocates and returns a date/time string, composed of the components "year", "month", "day", "hour", "minute", "second", and "microsecond" as specified by the format string "format". If "isnegative" is set true then the hour component will be negative.
"format" may contain any of the following format strings:
DD - 2 digit day, left padded with 0 D - 1 or 2 digit day, not left padded with 0 MM - 2 digit month, left padded with 0 MON - uppercase, 3-character month abbreviation Month - mixed-case full name of the month YYYY - 4 digit year, left padded with 0 YY - 2 digit year, left padded with 0 HH24 - 2 digit hour, in 24-hour format, left padded with 0 HH - 2 digit hour, in 12-hour format, left padded with 0 MI - 2 digit minute, left padded with 0 SS - 2 digit second, left padded with 0 FFFFFF - 6 digit fractional second, left padded with 0 FFFFF - 5 digit fractional second, left padded with 0 FFFF - 4 digit fractional second, left padded with 0 FFF - 3 digit fractional second, left padded with 0 FF - 2 digit fractional second, left padded with 0 F - 1 digit fractional second AM - AM or PM
Any other characters in "format" will be reprouduced verbatim.
Returns the formatted string or NULL if "format" was NULL.
int32_t datetime::getCentury | ( | ) |
Returns the (one-based) century component of the date/time currently represented in this instance.
int32_t datetime::getDayOfMonth | ( | ) |
Returns the day-of-the-month component (1-31) of the date/time currently represented in this instance.
int32_t datetime::getDayOfWeek | ( | ) |
Returns the day-of-the-week component (1-7) of the date/time currently represented in this instance.
int32_t datetime::getDayOfYear | ( | ) |
Returns the day-of-the-year component (1-365) of the date/time currently represented in this instance.
time_t datetime::getEpoch | ( | ) |
Returns the number of seconds since 1970 (the epoch).
Parses "datestring" which must be of the format: "mm/dd/yyyy hh:mm:ss TZN" and returns the number of seconds since 1970 (the epoch).
Note that TZN must be a valid timezone. Otherwise GMT is assumed.
Takes "tmstruct", a pointer to a platform-specific time structure (struct tm * on unix) and returns the number of seconds since 1970 (the eopch).
Note that in "tmstruct", the timezone and GMT offset must be set to valid values. Otherwise GMT is assumed.
int32_t datetime::getHour | ( | ) |
Returns the hour component of the date/time currently represented in this instance.
int32_t datetime::getMicrosecond | ( | ) |
Returns the microsecond component of the date/time currently represented in this instance.
int32_t datetime::getMinute | ( | ) |
Returns the minute component of the date/time currently represented in this instance.
int32_t datetime::getMonth | ( | ) |
Returns the month component (1-12) of the date/time currently represented in this instance.
Returns the abbreviated name of the month component of the date/time currently represented in this instance.
Returns the full name of the month component of the date/time currently represented in this instance.
int32_t datetime::getSecond | ( | ) |
Returns the second component of the date/time currently represented in this instance.
int32_t datetime::getShortYear | ( | ) |
Returns the year component, not including the century, of the date/time currently represented in this instance.
Returns a string of the format: "yyyy-mm-dd hh:mm:ss" for the date/time represented in this instance.
(Note that this method returns a pointer to an internal string which will be deleted if this instance is deleted.)
Returns a string representing the date/time in this instance.
If microseconds is false then the string is of the format: "yyyy-mm-dd hh:mm:ss"
If microseconds is true then the string is of the format: "yyyy-mm-dd hh:mm:ss.mmm"
(Note that this method returns a pointer to an internal string which will be deleted if this instance is deleted.)
Returns a string of the format: "mm/dd/yyyy hh:mm:ss TZN" for the date/time represented in this instance.
(Note that this method returns a pointer to an internal string which will be deleted if this instance is deleted.)
Returns a string representing the date/time in this instance.
If microseconds is false then the string is of the format: "mm/dd/yyyy hh:mm:ss TZN"
If microseconds is true then the string is of the format: "mm/dd/yyyy hh:mm:ss:uuu TZN"
(Note that this method returns a pointer to an internal string which will be deleted if this instance is deleted.)
Takes "tmstruct", a pointer to a platform-specific time structure (struct tm * on unix) and returns a string in "mm/dd/yyyy hh:mm:ss TZN" format.
Note that this method allocates a buffer to return the string in which must be deleted by the calling program.
Takes "seconds" since the epoch (the number of seconds since 1970) and returns a string in "mm/dd/yyyy hh:mm:ss TZN" format.
Note that this method allocates a buffer to return the string in which must be deleted by the calling program.
Takes "seconds" since the epoch (the number of seconds since 1970) and "microseconds" and returns a string in "mm/dd/yyyy hh:mm:ss:uuu TZN" format.
Note that this method allocates a buffer to return the string in which must be deleted by the calling program.
Returns a NULL terminated array of timezone abbreviations.
int32_t datetime::getTimeZoneOffset | ( | ) |
Returns the offset from GMT in seconds
Returns an array of timezone offsets from GMT (in seconds). Each element of this array corresponds to an element of the array returned b getTimeZoneAbbreviations().
Returns a 3 character string representing the time zone
int32_t datetime::getWeekOfYear | ( | ) |
Returns the week-of-the-year component (0-53) of the date/time currently represented in this instance.
Note that week 1 begins with the first Monday of the year, meaning that a year will have no week 0 if it starts on a Monday.
int32_t datetime::getYear | ( | ) |
Returns the year component, including the century, of the date/time currently represented in this instance.
Initializes this instance to the date/time represented by "tmstring" where "tmstring" is of the format: "mm/dd/yyyy hh:mm:ss TZN" or of the format "mm/dd/yyyy hh:mm:ss:uuu TZN"
Note that TZN must be a valid timezone. Otherwise GMT is assumed.
Returns true on success and false on failure.
Initializes this instance to the date/time represented by "tmstruct" where "tmstruct" is a pointer to a platform-specific time structure (eg. struct tm * on unix)
Note that in "tmstruct", the timezone and GMT offset must be set to valid values. Otherwise GMT is assumed.
Returns true on success and false on failure.
Initializes this intance to the date/time represented by "seconds" where "seconds" is the number of seconds since 1970 (the epoch).
Returns true on success and false on failure.
Initializes this instance to the date/time represented by "seconds" and "microseconds", where "seconds" is the number of seconds since 1970 (the epoch) and "microseconds" is the number of microseconds after "seconds".
Returns true on success and false on failure.
This method only works if your system has a working real-time clock at /dev/rtc.
Initialies this instance to the date/time stored in the hardware clock then adjusts it to the timezone used by the system.
Returns true on success and false on failure.
This method only works if your system has a working real-time clock at /dev/rtc.
Initialies this instance to the date/time stored in the hardware clock.
"hwtz" must be set to the timezone that the hardware clock is using.
Returns true on success and false on failure.
bool datetime::initFromSystemDateTime | ( | ) |
Initialies this instance to the date/time stored in the system clock.
Returns true on success and false on failure.
bool datetime::isDaylightSavingsTime | ( | ) |
Returns true if daylight savings time is currently in effect and false if it isn't
Returns true if string represents a valid date/time and false otherwise.
For this methods to return true, "string" must be formatted like: "00/00/0000 00:00:00" optionally followed by a space and a timezone.
Makes this instance of the datetime class identical to "d".
|
static |
Attempts to parse "datetime" into its components setting "year", "month", "day", "hour", "minute", "second", "microsecond", and "isnegative" to the different parts, as appropriate.
Handles a wide variety of date/time formats.
If "ddmm" is set true then the date format is assumed to be dd/mm/yyyy rather than mm/dd/yyyy when a date with a trailing year is encountered.
If "yyyyddmm" is set true then the date format is assumed to be yyyy/dd/mm rather than yyyy/mm/dd when a date with a leading year is encountered.
"datedelimiters" may be set to a set of valid date delimiters and may contain any combination of '/', '-', '.', and ':'. Eg. "/-" would mean that only '/' and '-' are valid date delimiters. If left NULL then it defaults to "/-.:"
If a component isn't found, then it is set to -1. Eg. if a date is given with no time, then "hour", "minute", "second", and "microsecond" are set to -1.
Any of "year", "month", "day", "hour", "minute", "second", "microsecond", and "isnegative" may be NULL. In that case, that component is just not set.
"isnegative" is set true if the hour component is negative. Eg. if "-10:00" was encountered. Note that if a negative hour component is encountered, then the "hour" will be set to a positive number, and "isnegative" will be set true. In the example above, "hour" would bet set to 10 and "isnegative" would be set true.
Returns true if the date/time was successfully parsed and false if it failed to parse the date/time.
bool datetime::setDayOfMonth | ( | int32_t | day | ) |
Sets the day-of-the-month component (1-31) of the date/time represented in this instance to "day" and recalculates the current date/time (eg. if "day" is greater than 31 or less than 0).
This method only works if your system has a working real-time clock at /dev/rtc.
Sets the hardware clock's date/time to the date/time currently represented in this instance.
"hwtz" must be set to the timezone that the system clock using.
Returns true on success and false on failure.
bool datetime::setHour | ( | int32_t | hour | ) |
Sets the hour component (0-24) of the date/time represented in this instance to "hour" and recalculates the current date/time (eg. if "hour" is greater than 24 or less than 0).
bool datetime::setMicrosecond | ( | int32_t | microsecond | ) |
Sets the microsecond component (0-999999) of the date/time represented in this instance to "microsecond" and recalculates the current date/time (eg. if "microsecondsecond" is greater than 999999 or less than 0).
bool datetime::setMinute | ( | int32_t | minute | ) |
Sets the minute component (0-59) of the date/time represented in this instance to "minute" and recalculates the current date/time (eg. if "minute" is greater than 60 or less than 0).
bool datetime::setMonth | ( | int32_t | month | ) |
Sets the month component (1-12) of the date/time represented in this instance to "month" and recalculates the current date/time (eg. if "month" is greater than 12 or less than 1).
|
static |
Many of the functions that the datetime class uses internally are not reentrant and thus not thread-safe. Indeed, for some functions, there is no thread-safe version available on any platform.
If your application is multi-threaded, you must use this method to supply a mutex and ensure thread safety.
If you don't supply a mutex, the methods in the class will still work, but will not be thread-safe.
bool datetime::setSecond | ( | int32_t | second | ) |
Sets the second component (0-59) of the date/time represented in this instance "second" and recalculates the current date/time (eg. if "second" is greater than 59 or less than 0).
bool datetime::setSystemDateTime | ( | ) |
Sets the system clock's date/time to the date/time currently represented in this instance.
Returns true on success and false on failure.
Sets the timezone of the date/time represented in this instance to "newtz" and recalculates the current date/time.
Eg. if the current timezone is EST, and "newtz" is CST then the hour will be decremented by 1.
If, in the new timezone, daylight savings time is currently in effect, the time zone will be adjusted accordingly. Ie. if "newtz" = EST and daylight savings time is in effect in EST5EDT, then EDT will be used instead.
Returns true on success and false on failure.
Sets the timezone of the date/time represented in this instance to "newtz" and recalculates the current date/time.
Eg. if the current timezone is EST, and "newtz" is CST then the hour will be decremented by 1.
If ignoredst is false, this method works like setTimezone(const char *newtz). However if ignoredst is true, this method will not adjust EST to EDT if daylight savings time is in effect in EST5EDT.
Returns true on success and false on failure.
bool datetime::setYear | ( | int32_t | year | ) |
Sets the year component of the date/time represented in this instance to "year".