lgraph_date_time

Implemnets the DateTime, Date and TimeZone classes.

namespace lgraph_api

Functions

static inline constexpr int32_t MinDaysSinceEpochForDate()

min and max values that Date can hold

static inline constexpr int32_t MaxDaysSinceEpochForDate()

Maximum days since epoch for date.

static inline constexpr int64_t MinMicroSecondsSinceEpochForDateTime()

min and max values that Date can hold

static inline constexpr int64_t MaxMicroSecondsSinceEpochForDateTime()

Maximum microseconds since epoch for date time.

class Date
#include <lgraph_date_time.h>

Implements the Date class. Range of dates is from 0/1/1 to 12/31/9999.

Public Functions

Date()

Construct a new Date object with the date set to 1970/1/1.

explicit Date(const std::chrono::system_clock::time_point &tp)

Construct a new Date object with date set to the specified time. The time point must be in the range of 0/1/1 to 12/31/9999.

Throws

OutOfRangeError – Thrown if the time point is out of range.

Parameters

tp – Time point to set the date to.

explicit Date(const YearMonthDay &ymd)

Construct a new Date object with date set to the specified date. The date must be in the range of 0/1/1 to 12/31/9999.

Throws

OutOfRangeError – Thrown if the time point is out of range.

Parameters

ymdDate in the form of year, month and day.

explicit Date(int32_t days_since_epoch)

Construct a new Date object with date set to an offset from epoch, i.e. the date is set to the specified number of days from epoch. The result date must be in the range of 0/1/1 to 12/31/9999.

Throws

OutOfRangeError – Thrown if the time point is out of range.

Parameters

days_since_epoch – Number of days since epoch.

explicit Date(const std::string &str)

Parse date from a YYYY-MM-DD string.

Throws

InputError – if the string is not in the correct format.

Parameters

str – The string.

YearMonthDay GetYearMonthDay() const noexcept

Returns the current Date in the form of year, month and day.

Returns

The year month day.

Date operator+(int days) const

Add a number of days to the Date object.

Throws

OutOfRangeError – Thrown if the time point is out of range.

Parameters

days – Number of days to add.

Returns

The resulting Date object.

Date &operator+=(int days)

Add a number of days to the current Date object. In case of overflow, current object is not modified.

Throws

OutOfRangeError – Thrown if the resulting date is out of range.

Parameters

days – Number of days to add.

Returns

Reference to the current date.

Date operator-(int days) const

Subtract a number of days from the Date object.

Throws

OutOfRangeError – Thrown if the resulting date is out of range.

Parameters

days – Number of days to subtract.

Returns

The resulting Date object.

Date &operator-=(int days)

Subtract a number of days from the current Date object. In case of overflow, current object is not modified.

Throws

OutOfRangeError – Thrown if the resulting date is out of range.

Parameters

days – Number of days to subtract.

Returns

Reference to the current Date object.

bool operator<(const Date &rhs) const noexcept
bool operator<=(const Date &rhs) const noexcept
bool operator>(const Date &rhs) const noexcept
bool operator>=(const Date &rhs) const noexcept
bool operator==(const Date &rhs) const noexcept
bool operator!=(const Date &rhs) const noexcept
int32_t DaysSinceEpoch() const noexcept

Returns the number of days this date is since epoch.

int32_t GetStorage() const noexcept

Returns the number of days this date is since epoch.

explicit operator int32_t() const noexcept

Returns the number of days this date is since epoch.

std::chrono::system_clock::time_point TimePoint() const noexcept

Returns the timepoint corresponding to this date at 00:00 am.

std::string ToString() const noexcept

Get the string representation of the date in the format of YYYY-MM-DD.

explicit operator DateTime() const noexcept

Get the DateTime object corresponding to 00:00 am on this date.

Public Static Functions

static bool Parse(const std::string &str, Date &d) noexcept

Parse date from YYYY-MM-DD, save value in d.

Parameters
  • str – The string.

  • d[out] The resulting Date.

Returns

True if success, otherwise false.

static size_t Parse(const char *beg, const char *end, Date &d) noexcept

Parse date from YYYY-MM-DD, save value in d.

Parameters
  • beg – The beg.

  • end – The end.

  • d[out] The result.

Returns

Number of bytes parsed (must be 10), 0 if failed.

static Date Now() noexcept

Returns the current Date.

Returns

Current Date in UTC.

static Date LocalNow() noexcept

Returns the current Date in local timezone.

Returns

Current Date in local timezone.

Private Members

int32_t days_since_epoch_

The days since epoch.

struct YearMonthDay
#include <lgraph_date_time.h>

Structure representing a date in the format of year, month and day.

Public Members

int year

The year, 0-9999.

unsigned month

Month, 1-12.

unsigned day

Day, 1-31.

class DateTime
#include <lgraph_date_time.h>

Implements a DateTime class that holds DateTime in the range of 0000-01-01 00:00:00.000000 to 9999-12-31 23:59:59.999999.

Public Functions

DateTime()

Construct a new DateTime object with date set to the epoch time, i.e., 1970-1-1 00:00:00.

explicit DateTime(const std::chrono::system_clock::time_point &tp)

Construct a new DateTime object with date set to the specified timepoint.

Throws

OutOfRangeError – Thrown if the time point is out of range.

Parameters

tp – Timepoint to set the DateTime to.

explicit DateTime(const YMDHMSF &ymdhmsf)

Construct a new DateTime object with date set to the specified date and time given in YMDHMSF.

Throws

OutOfRangeError – Thrown if the time point is out of range.

Parameters

ymdhmsfDate and time to set the DateTime to.

explicit DateTime(int64_t microseconds_since_epoch)

Construct a new DateTime object with date set to specified number of microseconds since epoch.

Throws

OutOfRangeError – Thrown if the time point is out of range.

Parameters

microseconds_since_epoch – Number of microseconds since epoch.

explicit DateTime(const std::string &str)

Construct a new DateTime object with date set to the specified date and time given in the form of YYYY-MM-DD HH:MM:SS[.FFFFFF].

Throws
Parameters

str – String representation of the date and time in the form of YYYY-MM-DD HH:MM:SS.

YMDHMSF GetYMDHMSF() const noexcept

Get current DateTime in the form of year, month, day, hour, minute, second, fraction.

Returns

The ymdhmsf.

DateTime operator+(int64_t n_microseconds) const

Add a number of microseconds to the DateTime.

Throws

OutOfRangeError – Thrown if the resulting DateTime is out of range.

Parameters

n_microseconds – Number of micorseconds to add.

Returns

The resulting DateTime.

DateTime &operator+=(int64_t n_microseconds)

Adds a number of microseconds to the current DateTime object. In case of overflow, the current DateTime object is not modified.

Throws

OutOfRangeError – Thrown if the resulting DateTime is out of range.

Parameters

n_microseconds – Number of microseconds to add.

Returns

A reference to current object.

DateTime operator-(int64_t n_microseconds) const

Subtract a number of microseconds from the DateTime.

Throws

OutOfRangeError – Thrown if the resulting DateTime is out of range.

Parameters

n_microseconds – Number of microseconds to subtract.

Returns

The resulting DateTime.

DateTime &operator-=(int64_t n_microseconds)

Subtract a number of microseconds from the current DateTime object. In case of overflow, the current DateTime object is not modified.

Throws

OutOfRangeError – Thrown if the resulting DateTime is out of range.

Parameters

n_microseconds – Number of microseconds to subtract.

Returns

A reference to current object.

bool operator<(const DateTime &rhs) const noexcept
bool operator<=(const DateTime &rhs) const noexcept
bool operator>(const DateTime &rhs) const noexcept
bool operator>=(const DateTime &rhs) const noexcept
bool operator==(const DateTime &rhs) const noexcept
bool operator!=(const DateTime &rhs) const noexcept
int64_t MicroSecondsSinceEpoch() const noexcept

Get the number of microseconds this DateTime is since epoch.

int64_t GetStorage() const noexcept

Get the number of microseconds this DateTime is since epoch.

explicit operator int64_t() const noexcept

Get the number of microseconds this DateTime is since epoch.

std::chrono::system_clock::time_point TimePoint() const noexcept

Get the timepoint correponding to this DateTime.

std::string ToString() const noexcept

Get string representation of the date and time in the form of YYYY-MM-DD HH:MM:SS.[ffffff].

explicit operator Date() const noexcept

Get the Date object corresponding to this DateTime.

DateTime ConvertToUTC()

Get the UTC time corresponding to this DateTime, assuming current DateTime is a local time.

Throws

OutOfRangeError – Thrown if the resulting DateTime is out of range.

Returns

Object converted to an UTC.

DateTime ConvertToLocal()

Get the local time corresponding to this DateTime, assuming current DateTime is a UTC time.

Throws

OutOfRangeError – Thrown if the resulting DateTime is out of range.

Returns

Object converted to a local.

Public Static Functions

static bool Parse(const std::string &str, DateTime &d) noexcept

Parse date from YYYY-MM-DD HH:MM:SS(.FFFFFF), save value in d.

Parameters
  • str – The string.

  • d[out] A DateTime to process.

Returns

True if success, otherwise false.

static size_t Parse(const char *beg, const char *end, DateTime &d) noexcept

Parse date from YYYY-MM-DD HH:MM:SS(.FFFFFF), save value in d.

Parameters
  • beg – The beg.

  • end – The end.

  • d[inout] A DateTime to process.

Returns

Number of bytes parsed (must be 19 or 26), 0 if failed.

static DateTime Now() noexcept

Get current DateTime in UTC.

Returns

A DateTime.

static DateTime LocalNow() noexcept

Get current DateTime in local timezone.

Returns

A DateTime.

Private Members

int64_t microseconds_since_epoch_
struct YMDHMSF
#include <lgraph_date_time.h>

Representation of a DateTime in year, month, day, hour, minute, second, fraction.

Public Members

int year

The year, 0-9999.

unsigned month

The month, 1-12.

unsigned day

The day, 1-31.

unsigned hour

The hour, 0-23.

unsigned minute

The minute, 0-59.

unsigned second

The second, 0-59.

unsigned fraction

The fraction 000000-999999.

class TimeZone
#include <lgraph_date_time.h>

A class that represents a time zone.

Public Functions

explicit TimeZone(int time_diff_hours = 0)

Create a timezone which has time difference with UTC in hours time_diff_hours.

Throws

InvalidParameterError – Thrown if time_diff_hours is invalid.

Parameters

time_diff_hours – (Optional) Difference between local timezone and UTC. Must be > =-10 && <=14. Otherwise the function will throw.

DateTime FromUTC(const DateTime &dt) const

Convert a DateTime from UTC time to this time zone.

Throws

OutOfRangeError – Thrown if the resulting value is out of range.

Parameters

dtDateTime in UTC time.

Returns

DateTime in local timezone.

DateTime ToUTC(const DateTime &dt) const

Convert a DateTime from this timezone to UTC time.

Throws

OutOfRangeError – Thrown if the resulting value is out of range.

Parameters

dtDateTime in local timezone.

Returns

DateTime in UTC.

int64_t UTCDiffSeconds() const noexcept

Returns diff from UTC in seconds, this is used in Date and DateTime.

Returns

Number of seconds local timezone is from UTC.

int64_t UTCDiffHours() const noexcept

Returns diff from UTC in hours, this is used in Date and DateTime.

Returns

Number of hours local timezone is from UTC.

Public Static Functions

static const TimeZone &LocalTimeZone() noexcept

Get local timezone.

Returns

A const reference to local timezone.

static void UpdateLocalTimeZone() noexcept

Update local timezone, used only when daylight saving time changes. Daylight saving time may change after LocalTZ was initialized, in which case we need to update it. This function will update all references returned by LocalTimeZone().

Private Members

int64_t time_diff_microseconds_

Private Static Functions

static TimeZone GetLocalTZ()
static TimeZone &LocalTZ()