userver: userver/utils/datetime.hpp Source File
Loading...
Searching...
No Matches
datetime.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/utils/datetime.hpp
4/// @brief Date, Time, and Timezone related converters
5/// @ingroup userver_universal
6
7#include <userver/utils/datetime_light.hpp>
8
9USERVER_NAMESPACE_BEGIN
10
11namespace utils::datetime {
12
13/// @brief Returns time in a string of specified format, for UTC times prefer a faster utils::datetime::UtcTimestring
14///
15/// @throws utils::datetime::TimezoneLookupError
16///
17/// Example:
18///
19/// @snippet utils/datetime_test.cpp Timestring example
20///
21/// @see kRfc3339Format, kTaximeterFormat, kStartOfTheEpoch,
22/// kDefaultDriverTimezone, kDefaultTimezone, kDefaultFormat, kIsoFormat
23std::string Timestring(
24 std::time_t timestamp,
25 const std::string& timezone = kDefaultTimezone,
26 const std::string& format = kDefaultFormat
27);
28
29/// @brief Returns time in a string of specified format, for UTC times prefer a faster utils::datetime::UtcTimestring
30/// @throws utils::datetime::TimezoneLookupError
31///
32/// Example:
33///
34/// @snippet utils/datetime_test.cpp Timestring example
35/// @see kRfc3339Format, kTaximeterFormat, kStartOfTheEpoch,
36/// kDefaultDriverTimezone, kDefaultTimezone, kDefaultFormat, kIsoFormat
37std::string Timestring(
38 std::chrono::system_clock::time_point tp,
39 const std::string& timezone,
40 const std::string& format = kDefaultFormat
41);
42
43/// @brief Extracts time point from a string of a specified format, for UTC times prefer a faster
44/// utils::datetime::UtcStringtime
45///
46/// @throws utils::datetime::DateParseError
47/// @throws utils::datetime::TimezoneLookupError
48///
49/// Example:
50///
51/// @snippet utils/datetime_test.cpp Stringtime example
52/// @see kRfc3339Format, kTaximeterFormat, kStartOfTheEpoch,
53/// kDefaultDriverTimezone, kDefaultTimezone, kDefaultFormat, kIsoFormat
54std::chrono::system_clock::time_point Stringtime(
55 const std::string& timestring,
56 const std::string& timezone,
57 const std::string& format = kDefaultFormat
58);
59
60/// @brief Extracts time point from a string, guessing the format
61/// @throws utils::datetime::DateParseError
62/// @throws utils::datetime::TimezoneLookupError
63///
64/// Example:
65///
66/// @snippet utils/datetime_test.cpp GuessStringtime example
67std::chrono::system_clock::time_point GuessStringtime(const std::string& timestamp, const std::string& timezone);
68
69/// @brief Returns optional time in a string of specified format
70///
71/// Example:
72///
73/// @snippet utils/datetime_test.cpp OptionalStringtime example
74/// @see kRfc3339Format, kTaximeterFormat, kStartOfTheEpoch,
75/// kDefaultDriverTimezone, kDefaultTimezone, kDefaultFormat, kIsoFormat
76std::optional<std::chrono::system_clock::time_point> OptionalStringtime(
77 const std::string& timestring,
78 const std::string& timezone,
79 const std::string& format = kDefaultFormat
80);
81
82/// @brief Converts absolute time in std::chrono::system_clock::time_point to
83/// a civil time of a particular timezone.
84/// @throws utils::datetime::TimezoneLookupError
85///
86/// Example:
87///
88/// @snippet utils/datetime_test.cpp Localize example
89cctz::civil_second Localize(const std::chrono::system_clock::time_point& tp, const std::string& timezone);
90
91/// @brief Converts a civil time in a specified timezone into an absolute time.
92/// @throws utils::datetime::TimezoneLookupError
93///
94/// Example:
95///
96/// @snippet utils/datetime_test.cpp Localize example
97std::time_t Unlocalize(const cctz::civil_second& local_tp, const std::string& timezone);
98
99/// @brief Retrieves a time zone by name
100///
101/// Returns the corresponding time zone if the given timezone name is valid.
102/// Returns std::nullopt if no matching time zone is found.
103///
104/// @param timezone Time zone name (e.g. "UTC", "Europe/Moscow")
105/// @return std::optional containing the time zone or std::nullopt
106std::optional<cctz::time_zone> GetOptionalTimezone(const std::string& timezone);
107
108} // namespace utils::datetime
109
110USERVER_NAMESPACE_END