userver: userver/utils/datetime.hpp File Reference
Loading...
Searching...
No Matches
datetime.hpp File Reference

Detailed Description

Date and Time related converters.

Definition in file datetime.hpp.

Go to the source code of this file.

#include <chrono>
#include <cstdint>
#include <optional>
#include <stdexcept>
#include <string>
#include <cctz/civil_time.h>
+ This graph shows which files directly or indirectly include this file:

Classes

class  utils::datetime::DateParseError
 Date/time parsing error. More...
 
class  utils::datetime::TimezoneLookupError
 Timezone information lookup error. More...
 
class  utils::datetime::SteadyClock
 

Namespaces

namespace  utils
 Utilities.
 
namespace  utils::datetime
 Date and time utilities.
 

Typedefs

using utils::datetime::timepair_t = std::pair<std::uint8_t, std::uint8_t>
 

Functions

std::chrono::system_clock::time_point utils::datetime::Now () noexcept
 std::chrono::system_clock::now() that could be mocked
 
std::chrono::system_clock::time_point utils::datetime::Epoch () noexcept
 Returns std::chrono::system_clock::time_point from the start of the epoch.
 
std::chrono::steady_clock::time_point utils::datetime::SteadyNow () noexcept
 std::chrono::steady_clock::now() that could be mocked
 
bool utils::datetime::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 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.
 
std::string utils::datetime::LocalTimezoneTimestring (std::time_t timestamp, const std::string &format=kDefaultFormat)
 Returns time in a string of specified format.
 
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.
 
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::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.
 
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.
 
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.
 
std::chrono::system_clock::time_point utils::datetime::GuessLocalTimezoneStringtime (const std::string &timestamp)
 Extracts time point from a string, guessing the format.
 
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.
 
std::time_t utils::datetime::Timestamp (std::chrono::system_clock::time_point tp) noexcept
 Converts time point to std::time_t.
 
std::time_t utils::datetime::Timestamp () noexcept
 Returned current time as std::time_t; could be mocked.
 
std::uint32_t utils::datetime::ParseDayTime (const std::string &str)
 Parse day time in hh:mm[:ss] format.
 
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.
 
cctz::civil_second utils::datetime::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 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.
 
std::time_t utils::datetime::LocalTimezoneUnlocalize (const cctz::civil_second &local_tp)
 Converts a civil time in a local timezone into an absolute time.
 
std::string utils::datetime::TimestampToString (std::time_t timestamp)
 Returns string with time in ISO8601 format "YYYY-MM-DDTHH:MM:SS+0000".
 
std::int64_t utils::datetime::TimePointToTicks (const std::chrono::system_clock::time_point &tp) noexcept
 Convert time_point to DotNet ticks.
 
std::chrono::system_clock::time_point utils::datetime::TicksToTimePoint (std::int64_t ticks) noexcept
 Convert DotNet ticks to a time point.
 
template<class Duration , class Clock >
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.
 

Variables

const std::string utils::datetime::kRfc3339Format = "%Y-%m-%dT%H:%M:%E*S%Ez"
 
const std::string utils::datetime::kTaximeterFormat = "%Y-%m-%dT%H:%M:%E6SZ"
 
constexpr std::time_t utils::datetime::kStartOfTheEpoch = 0
 
const std::string utils::datetime::kDefaultDriverTimezone = "Europe/Moscow"
 
const std::string utils::datetime::kDefaultTimezone = "UTC"
 
const std::string utils::datetime::kDefaultFormat = "%Y-%m-%dT%H:%M:%E*S%z"
 
const std::string utils::datetime::kIsoFormat = "%Y-%m-%dT%H:%M:%SZ"