MeVisLab Toolbox Reference
ml::DateTime Class Reference

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

#include <mlDateTime.h>

Public Types

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

Public Member Functions

Initialization
 DateTime ()=default
 
 DateTime (const DateTime &)=default
 
DateTimeoperator= (const DateTime &)=default
 
 DateTime (DateTime &&)=default
 
DateTimeoperator= (DateTime &&)=default
 
 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
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
 
bool isValid () const
 
Arithmetic
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
 

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)
 

Detailed Description

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

Thread-safety: This class is re-entrant.

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 can represent a pure date, pure time and date-time.

In case of a pure time representation, year, month and day return zero because of backward compatibility. The same is true for a pure date representation.

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 arithmetic, 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 Common Era. 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 and the value is not accepted. E.g. the date 2000-02-30 or the time stamp 12:61:00 are not accepted.

  • String representation: There are get() and set() methods for formatting the time as a string and for reading the time from a string. Only a complete date, a complete time or a complete date-time specification is accepted.
    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 141 of file mlDateTime.h.

Member Typedef Documentation

◆ DateTimeFloatType

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

Definition at line 149 of file mlDateTime.h.

◆ DateTimeIntType

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

Definition at line 147 of file mlDateTime.h.

Constructor & Destructor Documentation

◆ DateTime() [1/6]

ml::DateTime::DateTime ( )
default

◆ DateTime() [2/6]

ml::DateTime::DateTime ( const DateTime )
default

◆ DateTime() [3/6]

ml::DateTime::DateTime ( DateTime &&  )
default

◆ DateTime() [4/6]

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

◆ DateTime() [5/6]

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

◆ DateTime() [6/6]

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

Member Function Documentation

◆ addDays() [1/2]

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

◆ addDays() [2/2]

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

◆ addSeconds()

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

◆ day()

DateTimeIntType ml::DateTime::day ( ) const

◆ dayInYear()

DateTimeIntType ml::DateTime::dayInYear ( ) const

◆ daysOfMonth() [1/2]

DateTimeIntType ml::DateTime::daysOfMonth ( ) const

◆ daysOfMonth() [2/2]

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

◆ daysOfYear() [1/2]

DateTimeIntType ml::DateTime::daysOfYear ( ) const

◆ daysOfYear() [2/2]

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

◆ daysSince()

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

◆ get() [1/2]

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

◆ get() [2/2]

void ml::DateTime::get ( DateTimeIntType yearV,
DateTimeIntType monthV,
DateTimeIntType dayV,
DateTimeIntType hourV = nullptr,
DateTimeIntType minuteV = nullptr,
DateTimeFloatType secondV = 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]

bool ml::DateTime::isLeapYear ( ) const

◆ isLeapYear() [2/2]

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

◆ isValid()

bool ml::DateTime::isValid ( ) const

◆ linearDays()

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

Definition at line 221 of file mlDateTime.h.

◆ linearSeconds()

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

Definition at line 222 of file mlDateTime.h.

◆ minute()

DateTimeIntType ml::DateTime::minute ( ) const

◆ month()

DateTimeIntType ml::DateTime::month ( ) const

◆ now()

DateTime& ml::DateTime::now ( )

◆ 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=() [1/2]

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

◆ operator=() [2/2]

DateTime& ml::DateTime::operator= ( DateTime &&  )
default

◆ 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 ( const char *  dtString,
const char *  format = nullptr 
)

◆ set() [2/3]

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

◆ set() [3/3]

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

◆ 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: