MeVisLabToolboxReference
ml::DateTime Class Reference

Class for performing date/time arithmetics, comparisons and formatting. More...

#include <mlDateTime.h>

Public Types

typedef MLint32 DateTimeIntType
 Specify data type for year, month, day, hour and minute type as MLint32. More...
 
typedef MLdouble DateTimeFloatType
 Specify data type for seconds and fractions of other time units. More...
 

Public Member Functions

Initialization
 DateTime ()
 
 DateTime (const DateTime &dt)
 
 DateTime (DateTimeIntType yearV, DateTimeIntType monthV, DateTimeIntType dayV, DateTimeIntType hourV=0, DateTimeIntType minuteV=0, DateTimeFloatType secondV=0)
 
 DateTime (const char *dtString, const char *format=nullptr)
 
 DateTime (DateTimeIntType linearDaysV, DateTimeFloatType linearSecondsV)
 
Set date and time
DateTimeoperator= (const DateTime &dt)
 
bool set (DateTimeIntType yearV, DateTimeIntType monthV, DateTimeIntType dayV, DateTimeIntType hourV=0, DateTimeIntType minuteV=0, DateTimeFloatType secondV=0)
 
bool set (const char *dtString, const char *format=nullptr)
 
bool set (DateTimeIntType linDaysV, DateTimeFloatType linSecondsV)
 
bool setYMD (DateTimeIntType yearV, DateTimeIntType monthV, DateTimeIntType dayV)
 
bool setHMS (DateTimeIntType hourV, DateTimeIntType minuteV, DateTimeFloatType secondV)
 
DateTimenow ()
 
Returns date and time
void get (DateTimeIntType *yearV, DateTimeIntType *monthV, DateTimeIntType *dayV, DateTimeIntType *hourV=nullptr, DateTimeIntType *minuteV=nullptr, DateTimeFloatType *secondV=nullptr) const
 
void get (char *dtString, const char *format=nullptr) const
 
void getYMD (DateTimeIntType *yearV, DateTimeIntType *monthV, DateTimeIntType *dayV) const
 
void getHMS (DateTimeIntType *hourV, DateTimeIntType *minuteV, DateTimeFloatType *secondV) const
 
DateTimeIntType year () const
 
DateTimeIntType month () const
 
DateTimeIntType day () const
 
DateTimeIntType hour () const
 
DateTimeIntType minute () const
 
DateTimeFloatType second () const
 
DateTimeIntType dayInYear () const
 
DateTimeIntType linearDays () const
 
DateTimeFloatType linearSeconds () const
 
 operator void * () const
 
bool operator! () const
 
Arithmetics
void addDays (DateTimeIntType daysV)
 
void addDays (DateTimeFloatType daysV)
 
void addSeconds (DateTimeFloatType secondsV)
 
DateTimeFloatType daysSince (const DateTime &dateTime) const
 
DateTimeFloatType secondsSince (const DateTime &dateTime) const
 
Comparison
bool operator== (const DateTime &dt) const
 
bool operator!= (const DateTime &dt) const
 
bool operator< (const DateTime &dt) const
 
bool operator<= (const DateTime &dt) const
 
bool operator>= (const DateTime &dt) const
 
bool operator> (const DateTime &dt) const
 

Protected Member Functions

Auxiliary conversion methods
void normalize ()
 

Special date methods

DateTimeIntType daysOfMonth () const
 
DateTimeIntType daysOfYear () const
 
bool isLeapYear () const
 
static bool validYMD (DateTimeIntType yearV, DateTimeIntType monthV, DateTimeIntType dayV)
 
static bool validHMS (DateTimeIntType hourV, DateTimeIntType minuteV, DateTimeFloatType secondV)
 
static DateTimeIntType daysOfMonth (DateTimeIntType monthV, DateTimeIntType yearV)
 
static DateTimeIntType daysOfYear (DateTimeIntType yearV)
 
static bool isLeapYear (DateTimeIntType yearV)
 

Deprecated

typedef MLint32 DTIType
 
typedef MLdouble DTFType
 
DateTimeIntType linDays () const
 
DateTimeFloatType linSeconds () const
 

Detailed Description

Class for performing date/time arithmetics, comparisons and formatting.

Thread-safety: This class is reentrant.

This class represents points in a time continuum by

  • a linear day counter, counting days passed since some fictitious "day zero", and
  • a linear seconds counter, counting seconds since 0:00 am.

The class provides member functions to convert between the internal representation and the usual one using year, month, day, hour, minute and second. There are also functions to compare two dates and to perform simple arithmetics, as adding/subtracting a number of days or seconds and computing the difference between two dates in either days or seconds.

Years, months, days, hours, minutes are represented by the type DateTime::DateTimeIntType which is a normal integer type.

Seconds and fractions of other time units are represented as DateTime::DateTimeFloatType, a normal floating point type.

The seconds counter is implemented as a double precision variable, hence it is able to measure fractions of a second with a precision in the nanosecond range.

The linear day counter counts days passed since the (non-existing) date 01-Jan-0000, assuming that the modern leap year rules would have been valid since the beginning of time. It is well known that this is not true, so computations involving dates before 1752 (the year the Gregorian calendar has been established) are

  • at best - inaccurate.

A year is considered a leap year if

  • it is a multiple of 400, or
  • it is a multiple of 4 but not of 100.

Leap seconds and time zones are not supported.

The class supports three different time and date representations:

  • Linear time: This is the internal representation using linear day and seconds counter as described above.
  • Calendar time: This is the usual representation using integer values for year, month, day, hour and minute and a double precision value for seconds. The valid ranges for these values are:

    1 <= month <= 12 1 <= day <= Number of days in month 0 <= hour <= 23 0 <= minute <= 59 0 <= second < 60

    The year number can be arbitrary, although numbers smaller than 1752 might make no sense (cf above). There is no implicit assumption on the century, i.e., the year 99 is NOT the year 1999, and 01 is NOT 2001.

    Specifications outside the valid range are signaled as invalid but are nevertheless converted into a valid internal representation by "extrapolation". For example, the dates 2000-02-30 and 2000-03-01 are considered the same, 12:61:00 is the same as 13:01:00 and 24:15:30 is really 0:15:30 the next day.

  • String representation: There are get() and set() methods for formatting the time as a string and for reading the time from a string. In both cases, the format of the string has to specified using the following field specifiers:

        %y     : Year, two digits (only get())
        %Y     : Year, four digits
        %m     : Month
        %d, %D : Day
        %h, %H : Hour
        %M     : Minute
        %s, %S : Second (as an integer)
        %t, %T : Milliseconds, three digits
        %r, %R : Microseconds, six digits (you can only use either
                 milliseconds or microseconds, not both)

    If no format is specified, the default format

        %Y-%m-%d %H:%M:%S

    is used.

    Note that because of how the format string works, if you call

        set("01.23", "%S.%T")

    you will get 1.023 seconds, because there were 23 milliseconds specified.

Definition at line 127 of file mlDateTime.h.

Member Typedef Documentation

◆ DateTimeFloatType

Specify data type for seconds and fractions of other time units.

Definition at line 135 of file mlDateTime.h.

◆ DateTimeIntType

Specify data type for year, month, day, hour and minute type as MLint32.

Definition at line 133 of file mlDateTime.h.

◆ DTFType

Deprecated:
Use DateTimeFloatType instead.

Definition at line 326 of file mlDateTime.h.

◆ DTIType

Deprecated:
Use DateTimeIntType instead.

Definition at line 323 of file mlDateTime.h.

Constructor & Destructor Documentation

◆ DateTime() [1/5]

ml::DateTime::DateTime ( )

◆ DateTime() [2/5]

ml::DateTime::DateTime ( const DateTime dt)

◆ DateTime() [3/5]

ml::DateTime::DateTime ( DateTimeIntType  yearV,
DateTimeIntType  monthV,
DateTimeIntType  dayV,
DateTimeIntType  hourV = 0,
DateTimeIntType  minuteV = 0,
DateTimeFloatType  secondV = 0 
)

◆ DateTime() [4/5]

ml::DateTime::DateTime ( const char *  dtString,
const char *  format = nullptr 
)

◆ DateTime() [5/5]

ml::DateTime::DateTime ( DateTimeIntType  linearDaysV,
DateTimeFloatType  linearSecondsV 
)

Member Function Documentation

◆ addDays() [1/2]

void ml::DateTime::addDays ( DateTimeIntType  daysV)

◆ addDays() [2/2]

void ml::DateTime::addDays ( DateTimeFloatType  daysV)

◆ addSeconds()

void ml::DateTime::addSeconds ( DateTimeFloatType  secondsV)

◆ day()

DateTimeIntType ml::DateTime::day ( ) const

◆ dayInYear()

DateTimeIntType ml::DateTime::dayInYear ( ) const

◆ daysOfMonth() [1/2]

static DateTimeIntType ml::DateTime::daysOfMonth ( DateTimeIntType  monthV,
DateTimeIntType  yearV 
)
static

◆ daysOfMonth() [2/2]

DateTimeIntType ml::DateTime::daysOfMonth ( ) const

◆ daysOfYear() [1/2]

static DateTimeIntType ml::DateTime::daysOfYear ( DateTimeIntType  yearV)
static

◆ daysOfYear() [2/2]

DateTimeIntType ml::DateTime::daysOfYear ( ) const

◆ daysSince()

DateTimeFloatType ml::DateTime::daysSince ( const DateTime dateTime) const

◆ get() [1/2]

void ml::DateTime::get ( DateTimeIntType yearV,
DateTimeIntType monthV,
DateTimeIntType dayV,
DateTimeIntType hourV = nullptr,
DateTimeIntType minuteV = nullptr,
DateTimeFloatType secondV = nullptr 
) const

◆ get() [2/2]

void ml::DateTime::get ( char *  dtString,
const char *  format = nullptr 
) const

◆ getHMS()

void ml::DateTime::getHMS ( DateTimeIntType hourV,
DateTimeIntType minuteV,
DateTimeFloatType secondV 
) const

◆ getYMD()

void ml::DateTime::getYMD ( DateTimeIntType yearV,
DateTimeIntType monthV,
DateTimeIntType dayV 
) const

◆ hour()

DateTimeIntType ml::DateTime::hour ( ) const

◆ isLeapYear() [1/2]

static bool ml::DateTime::isLeapYear ( DateTimeIntType  yearV)
static

◆ isLeapYear() [2/2]

bool ml::DateTime::isLeapYear ( ) const

◆ linDays()

DateTimeIntType ml::DateTime::linDays ( ) const
inline
Deprecated:
Use linearDays() instead.

Definition at line 329 of file mlDateTime.h.

◆ linearDays()

DateTimeIntType ml::DateTime::linearDays ( ) const
inline

Definition at line 205 of file mlDateTime.h.

◆ linearSeconds()

DateTimeFloatType ml::DateTime::linearSeconds ( ) const
inline

Definition at line 206 of file mlDateTime.h.

◆ linSeconds()

DateTimeFloatType ml::DateTime::linSeconds ( ) const
inline
Deprecated:
Use linearSeconds() instead.

Definition at line 332 of file mlDateTime.h.

◆ minute()

DateTimeIntType ml::DateTime::minute ( ) const

◆ month()

DateTimeIntType ml::DateTime::month ( ) const

◆ normalize()

void ml::DateTime::normalize ( )
protected

◆ now()

DateTime& ml::DateTime::now ( )

◆ operator void *()

ml::DateTime::operator void * ( ) const

◆ operator!()

bool ml::DateTime::operator! ( ) const

◆ operator!=()

bool ml::DateTime::operator!= ( const DateTime dt) const

◆ operator<()

bool ml::DateTime::operator< ( const DateTime dt) const

◆ operator<=()

bool ml::DateTime::operator<= ( const DateTime dt) const

◆ operator=()

DateTime& ml::DateTime::operator= ( const DateTime dt)

◆ operator==()

bool ml::DateTime::operator== ( const DateTime dt) const

◆ operator>()

bool ml::DateTime::operator> ( const DateTime dt) const

◆ operator>=()

bool ml::DateTime::operator>= ( const DateTime dt) const

◆ second()

DateTimeFloatType ml::DateTime::second ( ) const

◆ secondsSince()

DateTimeFloatType ml::DateTime::secondsSince ( const DateTime dateTime) const

◆ set() [1/3]

bool ml::DateTime::set ( DateTimeIntType  yearV,
DateTimeIntType  monthV,
DateTimeIntType  dayV,
DateTimeIntType  hourV = 0,
DateTimeIntType  minuteV = 0,
DateTimeFloatType  secondV = 0 
)

◆ set() [2/3]

bool ml::DateTime::set ( const char *  dtString,
const char *  format = nullptr 
)

◆ set() [3/3]

bool ml::DateTime::set ( DateTimeIntType  linDaysV,
DateTimeFloatType  linSecondsV 
)

◆ setHMS()

bool ml::DateTime::setHMS ( DateTimeIntType  hourV,
DateTimeIntType  minuteV,
DateTimeFloatType  secondV 
)

◆ setYMD()

bool ml::DateTime::setYMD ( DateTimeIntType  yearV,
DateTimeIntType  monthV,
DateTimeIntType  dayV 
)

◆ validHMS()

static bool ml::DateTime::validHMS ( DateTimeIntType  hourV,
DateTimeIntType  minuteV,
DateTimeFloatType  secondV 
)
static

◆ validYMD()

static bool ml::DateTime::validYMD ( DateTimeIntType  yearV,
DateTimeIntType  monthV,
DateTimeIntType  dayV 
)
static

◆ year()

DateTimeIntType ml::DateTime::year ( ) const

The documentation for this class was generated from the following file: