LocalDateTime Class Reference

#include <datetime.h>

Detailed Description

At some point developers have to deal with times, dates and timezones. This topic can promote headaches because it can be quite complex. In general you have to be aware of differences in timezones, especially when your software runs on mobile devices are you let the user share data across countries (or even within the same country).

In this header file you find some convenient helper functions and classes to reduce the amount of effort you have to put into to handle this topic to a minimum. The implementation and definition of the classes rely on the ISO 8601. Use the UniversalDateTime whenever possible to store normalized time and date values and convert them to a LocalDateTime instance as late as possible (e.g. for a local string output). An object representing a local 'time and date' of the current system time. All members are public and are allowed to be modified for custom needs. Keep in mind that you have to ensure that the values are correct and valid when converting an object back to UniversalDateTime, otherwise the behavior is undefined.

Public Member Functions

Bool IsLeapYear () const
 
DAYOFWEEK GetDayOfWeek () const
 
UniversalDateTime ConvertToUniversalDateTime () const
 
Bool operator== (const LocalDateTime &x) const
 
Bool operator!= (const LocalDateTime &x) const
 
void Reset ()
 
String FormatTime (const Char *formatString) const
 
String ToString (const FormatStatement *formatStatement=nullptr) const
 
HashInt GetHashCode () const
 

Static Public Member Functions

static TimeValue GetTimezoneOffset (Bool *daylightSavingTime=nullptr)
 
static Bool IsLeapYear (Int year)
 
static DAYOFWEEK GetDayOfWeek (Int lYear, Int lMonth, Int lDay)
 
static LocalDateTime GetNow ()
 
static Result< LocalDateTimeFromString (const String &dateTimeStr, const Char *formatString)
 
static Result< LocalDateTimeFromValues (Int32 year, UChar month, UChar day, UChar hour, UChar minute, UChar second, DST daylightSavingTime=DST::AUTOMATIC)
 
static LocalDateTime FromUnixTimestamp (UInt64 timestamp)
 

Public Attributes

Int32 _year
 
UChar _month
 
UChar _day
 
UChar _hour
 
UChar _minute
 
UChar _second
 
DST _daylightSavingTime
 

Member Function Documentation

◆ IsLeapYear() [1/2]

Bool IsLeapYear ( ) const

Check if the set year is a leap year.

Returns
True for a leap year, otherwise false.

◆ GetDayOfWeek() [1/2]

DAYOFWEEK GetDayOfWeek ( ) const

Return the day of the set time.

Returns
See DAYOFWEEK

◆ ConvertToUniversalDateTime()

UniversalDateTime ConvertToUniversalDateTime ( ) const

Converts the local time to the universal date-time

Returns
The normalized time.

◆ operator==()

Bool operator== ( const LocalDateTime x) const

Compare operator ==. The objects must represent a date and time in the same timezone. The Daylight Saving Time is not compared.

Returns
True if both UniversalDateTime are identical.

◆ operator!=()

Bool operator!= ( const LocalDateTime x) const

◆ Reset()

void Reset ( )

Resets the date-time object and sets all members to 0.

◆ FormatTime()

String FormatTime ( const Char formatString) const

Formats the time into a specific format defined be the format string.

Parameters
[in]formatStringOptional. OS depending C lib format string (see strftime). "%Y-%m-%d %H:%M:%S" is used if formatString is nullptr.
Returns
Formated String of the date-time.

◆ ToString()

String ToString ( const FormatStatement formatStatement = nullptr) const

◆ GetHashCode()

HashInt GetHashCode ( ) const

Returns the hash value of this object. This value can be used for all HashSet<>/HashMap<> classes.

◆ GetTimezoneOffset()

static TimeValue GetTimezoneOffset ( Bool daylightSavingTime = nullptr)
static

Return the current timezone offset to the universal time coordinate (UTC).

Parameters
[out]daylightSavingTimeTrue if Daylight Saving Time effects the local time.
Returns
The timezone offset.

◆ IsLeapYear() [2/2]

static Bool IsLeapYear ( Int  year)
static

Check if the passed year is a leap year.

Parameters
[in]yearYear value.
Returns
True for a leap year, otherwise false.

◆ GetDayOfWeek() [2/2]

static DAYOFWEEK GetDayOfWeek ( Int  lYear,
Int  lMonth,
Int  lDay 
)
static

Return the day of the passed date.

Parameters
[in]lYearYear value.
[in]lMonthMonth value.
[in]lDayDay value.
Returns
See DAYOFWEEK

◆ GetNow()

static LocalDateTime GetNow ( )
static

Return the current date-time object of the current time zone.

Returns
See LocalDateTime

◆ FromString()

static Result<LocalDateTime> FromString ( const String dateTimeStr,
const Char formatString 
)
static

Creates a date-time object by the passed string and its passed format string. An example:

LocalDateTime dt = LocalDateTime::FromString(str, "%a, %d %b %Y %H:%M:%S") iferr_return;
static Result< LocalDateTime > FromString(const String &dateTimeStr, const Char *formatString)
void * str
Definition: bytesobject.h:77
#define iferr_scope
Definition: resultbase.h:1389
#define iferr_return
Definition: resultbase.h:1524
Parameters
[in]dateTimeStrThe time string.
[in]formatStringThe matching format string.
Returns
The date-time object or an error if the passed date-time string cannot be resolved by the format string.

◆ FromValues()

static Result<LocalDateTime> FromValues ( Int32  year,
UChar  month,
UChar  day,
UChar  hour,
UChar  minute,
UChar  second,
DST  daylightSavingTime = DST::AUTOMATIC 
)
static

Creates a local date-time object by the passed date values.

Warning
The passed values are not checked for their validity!
Parameters
[in]yearYear value [e.g. 2015]
[in]monthMonth value value [1-12]
[in]dayDay value [1-31]
[in]hourHour value [0-23]
[in]minuteMinute value [0-59]
[in]secondSecond value [0-59]
[in]daylightSavingTimeOptional Daylight Saving Time information. See DST.
Returns
date-time object.

◆ FromUnixTimestamp()

static LocalDateTime FromUnixTimestamp ( UInt64  timestamp)
static

Create a local date-time object by passing a Unix time stamp.

Parameters
[in]timestampThe local Unix time stamp where 0 represents the 01/01/1970-00:00 in the current timezone.
Returns
The local date-time object.

Member Data Documentation

◆ _year

Int32 _year

Full year, e.g. 2014.

◆ _month

UChar _month

1..12

◆ _day

UChar _day

1..31

◆ _hour

UChar _hour

0..23

◆ _minute

UChar _minute

0..59

◆ _second

UChar _second

0..59

◆ _daylightSavingTime

DST _daylightSavingTime