lgraph_date_time
Implemnets the DateTime, Date and TimeZone classes.
-
namespace lgraph_api
Functions
-
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
-
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
ymd – Date 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.
-
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.
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.
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.
-
explicit Date(const std::chrono::system_clock::time_point &tp)
-
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
-
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
OutOfRangeError – Thrown if the time point is out of range.
InputError – Thrown if str has invalid format.
- 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.
-
int64_t MicroSecondsSinceEpoch() 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].
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.
Private Members
-
int64_t microseconds_since_epoch_
-
DateTime()
-
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
dt – DateTime 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
dt – DateTime in local timezone.
- Returns
DateTime in 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_
-
explicit TimeZone(int time_diff_hours = 0)
-
static inline constexpr int32_t MaxDaysSinceEpochForDate()