Base Framework
Classes | Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
Date Class Reference

Date and time. More...

#include <base/Date.h>

Classes

class  DateTime
 

Public Types

enum  Constants {
  SUNDAY = 0, MONDAY = 1, TUESDAY = 2, WEDNESDAY = 3,
  THURSDAY = 4, FRIDAY = 5, SATURDAY = 6, JANUARY = 0,
  FEBRUARY = 1, MARCH = 2, APRIL = 3, MAY = 4,
  JUNE = 5, JULY = 6, AUGUST = 7, SEPTEMBER = 8,
  OCTOBER = 9, NOVEMBER = 10, DECEMBER = 11, EPOCH_YEAR = 1970,
  EPOCH_WEEKDAY = THURSDAY, DAYS_PER_NONLEAP_YEAR = 365, DAYS_PER_LEAP_YEAR = DAYS_PER_NONLEAP_YEAR + 1, DAYS_PER_400_YEARS = (300+4-1)*365 + (100-4+1)*366,
  DAYS_PER_WEEK = 7, MONTHS_PER_YEAR = 12, HOURS_PER_DAY = 24, MINUTES_PER_HOUR = 60,
  MINUTES_PER_DAY = MINUTES_PER_HOUR * HOURS_PER_DAY, SECONDS_PER_MINUTE = 60, SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR, SECONDS_PER_DAY = SECONDS_PER_HOUR * HOURS_PER_DAY
}
 

Public Member Functions

 Date () noexcept
 
 Date (int64 _date) noexcept
 
 Date (const Date &copy) noexcept
 
Dateoperator= (const Date &assign) noexcept
 
int64 getValue () const noexcept
 
 operator int64 () const noexcept
 
 operator bool () const noexcept
 
Date getLocalTime () const noexcept
 
Date getUTCTime () const noexcept
 
void addBias (int64 bias) noexcept
 
int getMillisecond () const noexcept
 
int getSecond () const noexcept
 
int getMinute () const noexcept
 
int getHour () const noexcept
 
int getDay () const noexcept
 
int getDayOfWeek () const noexcept
 
int getDayOfYear () const noexcept
 
int getMonth () const noexcept
 
int getYear () const noexcept
 
int getJulianDay () const noexcept
 
DateTime split (bool local=false) const noexcept
 
String getISO8601 (int offset=0) const
 
String getISO8601Compact (int offset=0) const
 
String getISO8601_MS () const
 
String getISO8601_US () const
 
String format (const String &format) const
 
WideString format (const WideString &format) const
 

Static Public Member Functions

static int getWeek (const DateTime &dt) noexcept
 
static int normalize (DateTime &dateTime) noexcept
 
static bool isLeapYear (int year) noexcept
 
static int getDaysOfYear (int year) noexcept
 
static int getDaysOfMonth (int month, int year)
 
static Date getNow ()
 
static int64 getBias ()
 
static Date getTime (int second, int minute, int hour)
 
static Date getDate (int day, int month, int year)
 
static Date getDate (int second, int minute, int hour, int day, int month, int year)
 
static Date getDateByJulianDay (int day) noexcept
 
static bool isValidDateTime (const DateTime &dt)
 
static Date makeDate (const DateTime &dateTime, bool local=false)
 
static Date parseISO8601 (const String &text, bool subsecond=false)
 

Static Public Attributes

static const int DAYS_PER_MONTH_NONLEAP_YEAR [MONTHS_PER_YEAR]
 
static const int DAYS_PER_MONTH_LEAP_YEAR [MONTHS_PER_YEAR]
 
static const int DAYS_BEFORE_FIRST_OF_MONTH_NONLEAP_YEAR [MONTHS_PER_YEAR+1]
 
static const int DAYS_BEFORE_FIRST_OF_MONTH_LEAP_YEAR [MONTHS_PER_YEAR+1]
 

Detailed Description

Date and time.

Representation of date/time in Coordinated Universal Time (UTC). It is recommended that you keep the date/time in UTC and only convert to local time when needed.

Version
1.3
Examples
testsuite/ls.cpp.

Member Enumeration Documentation

◆ Constants

Enumerator
SUNDAY 

Index of Sunday.

MONDAY 

Index of Monday.

TUESDAY 

Index of Tuesday.

WEDNESDAY 

Index of Wednesday.

THURSDAY 

Index of Thursday.

FRIDAY 

Index of Friday.

SATURDAY 

Index of Saturday.

JANUARY 

Normalized index for January.

FEBRUARY 

Normalized index for February.

MARCH 

Normalized index for March.

APRIL 

Normalized index for April.

MAY 

Normalized index for May.

JUNE 

Normalized index for June.

JULY 

Normalized index for July.

AUGUST 

Normalized index for August.

SEPTEMBER 

Normalized index for September.

OCTOBER 

Normalized index for October.

NOVEMBER 

Normalized index for November.

DECEMBER 

Normalized index for December.

EPOCH_YEAR 

The year of the epoch.

EPOCH_WEEKDAY 

The weekday of the first epoch day.

DAYS_PER_NONLEAP_YEAR 

The number of days per non-leap year.

DAYS_PER_LEAP_YEAR 

The number of days per leap year.

DAYS_PER_400_YEARS 

The number of days per 400 years.

DAYS_PER_WEEK 

The number of days per week.

MONTHS_PER_YEAR 

The number of months per year.

HOURS_PER_DAY 

The number of hours per day.

MINUTES_PER_HOUR 

The number of minutes per hour.

MINUTES_PER_DAY 

The number of minutes per day.

SECONDS_PER_MINUTE 

The number of seconds per minute.

SECONDS_PER_HOUR 

The number of seconds per hour.

SECONDS_PER_DAY 

The number of seconds per day.

Constructor & Destructor Documentation

◆ Date() [1/3]

Date::Date ( )
inlinenoexcept

Initializes the date as 00:00:00 on January 1, 1970.

◆ Date() [2/3]

Date::Date ( int64  _date)
inlinenoexcept

Initializes the date with the specified date.

Parameters
dateThe number of microseconds elapsed since 00:00:00 on January 1, 1970, Coordinated Universal Time (UTC).

◆ Date() [3/3]

Date::Date ( const Date copy)
inlinenoexcept

Initialize date from other date.

Member Function Documentation

◆ addBias()

void Date::addBias ( int64  bias)
inlinenoexcept

Adds a bias (in microseconds) to the date.

◆ format() [1/2]

String Date::format ( const String format) const

Returns the date/time as a string.

Parameters
formatThe desired format of the resulting string.
Examples
testsuite/ls.cpp.

◆ format() [2/2]

WideString Date::format ( const WideString format) const

Returns the date/time as a string.

Parameters
formatThe desired format of the resulting string.

◆ getBias()

static int64 Date::getBias ( )
static

Returns the bias (in microseconds) for convertion from UTC to local time. Bias may change during the lifetime of the process. This method only works on some planet called Earth :-]. UTC = local time + bias.

◆ getDate() [1/2]

static Date Date::getDate ( int  day,
int  month,
int  year 
)
static

Returns date object for the specified date. Invalid values will be normalized. Raises 'DateException' if the date cannot be represented.

Parameters
dayThe day of the month.
monthThe month of the year.
yearThe year.

◆ getDate() [2/2]

static Date Date::getDate ( int  second,
int  minute,
int  hour,
int  day,
int  month,
int  year 
)
static

Returns date object for the specified date and time. Invalid values will be normalized. Raises 'DateException' if the date cannot be represented.

Parameters
secondThe second.
minuteThe minute.
hourThe hour.
dayThe day of the month.
monthThe month of the year.
yearThe year.

◆ getDateByJulianDay()

static Date Date::getDateByJulianDay ( int  day)
staticnoexcept

Returns the date corresponding to the specified Julian day.

◆ getDay()

int Date::getDay ( ) const
noexcept

Returns the day of the month in local time. [1-31].

◆ getDayOfWeek()

int Date::getDayOfWeek ( ) const
noexcept

Returns the day of the week in local time. Week starts at Sunday (0).

◆ getDayOfYear()

int Date::getDayOfYear ( ) const
noexcept

Returns the day of the year in local time.

◆ getDaysOfMonth()

static int Date::getDaysOfMonth ( int  month,
int  year 
)
static

Returns the number of days in the specified month.

◆ getDaysOfYear()

static int Date::getDaysOfYear ( int  year)
inlinestaticnoexcept

Returns the number of days in the specified year.

◆ getHour()

int Date::getHour ( ) const
noexcept

Returns the hour in local time.

◆ getISO8601()

String Date::getISO8601 ( int  offset = 0) const

Returns date/time in ISO 8601 format. E.g. 2020-01-31T18:00:00Z. Offset is in minutes. Date should be in UTC when using offset.

◆ getISO8601_MS()

String Date::getISO8601_MS ( ) const

Returns date/time in ISO 8601 format with milliseconds. E.g. 2020-01-31T18:00:00.123Z.

◆ getISO8601_US()

String Date::getISO8601_US ( ) const

Returns date/time in ISO 8601 format with microseconds. E.g. 2020-01-31T18:00:00.123456Z.

◆ getISO8601Compact()

String Date::getISO8601Compact ( int  offset = 0) const

Returns date/time in ISO 8601 format. E.g. 20200131T180000Z. Offset is in minutes. Date should be in UTC when using offset.

◆ getJulianDay()

int Date::getJulianDay ( ) const
noexcept

Returns the Julian day.

◆ getLocalTime()

Date Date::getLocalTime ( ) const
inlinenoexcept

Converts from UTC to local time.

Examples
testsuite/ls.cpp.

◆ getMillisecond()

int Date::getMillisecond ( ) const
noexcept

Returns the millisecond in local time.

◆ getMinute()

int Date::getMinute ( ) const
noexcept

Returns the minute in local time.

◆ getMonth()

int Date::getMonth ( ) const
noexcept

Returns the month in local time. 0-11.

◆ getNow()

static Date Date::getNow ( )
static

Returns the current time in UTC time.

Examples
testsuite/ls.cpp.

◆ getSecond()

int Date::getSecond ( ) const
noexcept

Returns the second in local time.

◆ getTime()

static Date Date::getTime ( int  second,
int  minute,
int  hour 
)
static

Returns date object for the specified time. Invalid values will be normalized. Raises 'DateException' if the date cannot be represented. The used date is EPOCH.

Parameters
secondThe second.
minuteThe minute.
hourThe hour.

◆ getUTCTime()

Date Date::getUTCTime ( ) const
inlinenoexcept

Converts from local to UTC time.

◆ getValue()

int64 Date::getValue ( ) const
inlinenoexcept

Returns the number of microseconds elapsed since 00:00:00 on January 1, 1970, Coordinated Universal Time (UTC).

◆ getWeek()

static int Date::getWeek ( const DateTime dt)
staticnoexcept

Returns the week of the year.

◆ getYear()

int Date::getYear ( ) const
noexcept

Returns the year in local time.

Examples
testsuite/ls.cpp.

◆ isLeapYear()

static bool Date::isLeapYear ( int  year)
inlinestaticnoexcept

Returns true if the year is a leap year.

◆ isValidDateTime()

static bool Date::isValidDateTime ( const DateTime dt)
static

Returns true if the date/time is within the valid range.

◆ makeDate()

static Date Date::makeDate ( const DateTime dateTime,
bool  local = false 
)
static

Returns the date/time for the given date/time components.

Parameters
dateTimeThe broken out date and time.
localSpecifies that datetime is in local time. UTC by default.

◆ normalize()

static int Date::normalize ( DateTime dateTime)
staticnoexcept

Normalizes the specified data/time structure such that the fields are brought into their normal operating range.

Parameters
dateTimeThe structure to normalize.
Returns
The year carrier. Only non-zero if the year falls outside the range [-9999;9999].

◆ operator bool()

Date::operator bool ( ) const
inlinenoexcept

Returns true if date is not epoch.

◆ operator int64()

Date::operator int64 ( ) const
inlinenoexcept

Returns the number of microseconds elapsed since 00:00:00 on January 1, 1970, Coordinated Universal Time (UTC).

◆ operator=()

Date& Date::operator= ( const Date assign)
inlinenoexcept

Assignment of date with date.

◆ parseISO8601()

static Date Date::parseISO8601 ( const String text,
bool  subsecond = false 
)
static

Returns date/time in ISO 8601 format. E.g. 2020-01-31T18:00:00Z.

◆ split()

DateTime Date::split ( bool  local = false) const
noexcept

Fills the given time structure with the year, month, day of month, day of week, hour, minute, second, and millisecond (if supported).

Parameters
timeThe time structure.
localSpecifies that time should be converted to local time from UTC.

Member Data Documentation

◆ DAYS_BEFORE_FIRST_OF_MONTH_LEAP_YEAR

const int Date::DAYS_BEFORE_FIRST_OF_MONTH_LEAP_YEAR[MONTHS_PER_YEAR+1]
static

The days before the first of the month for leap years.

◆ DAYS_BEFORE_FIRST_OF_MONTH_NONLEAP_YEAR

const int Date::DAYS_BEFORE_FIRST_OF_MONTH_NONLEAP_YEAR[MONTHS_PER_YEAR+1]
static

The days before the first of the month for non-leap years.

◆ DAYS_PER_MONTH_LEAP_YEAR

const int Date::DAYS_PER_MONTH_LEAP_YEAR[MONTHS_PER_YEAR]
static

The days per month for leap years.

◆ DAYS_PER_MONTH_NONLEAP_YEAR

const int Date::DAYS_PER_MONTH_NONLEAP_YEAR[MONTHS_PER_YEAR]
static

The days per month for non-leap years.