Date and time utilities.
Classes | |
class | Date |
Date in format YYYY-MM-DD, std::chrono::year_month_day like type. More... | |
class | DateParseError |
Date/time parsing error. More... | |
class | SteadyClock |
struct | SteadyCoarseClock |
Steady clock with up to a few millisecond resolution that is slightly faster than the std::chrono::steady_clock. More... | |
class | TimeOfDay |
A simple implementation of a "time since midnight" datatype. More... | |
class | TimeOfDay< std::chrono::duration< Rep, Period > > |
class | TimePointTz |
Timepoint with timezone parsed in kRfc3339Format. More... | |
class | TimePointTzBase |
class | TimePointTzIsoBasic |
Timepoint with timezone parsed in kDefaultFormat. More... | |
class | TimezoneLookupError |
Timezone information lookup error. More... | |
struct | WallCoarseClock |
System clock with up to a few millisecond resolution that is slightly faster than the std::chrono::system_clock. More... | |
Typedefs | |
using | timepair_t = std::pair<std::uint8_t, std::uint8_t> |
Functions | |
std::chrono::system_clock::time_point | Now () noexcept |
std::chrono::system_clock::now() that could be mocked | |
std::chrono::system_clock::time_point | Epoch () noexcept |
Returns std::chrono::system_clock::time_point from the start of the epoch. | |
std::chrono::steady_clock::time_point | SteadyNow () noexcept |
std::chrono::steady_clock::now() that could be mocked | |
bool | IsTimeBetween (int hour, int min, int hour_from, int min_from, int hour_to, int min_to, bool include_time_to=false) noexcept |
Returns true if the time is in range; works over midnight too. | |
std::string | Timestring (std::time_t timestamp, const std::string &timezone=kDefaultTimezone, const std::string &format=kDefaultFormat) |
Returns time in a string of specified format. | |
std::string | LocalTimezoneTimestring (std::time_t timestamp, const std::string &format=kDefaultFormat) |
Returns time in a string of specified format. | |
std::string | Timestring (std::chrono::system_clock::time_point tp, const std::string &timezone=kDefaultTimezone, const std::string &format=kDefaultFormat) |
Returns time in a string of specified format. | |
std::string | LocalTimezoneTimestring (std::chrono::system_clock::time_point tp, const std::string &format=kDefaultFormat) |
Returns time in a string of specified format. | |
std::chrono::system_clock::time_point | Stringtime (const std::string ×tring, const std::string &timezone=kDefaultTimezone, const std::string &format=kDefaultFormat) |
Extracts time point from a string of a specified format. | |
std::chrono::system_clock::time_point | LocalTimezoneStringtime (const std::string ×tring, const std::string &format=kDefaultFormat) |
Extracts time point from a string of a specified format. | |
std::chrono::system_clock::time_point | GuessStringtime (const std::string ×tamp, const std::string &timezone) |
Extracts time point from a string, guessing the format. | |
std::chrono::system_clock::time_point | GuessLocalTimezoneStringtime (const std::string ×tamp) |
Extracts time point from a string, guessing the format. | |
std::optional< std::chrono::system_clock::time_point > | OptionalStringtime (const std::string ×tring, const std::string &timezone=kDefaultTimezone, const std::string &format=kDefaultFormat) |
Returns optional time in a string of specified format. | |
std::time_t | Timestamp (std::chrono::system_clock::time_point tp) noexcept |
Converts time point to std::time_t. | |
std::time_t | Timestamp () noexcept |
Returned current time as std::time_t; could be mocked. | |
std::uint32_t | ParseDayTime (const std::string &str) |
Parse day time in hh:mm[:ss] format. | |
cctz::civil_second | Localize (const std::chrono::system_clock::time_point &tp, const std::string &timezone) |
Converts absolute time in std::chrono::system_clock::time_point to a civil time of a particular timezone. | |
cctz::civil_second | LocalTimezoneLocalize (const std::chrono::system_clock::time_point &tp) |
Converts absolute time in std::chrono::system_clock::time_point to a civil time of a local timezone. | |
std::time_t | Unlocalize (const cctz::civil_second &local_tp, const std::string &timezone) |
Converts a civil time in a specified timezone into an absolute time. | |
std::time_t | LocalTimezoneUnlocalize (const cctz::civil_second &local_tp) |
Converts a civil time in a local timezone into an absolute time. | |
std::string | TimestampToString (std::time_t timestamp) |
Returns string with time in ISO8601 format "YYYY-MM-DDTHH:MM:SS+0000". | |
std::int64_t | TimePointToTicks (const std::chrono::system_clock::time_point &tp) noexcept |
Convert time_point to DotNet ticks. | |
std::chrono::system_clock::time_point | TicksToTimePoint (std::int64_t ticks) noexcept |
Convert DotNet ticks to a time point. | |
template<class Duration , class Clock > | |
double | CalcTimeDiff (const std::chrono::time_point< Clock > &a, const std::chrono::time_point< Clock > &b) |
Compute (a - b) with a specified duration. | |
Date | DateFromRFC3339String (const std::string &date_string) |
Validates date_string and constructs date from YYYY-MM-DD string and. | |
std::string | ToString (Date date) |
Outputs date as a YYYY-MM-DD string. | |
template<typename Value > | |
std::enable_if_t< formats::common::kIsFormatValue< Value >, Date > | Parse (const Value &value, formats::parse::To< Date >) |
template<typename Value > | |
std::enable_if_t< formats::common::kIsFormatValue< Value >, Value > | Serialize (Date date, formats::serialize::To< Value >) |
template<typename StringBuilder > | |
void | WriteToStream (Date value, StringBuilder &sw) |
template<typename LogHelper = ::logging::LogHelper> | |
::logging::LogHelper & | operator<< (::logging::LogHelper &lh, const Date &date) |
std::ostream & | operator<< (std::ostream &os, Date date) |
std::chrono::system_clock::time_point | FromRfc3339StringSaturating (const std::string ×tring) |
Converts strings like "2012-12-12T00:00:00" to std::chrono::system_clock::time_point in UTC timezone and saturates on overflow Example: | |
std::chrono::system_clock::time_point | FromStringSaturating (const std::string ×tring, const std::string &format) |
Converts strings of the specified format starting with "%Y" to std::chrono::system_clock::time_point in UTC timezone and saturates on overflow. | |
bool | operator< (const TimePointTzBase::TimePoint &lhs, const TimePointTzBase &rhs) |
bool | operator> (const TimePointTzBase::TimePoint &lhs, const TimePointTzBase &rhs) |
bool | operator<= (const TimePointTzBase::TimePoint &lhs, const TimePointTzBase &rhs) |
bool | operator>= (const TimePointTzBase::TimePoint &lhs, const TimePointTzBase &rhs) |
logging::LogHelper & | operator<< (logging::LogHelper &os, const TimePointTz &v) |
logging::LogHelper & | operator<< (logging::LogHelper &os, const TimePointTzIsoBasic &v) |
void | MockNowSet (std::chrono::system_clock::time_point new_mocked_now) |
void | MockSleep (std::chrono::milliseconds duration) |
void | MockNowUnset () noexcept |
bool | IsMockNow () noexcept |
Date | Convert (const std::string &value, chaotic::convert::To< Date >) |
std::string | Convert (const Date &value, chaotic::convert::To< std::string >) |
TimePointTz | Convert (const std::string &str, chaotic::convert::To< TimePointTz >) |
std::string | Convert (const TimePointTz &tp, chaotic::convert::To< std::string >) |
TimePointTzIsoBasic | Convert (const std::string &str, chaotic::convert::To< TimePointTzIsoBasic >) |
std::string | Convert (const TimePointTzIsoBasic &tp, chaotic::convert::To< std::string >) |
Duration arithmetic | |
template<typename LDuration , typename RDuration > | |
auto | operator- (TimeOfDay< LDuration > lhs, TimeOfDay< RDuration > rhs) |
template<typename Duration , typename Rep , typename Period > | |
TimeOfDay< Duration > | operator+ (TimeOfDay< Duration > lhs, std::chrono::duration< Rep, Period > rhs) |
template<typename Duration , typename Rep , typename Period > | |
TimeOfDay< Duration > | operator- (TimeOfDay< Duration > lhs, std::chrono::duration< Rep, Period > rhs) |
template<typename Duration > | |
logging::LogHelper & | operator<< (logging::LogHelper &lh, TimeOfDay< Duration > value) |
Variables | |
const std::string | kRfc3339Format = "%Y-%m-%dT%H:%M:%E*S%Ez" |
const std::string | kTaximeterFormat = "%Y-%m-%dT%H:%M:%E6SZ" |
constexpr std::time_t | kStartOfTheEpoch = 0 |
const std::string | kDefaultDriverTimezone = "Europe/Moscow" |
const std::string | kDefaultTimezone = "UTC" |
const std::string | kDefaultFormat = "%Y-%m-%dT%H:%M:%E*S%z" |
const std::string | kIsoFormat = "%Y-%m-%dT%H:%M:%SZ" |
using utils::datetime::timepair_t = std::pair<std::uint8_t, std::uint8_t> |
Definition at line 32 of file datetime.hpp.
double utils::datetime::CalcTimeDiff | ( | const std::chrono::time_point< Clock > & | a, |
const std::chrono::time_point< Clock > & | b ) |
Compute (a - b) with a specified duration.
Definition at line 232 of file datetime.hpp.
std::chrono::system_clock::time_point utils::datetime::FromRfc3339StringSaturating | ( | const std::string & | timestring | ) |
Converts strings like "2012-12-12T00:00:00" to std::chrono::system_clock::time_point in UTC timezone and saturates on overflow Example:
std::chrono::system_clock::time_point utils::datetime::GuessLocalTimezoneStringtime | ( | const std::string & | timestamp | ) |
Extracts time point from a string, guessing the format.
utils::datetime::DateParseError |
std::chrono::system_clock::time_point utils::datetime::GuessStringtime | ( | const std::string & | timestamp, |
const std::string & | timezone ) |
Extracts time point from a string, guessing the format.
Example:
|
noexcept |
Returns true if time point is mocked for utils::datetime::Now() and utils::datetime::SteadyNow().
cctz::civil_second utils::datetime::Localize | ( | const std::chrono::system_clock::time_point & | tp, |
const std::string & | timezone ) |
Converts absolute time in std::chrono::system_clock::time_point to a civil time of a particular timezone.
utils::datetime::TimezoneLookupError |
Example:
std::chrono::system_clock::time_point utils::datetime::LocalTimezoneStringtime | ( | const std::string & | timestring, |
const std::string & | format = kDefaultFormat ) |
Extracts time point from a string of a specified format.
utils::datetime::DateParseError |
std::string utils::datetime::LocalTimezoneTimestring | ( | std::chrono::system_clock::time_point | tp, |
const std::string & | format = kDefaultFormat ) |
Returns time in a string of specified format.
std::string utils::datetime::LocalTimezoneTimestring | ( | std::time_t | timestamp, |
const std::string & | format = kDefaultFormat ) |
Returns time in a string of specified format.
void utils::datetime::MockNowSet | ( | std::chrono::system_clock::time_point | new_mocked_now | ) |
Sets the mocked value for utils::datetime::Now() and utils::datetime::SteadyNow().
Thread safe.
|
noexcept |
Removes time point mocking for utils::datetime::Now() and utils::datetime::SteadyNow().
void utils::datetime::MockSleep | ( | std::chrono::milliseconds | duration | ) |
Adds duration to current mocked time point value
Thread safe.
utils::InvariantError | if IsMockNow() returns false. |
|
noexcept |
std::chrono::system_clock::now() that could be mocked
Returns last time point passed to utils::datetime::MockNowSet(), or std::chrono::system_clock::now() if the timepoint is not mocked.
TimeOfDay< Duration > utils::datetime::operator+ | ( | TimeOfDay< Duration > | lhs, |
std::chrono::duration< Rep, Period > | rhs ) |
Definition at line 134 of file time_of_day.hpp.
TimeOfDay< Duration > utils::datetime::operator- | ( | TimeOfDay< Duration > | lhs, |
std::chrono::duration< Rep, Period > | rhs ) |
Definition at line 139 of file time_of_day.hpp.
Definition at line 129 of file time_of_day.hpp.
::logging::LogHelper & utils::datetime::operator<< | ( | ::logging::LogHelper & | lh, |
const Date & | date ) |
logging::LogHelper & utils::datetime::operator<< | ( | logging::LogHelper & | lh, |
TimeOfDay< Duration > | value ) |
Definition at line 145 of file time_of_day.hpp.
std::optional< std::chrono::system_clock::time_point > utils::datetime::OptionalStringtime | ( | const std::string & | timestring, |
const std::string & | timezone = kDefaultTimezone, | ||
const std::string & | format = kDefaultFormat ) |
Returns optional time in a string of specified format.
Example:
std::enable_if_t< formats::common::kIsFormatValue< Value >, Date > utils::datetime::Parse | ( | const Value & | value, |
formats::parse::To< Date > | ) |
std::uint32_t utils::datetime::ParseDayTime | ( | const std::string & | str | ) |
Parse day time in hh:mm[:ss] format.
str | day time in format hh:mm[:ss] |
std::enable_if_t< formats::common::kIsFormatValue< Value >, Value > utils::datetime::Serialize | ( | Date | date, |
formats::serialize::To< Value > | ) |
|
noexcept |
std::chrono::steady_clock::now() that could be mocked
Returns last time point passed to utils::datetime::MockNowSet(), or std::chrono::steady_clock::now() if the timepoint is not mocked.
It is only intended for period-based structures/algorithms testing.
std::chrono::system_clock::time_point utils::datetime::Stringtime | ( | const std::string & | timestring, |
const std::string & | timezone = kDefaultTimezone, | ||
const std::string & | format = kDefaultFormat ) |
Extracts time point from a string of a specified format.
Example:
|
noexcept |
Convert time_point to DotNet ticks.
time | point day time |
Example:
|
noexcept |
Converts time point to std::time_t.
Example:
std::string utils::datetime::TimestampToString | ( | std::time_t | timestamp | ) |
Returns string with time in ISO8601 format "YYYY-MM-DDTHH:MM:SS+0000".
timestamp | unix timestamp |
std::string utils::datetime::Timestring | ( | std::chrono::system_clock::time_point | tp, |
const std::string & | timezone = kDefaultTimezone, | ||
const std::string & | format = kDefaultFormat ) |
Returns time in a string of specified format.
utils::datetime::TimezoneLookupError |
Example:
std::string utils::datetime::Timestring | ( | std::time_t | timestamp, |
const std::string & | timezone = kDefaultTimezone, | ||
const std::string & | format = kDefaultFormat ) |
Returns time in a string of specified format.
utils::datetime::TimezoneLookupError |
Example:
std::time_t utils::datetime::Unlocalize | ( | const cctz::civil_second & | local_tp, |
const std::string & | timezone ) |
Converts a civil time in a specified timezone into an absolute time.
utils::datetime::TimezoneLookupError |
Example:
Definition at line 24 of file datetime.hpp.
Definition at line 28 of file datetime.hpp.
|
inline |
Definition at line 26 of file datetime.hpp.
Definition at line 30 of file datetime.hpp.
Definition at line 19 of file datetime.hpp.
|
inlineconstexpr |
Definition at line 22 of file datetime.hpp.
Definition at line 21 of file datetime.hpp.