Rudiments
Public Member Functions | Static Public Member Functions | List of all members
datetime Class Reference

Inherits object.

Public Member Functions

 datetime ()
 
 datetime (datetime &d)
 
datetimeoperator= (datetime &d)
 
 ~datetime ()
 
bool init (const char *tmstring)
 
bool init (time_t seconds)
 
bool init (time_t seconds, time_t microseconds)
 
bool init (const void *tmstruct)
 
bool initFromSystemDateTime ()
 
bool initFromHardwareDateTime (const char *hwtz)
 
bool initFromAdjustedHardwareDateTime (const char *hwtz)
 
bool setSystemDateTime ()
 
bool setHardwareDateTime (const char *hwtz)
 
bool setMicrosecond (int32_t microsecond)
 
bool setSecond (int32_t second)
 
bool setMinute (int32_t minute)
 
bool setHour (int32_t hour)
 
bool setDayOfMonth (int32_t day)
 
bool setMonth (int32_t month)
 
bool setYear (int32_t year)
 
bool setTimeZone (const char *newtz)
 
bool setTimeZone (const char *newtz, bool ignoredst)
 
bool addSeconds (int32_t seconds)
 
bool addMicroseconds (int32_t microseconds)
 
bool addMinutes (int32_t minutes)
 
bool addHours (int32_t hours)
 
bool addDays (int32_t days)
 
bool addMonths (int32_t months)
 
bool addYears (int32_t years)
 
int32_t getHour ()
 
int32_t getMinute ()
 
int32_t getSecond ()
 
int32_t getMicrosecond ()
 
int32_t getMonth ()
 
const char * getMonthName ()
 
const char * getMonthAbbreviation ()
 
int32_t getDayOfMonth ()
 
int32_t getDayOfWeek ()
 
int32_t getDayOfYear ()
 
int32_t getWeekOfYear ()
 
int32_t getYear ()
 
int32_t getShortYear ()
 
int32_t getCentury ()
 
bool isDaylightSavingsTime ()
 
const char * getTimeZoneString ()
 
int32_t getTimeZoneOffset ()
 
const char * getString ()
 
const char * getString (bool microseconds)
 
const char * getSqlString ()
 
const char * getSqlString (bool microseconds)
 
time_t getEpoch ()
 
- Public Member Functions inherited from object
virtual ~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)
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ datetime() [1/2]

datetime::datetime ( )

Creates an instance of the datetime class.

◆ datetime() [2/2]

datetime::datetime ( datetime d)

Creates an instance of the datetime class that is a copy of "d".

◆ ~datetime()

datetime::~datetime ( )

Destroys this instance of the datetime class.

Member Function Documentation

◆ addDays()

bool datetime::addDays ( int32_t  days)

Adds "days" to the date/time represented in this instance. "days" may be negative.

◆ addHours()

bool datetime::addHours ( int32_t  hours)

Adds "hours" to the date/time represented in this instance. "hours" may be negative.

◆ addMicroseconds()

bool datetime::addMicroseconds ( int32_t  microseconds)

Adds "microseconds" to the date/time represented in this instance. "microseconds" may be negative.

◆ addMinutes()

bool datetime::addMinutes ( int32_t  minutes)

Adds "minutes" to the date/time represented in this instance. "minutes" may be negative.

◆ addMonths()

bool datetime::addMonths ( int32_t  months)

Adds "months" to the date/time represented in this instance. "months" may be negative.

◆ addSeconds()

bool datetime::addSeconds ( int32_t  seconds)

Adds "seconds" to the date/time represented in this instance. "seconds" may be negative.

◆ addYears()

bool datetime::addYears ( int32_t  years)

Adds "years" to the date/time represented in this instance. "years" may be negative.

◆ formatAs()

static char* datetime::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

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.

◆ getCentury()

int32_t datetime::getCentury ( )

Returns the (one-based) century component of the date/time currently represented in this instance.

◆ getDayOfMonth()

int32_t datetime::getDayOfMonth ( )

Returns the day-of-the-month component (1-31) of the date/time currently represented in this instance.

◆ getDayOfWeek()

int32_t datetime::getDayOfWeek ( )

Returns the day-of-the-week component (1-7) of the date/time currently represented in this instance.

◆ getDayOfYear()

int32_t datetime::getDayOfYear ( )

Returns the day-of-the-year component (1-365) of the date/time currently represented in this instance.

◆ getEpoch() [1/3]

time_t datetime::getEpoch ( )

Returns the number of seconds since 1970 (the epoch).

◆ getEpoch() [2/3]

static time_t datetime::getEpoch ( const char *  datestring)
static

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.

◆ getEpoch() [3/3]

static time_t datetime::getEpoch ( const void *  tmstruct)
static

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.

◆ getHour()

int32_t datetime::getHour ( )

Returns the hour component of the date/time currently represented in this instance.

◆ getMicrosecond()

int32_t datetime::getMicrosecond ( )

Returns the microsecond component of the date/time currently represented in this instance.

◆ getMinute()

int32_t datetime::getMinute ( )

Returns the minute component of the date/time currently represented in this instance.

◆ getMonth()

int32_t datetime::getMonth ( )

Returns the month component (1-12) of the date/time currently represented in this instance.

◆ getMonthAbbreviation()

const char* datetime::getMonthAbbreviation ( )

Returns the abbreviated name of the month component of the date/time currently represented in this instance.

◆ getMonthName()

const char* datetime::getMonthName ( )

Returns the full name of the month component of the date/time currently represented in this instance.

◆ getSecond()

int32_t datetime::getSecond ( )

Returns the second component of the date/time currently represented in this instance.

◆ getShortYear()

int32_t datetime::getShortYear ( )

Returns the year component, not including the century, of the date/time currently represented in this instance.

◆ getSqlString() [1/2]

const char* datetime::getSqlString ( )

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

◆ getSqlString() [2/2]

const char* datetime::getSqlString ( bool  microseconds)

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

◆ getString() [1/5]

const char* datetime::getString ( )

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

◆ getString() [2/5]

const char* datetime::getString ( bool  microseconds)

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

◆ getString() [3/5]

static char* datetime::getString ( const void *  tmstruct)
static

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.

◆ getString() [4/5]

static char* datetime::getString ( time_t  seconds)
static

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.

◆ getString() [5/5]

static char* datetime::getString ( time_t  seconds,
time_t  microseconds 
)
static

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.

◆ getTimeZoneAbbreviations()

static const char* const* datetime::getTimeZoneAbbreviations ( )
static

Returns a NULL terminated array of timezone abbreviations.

◆ getTimeZoneOffset()

int32_t datetime::getTimeZoneOffset ( )

Returns the offset from GMT in seconds

◆ getTimeZoneOffsets()

static const int32_t* datetime::getTimeZoneOffsets ( )
static

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

◆ getTimeZoneString()

const char* datetime::getTimeZoneString ( )

Returns a 3 character string representing the time zone

◆ getWeekOfYear()

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.

◆ getYear()

int32_t datetime::getYear ( )

Returns the year component, including the century, of the date/time currently represented in this instance.

◆ init() [1/4]

bool datetime::init ( const char *  tmstring)

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.

◆ init() [2/4]

bool datetime::init ( const void *  tmstruct)

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.

◆ init() [3/4]

bool datetime::init ( time_t  seconds)

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.

◆ init() [4/4]

bool datetime::init ( time_t  seconds,
time_t  microseconds 
)

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.

◆ initFromAdjustedHardwareDateTime()

bool datetime::initFromAdjustedHardwareDateTime ( const char *  hwtz)

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.

◆ initFromHardwareDateTime()

bool datetime::initFromHardwareDateTime ( const char *  hwtz)

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.

◆ initFromSystemDateTime()

bool datetime::initFromSystemDateTime ( )

Initialies this instance to the date/time stored in the system clock.

Returns true on success and false on failure.

◆ isDaylightSavingsTime()

bool datetime::isDaylightSavingsTime ( )

Returns true if daylight savings time is currently in effect and false if it isn't

◆ isValidDateTime()

static bool datetime::isValidDateTime ( const char *  string)
static

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.

◆ operator=()

datetime& datetime::operator= ( datetime d)

Makes this instance of the datetime class identical to "d".

◆ parse()

static bool datetime::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

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.

◆ setDayOfMonth()

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

◆ setHardwareDateTime()

bool datetime::setHardwareDateTime ( const char *  hwtz)

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.

◆ setHour()

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

◆ setMicrosecond()

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

◆ setMinute()

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

◆ setMonth()

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

◆ setMutex()

static void datetime::setMutex ( threadmutex mtx)
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.

◆ setSecond()

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

◆ setSystemDateTime()

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.

◆ setTimeZone() [1/2]

bool datetime::setTimeZone ( const char *  newtz)

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.

◆ setTimeZone() [2/2]

bool datetime::setTimeZone ( const char *  newtz,
bool  ignoredst 
)

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.

◆ setYear()

bool datetime::setYear ( int32_t  year)

Sets the year component of the date/time represented in this instance to "year".