time.h 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // -----------------------------------------------------------------------------
  16. // File: time.h
  17. // -----------------------------------------------------------------------------
  18. //
  19. // This header file defines abstractions for computing with absolute points
  20. // in time, durations of time, and formatting and parsing time within a given
  21. // time zone. The following abstractions are defined:
  22. //
  23. // * `y_absl::Time` defines an absolute, specific instance in time
  24. // * `y_absl::Duration` defines a signed, fixed-length span of time
  25. // * `y_absl::TimeZone` defines geopolitical time zone regions (as collected
  26. // within the IANA Time Zone database (https://www.iana.org/time-zones)).
  27. //
  28. // Note: Absolute times are distinct from civil times, which refer to the
  29. // human-scale time commonly represented by `YYYY-MM-DD hh:mm:ss`. The mapping
  30. // between absolute and civil times can be specified by use of time zones
  31. // (`y_absl::TimeZone` within this API). That is:
  32. //
  33. // Civil Time = F(Absolute Time, Time Zone)
  34. // Absolute Time = G(Civil Time, Time Zone)
  35. //
  36. // See civil_time.h for abstractions related to constructing and manipulating
  37. // civil time.
  38. //
  39. // Example:
  40. //
  41. // y_absl::TimeZone nyc;
  42. // // LoadTimeZone() may fail so it's always better to check for success.
  43. // if (!y_absl::LoadTimeZone("America/New_York", &nyc)) {
  44. // // handle error case
  45. // }
  46. //
  47. // // My flight leaves NYC on Jan 2, 2017 at 03:04:05
  48. // y_absl::CivilSecond cs(2017, 1, 2, 3, 4, 5);
  49. // y_absl::Time takeoff = y_absl::FromCivil(cs, nyc);
  50. //
  51. // y_absl::Duration flight_duration = y_absl::Hours(21) + y_absl::Minutes(35);
  52. // y_absl::Time landing = takeoff + flight_duration;
  53. //
  54. // y_absl::TimeZone syd;
  55. // if (!y_absl::LoadTimeZone("Australia/Sydney", &syd)) {
  56. // // handle error case
  57. // }
  58. // TString s = y_absl::FormatTime(
  59. // "My flight will land in Sydney on %Y-%m-%d at %H:%M:%S",
  60. // landing, syd);
  61. #ifndef Y_ABSL_TIME_TIME_H_
  62. #define Y_ABSL_TIME_TIME_H_
  63. #if !defined(_MSC_VER)
  64. #include <sys/time.h>
  65. #else
  66. // We don't include `winsock2.h` because it drags in `windows.h` and friends,
  67. // and they define conflicting macros like OPAQUE, ERROR, and more. This has the
  68. // potential to break Abseil users.
  69. //
  70. // Instead we only forward declare `timeval` and require Windows users include
  71. // `winsock2.h` themselves. This is both inconsistent and troublesome, but so is
  72. // including 'windows.h' so we are picking the lesser of two evils here.
  73. struct timeval;
  74. #endif
  75. #include <chrono> // NOLINT(build/c++11)
  76. #ifdef __cpp_lib_three_way_comparison
  77. #include <compare>
  78. #endif // __cpp_lib_three_way_comparison
  79. #include <cmath>
  80. #include <cstdint>
  81. #include <ctime>
  82. #include <limits>
  83. #include <ostream>
  84. #include <ratio> // NOLINT(build/c++11)
  85. #include <util/generic/string.h>
  86. #include <type_traits>
  87. #include <utility>
  88. #include "y_absl/base/attributes.h"
  89. #include "y_absl/base/config.h"
  90. #include "y_absl/base/macros.h"
  91. #include "y_absl/strings/string_view.h"
  92. #include "y_absl/time/civil_time.h"
  93. #include "y_absl/time/internal/cctz/include/cctz/time_zone.h"
  94. namespace y_absl {
  95. Y_ABSL_NAMESPACE_BEGIN
  96. class Duration; // Defined below
  97. class Time; // Defined below
  98. class TimeZone; // Defined below
  99. namespace time_internal {
  100. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromUnixDuration(Duration d);
  101. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration ToUnixDuration(Time t);
  102. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr int64_t GetRepHi(Duration d);
  103. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr uint32_t GetRepLo(Duration d);
  104. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration MakeDuration(int64_t hi,
  105. uint32_t lo);
  106. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration MakeDuration(int64_t hi,
  107. int64_t lo);
  108. Y_ABSL_ATTRIBUTE_CONST_FUNCTION inline Duration MakePosDoubleDuration(double n);
  109. constexpr int64_t kTicksPerNanosecond = 4;
  110. constexpr int64_t kTicksPerSecond = 1000 * 1000 * 1000 * kTicksPerNanosecond;
  111. template <std::intmax_t N>
  112. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration FromInt64(int64_t v,
  113. std::ratio<1, N>);
  114. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration FromInt64(int64_t v,
  115. std::ratio<60>);
  116. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration FromInt64(int64_t v,
  117. std::ratio<3600>);
  118. template <typename T>
  119. using EnableIfIntegral = typename std::enable_if<
  120. std::is_integral<T>::value || std::is_enum<T>::value, int>::type;
  121. template <typename T>
  122. using EnableIfFloat =
  123. typename std::enable_if<std::is_floating_point<T>::value, int>::type;
  124. } // namespace time_internal
  125. // Duration
  126. //
  127. // The `y_absl::Duration` class represents a signed, fixed-length amount of time.
  128. // A `Duration` is generated using a unit-specific factory function, or is
  129. // the result of subtracting one `y_absl::Time` from another. Durations behave
  130. // like unit-safe integers and they support all the natural integer-like
  131. // arithmetic operations. Arithmetic overflows and saturates at +/- infinity.
  132. // `Duration` should be passed by value rather than const reference.
  133. //
  134. // Factory functions `Nanoseconds()`, `Microseconds()`, `Milliseconds()`,
  135. // `Seconds()`, `Minutes()`, `Hours()` and `InfiniteDuration()` allow for
  136. // creation of constexpr `Duration` values
  137. //
  138. // Examples:
  139. //
  140. // constexpr y_absl::Duration ten_ns = y_absl::Nanoseconds(10);
  141. // constexpr y_absl::Duration min = y_absl::Minutes(1);
  142. // constexpr y_absl::Duration hour = y_absl::Hours(1);
  143. // y_absl::Duration dur = 60 * min; // dur == hour
  144. // y_absl::Duration half_sec = y_absl::Milliseconds(500);
  145. // y_absl::Duration quarter_sec = 0.25 * y_absl::Seconds(1);
  146. //
  147. // `Duration` values can be easily converted to an integral number of units
  148. // using the division operator.
  149. //
  150. // Example:
  151. //
  152. // constexpr y_absl::Duration dur = y_absl::Milliseconds(1500);
  153. // int64_t ns = dur / y_absl::Nanoseconds(1); // ns == 1500000000
  154. // int64_t ms = dur / y_absl::Milliseconds(1); // ms == 1500
  155. // int64_t sec = dur / y_absl::Seconds(1); // sec == 1 (subseconds truncated)
  156. // int64_t min = dur / y_absl::Minutes(1); // min == 0
  157. //
  158. // See the `IDivDuration()` and `FDivDuration()` functions below for details on
  159. // how to access the fractional parts of the quotient.
  160. //
  161. // Alternatively, conversions can be performed using helpers such as
  162. // `ToInt64Microseconds()` and `ToDoubleSeconds()`.
  163. class Duration {
  164. public:
  165. // Value semantics.
  166. constexpr Duration() : rep_hi_(0), rep_lo_(0) {} // zero-length duration
  167. // Copyable.
  168. #if !defined(__clang__) && defined(_MSC_VER) && _MSC_VER < 1930
  169. // Explicitly defining the constexpr copy constructor avoids an MSVC bug.
  170. constexpr Duration(const Duration& d)
  171. : rep_hi_(d.rep_hi_), rep_lo_(d.rep_lo_) {}
  172. #else
  173. constexpr Duration(const Duration& d) = default;
  174. #endif
  175. Duration& operator=(const Duration& d) = default;
  176. // Compound assignment operators.
  177. Duration& operator+=(Duration d);
  178. Duration& operator-=(Duration d);
  179. Duration& operator*=(int64_t r);
  180. Duration& operator*=(double r);
  181. Duration& operator/=(int64_t r);
  182. Duration& operator/=(double r);
  183. Duration& operator%=(Duration rhs);
  184. // Overloads that forward to either the int64_t or double overloads above.
  185. // Integer operands must be representable as int64_t. Integer division is
  186. // truncating, so values less than the resolution will be returned as zero.
  187. // Floating-point multiplication and division is rounding (halfway cases
  188. // rounding away from zero), so values less than the resolution may be
  189. // returned as either the resolution or zero. In particular, `d / 2.0`
  190. // can produce `d` when it is the resolution and "even".
  191. template <typename T, time_internal::EnableIfIntegral<T> = 0>
  192. Duration& operator*=(T r) {
  193. int64_t x = r;
  194. return *this *= x;
  195. }
  196. template <typename T, time_internal::EnableIfIntegral<T> = 0>
  197. Duration& operator/=(T r) {
  198. int64_t x = r;
  199. return *this /= x;
  200. }
  201. template <typename T, time_internal::EnableIfFloat<T> = 0>
  202. Duration& operator*=(T r) {
  203. double x = r;
  204. return *this *= x;
  205. }
  206. template <typename T, time_internal::EnableIfFloat<T> = 0>
  207. Duration& operator/=(T r) {
  208. double x = r;
  209. return *this /= x;
  210. }
  211. template <typename H>
  212. friend H AbslHashValue(H h, Duration d) {
  213. return H::combine(std::move(h), d.rep_hi_.Get(), d.rep_lo_);
  214. }
  215. private:
  216. friend constexpr int64_t time_internal::GetRepHi(Duration d);
  217. friend constexpr uint32_t time_internal::GetRepLo(Duration d);
  218. friend constexpr Duration time_internal::MakeDuration(int64_t hi,
  219. uint32_t lo);
  220. constexpr Duration(int64_t hi, uint32_t lo) : rep_hi_(hi), rep_lo_(lo) {}
  221. // We store `rep_hi_` 4-byte rather than 8-byte aligned to avoid 4 bytes of
  222. // tail padding.
  223. class HiRep {
  224. public:
  225. // Default constructor default-initializes `hi_`, which has the same
  226. // semantics as default-initializing an `int64_t` (undetermined value).
  227. HiRep() = default;
  228. HiRep(const HiRep&) = default;
  229. HiRep& operator=(const HiRep&) = default;
  230. explicit constexpr HiRep(const int64_t value)
  231. : // C++17 forbids default-initialization in constexpr contexts. We can
  232. // remove this in C++20.
  233. #if defined(Y_ABSL_IS_BIG_ENDIAN) && Y_ABSL_IS_BIG_ENDIAN
  234. hi_(0),
  235. lo_(0)
  236. #else
  237. lo_(0),
  238. hi_(0)
  239. #endif
  240. {
  241. *this = value;
  242. }
  243. constexpr int64_t Get() const {
  244. const uint64_t unsigned_value =
  245. (static_cast<uint64_t>(hi_) << 32) | static_cast<uint64_t>(lo_);
  246. // `static_cast<int64_t>(unsigned_value)` is implementation-defined
  247. // before c++20. On all supported platforms the behaviour is that mandated
  248. // by c++20, i.e. "If the destination type is signed, [...] the result is
  249. // the unique value of the destination type equal to the source value
  250. // modulo 2^n, where n is the number of bits used to represent the
  251. // destination type."
  252. static_assert(
  253. (static_cast<int64_t>((std::numeric_limits<uint64_t>::max)()) ==
  254. int64_t{-1}) &&
  255. (static_cast<int64_t>(static_cast<uint64_t>(
  256. (std::numeric_limits<int64_t>::max)()) +
  257. 1) ==
  258. (std::numeric_limits<int64_t>::min)()),
  259. "static_cast<int64_t>(uint64_t) does not have c++20 semantics");
  260. return static_cast<int64_t>(unsigned_value);
  261. }
  262. constexpr HiRep& operator=(const int64_t value) {
  263. // "If the destination type is unsigned, the resulting value is the
  264. // smallest unsigned value equal to the source value modulo 2^n
  265. // where `n` is the number of bits used to represent the destination
  266. // type".
  267. const auto unsigned_value = static_cast<uint64_t>(value);
  268. hi_ = static_cast<uint32_t>(unsigned_value >> 32);
  269. lo_ = static_cast<uint32_t>(unsigned_value);
  270. return *this;
  271. }
  272. private:
  273. // Notes:
  274. // - Ideally we would use a `char[]` and `std::bitcast`, but the latter
  275. // does not exist (and is not constexpr in `y_absl`) before c++20.
  276. // - Order is optimized depending on endianness so that the compiler can
  277. // turn `Get()` (resp. `operator=()`) into a single 8-byte load (resp.
  278. // store).
  279. #if defined(Y_ABSL_IS_BIG_ENDIAN) && Y_ABSL_IS_BIG_ENDIAN
  280. uint32_t hi_;
  281. uint32_t lo_;
  282. #else
  283. uint32_t lo_;
  284. uint32_t hi_;
  285. #endif
  286. };
  287. HiRep rep_hi_;
  288. uint32_t rep_lo_;
  289. };
  290. // Relational Operators
  291. #ifdef __cpp_lib_three_way_comparison
  292. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr std::strong_ordering operator<=>(
  293. Duration lhs, Duration rhs);
  294. #endif // __cpp_lib_three_way_comparison
  295. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator<(Duration lhs,
  296. Duration rhs);
  297. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator>(Duration lhs,
  298. Duration rhs) {
  299. return rhs < lhs;
  300. }
  301. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator>=(Duration lhs,
  302. Duration rhs) {
  303. return !(lhs < rhs);
  304. }
  305. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator<=(Duration lhs,
  306. Duration rhs) {
  307. return !(rhs < lhs);
  308. }
  309. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator==(Duration lhs,
  310. Duration rhs);
  311. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator!=(Duration lhs,
  312. Duration rhs) {
  313. return !(lhs == rhs);
  314. }
  315. // Additive Operators
  316. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration operator-(Duration d);
  317. Y_ABSL_ATTRIBUTE_CONST_FUNCTION inline Duration operator+(Duration lhs,
  318. Duration rhs) {
  319. return lhs += rhs;
  320. }
  321. Y_ABSL_ATTRIBUTE_CONST_FUNCTION inline Duration operator-(Duration lhs,
  322. Duration rhs) {
  323. return lhs -= rhs;
  324. }
  325. // IDivDuration()
  326. //
  327. // Divides a numerator `Duration` by a denominator `Duration`, returning the
  328. // quotient and remainder. The remainder always has the same sign as the
  329. // numerator. The returned quotient and remainder respect the identity:
  330. //
  331. // numerator = denominator * quotient + remainder
  332. //
  333. // Returned quotients are capped to the range of `int64_t`, with the difference
  334. // spilling into the remainder to uphold the above identity. This means that the
  335. // remainder returned could differ from the remainder returned by
  336. // `Duration::operator%` for huge quotients.
  337. //
  338. // See also the notes on `InfiniteDuration()` below regarding the behavior of
  339. // division involving zero and infinite durations.
  340. //
  341. // Example:
  342. //
  343. // constexpr y_absl::Duration a =
  344. // y_absl::Seconds(std::numeric_limits<int64_t>::max()); // big
  345. // constexpr y_absl::Duration b = y_absl::Nanoseconds(1); // small
  346. //
  347. // y_absl::Duration rem = a % b;
  348. // // rem == y_absl::ZeroDuration()
  349. //
  350. // // Here, q would overflow int64_t, so rem accounts for the difference.
  351. // int64_t q = y_absl::IDivDuration(a, b, &rem);
  352. // // q == std::numeric_limits<int64_t>::max(), rem == a - b * q
  353. int64_t IDivDuration(Duration num, Duration den, Duration* rem);
  354. // FDivDuration()
  355. //
  356. // Divides a `Duration` numerator into a fractional number of units of a
  357. // `Duration` denominator.
  358. //
  359. // See also the notes on `InfiniteDuration()` below regarding the behavior of
  360. // division involving zero and infinite durations.
  361. //
  362. // Example:
  363. //
  364. // double d = y_absl::FDivDuration(y_absl::Milliseconds(1500), y_absl::Seconds(1));
  365. // // d == 1.5
  366. Y_ABSL_ATTRIBUTE_CONST_FUNCTION double FDivDuration(Duration num, Duration den);
  367. // Multiplicative Operators
  368. // Integer operands must be representable as int64_t.
  369. template <typename T>
  370. Y_ABSL_ATTRIBUTE_CONST_FUNCTION Duration operator*(Duration lhs, T rhs) {
  371. return lhs *= rhs;
  372. }
  373. template <typename T>
  374. Y_ABSL_ATTRIBUTE_CONST_FUNCTION Duration operator*(T lhs, Duration rhs) {
  375. return rhs *= lhs;
  376. }
  377. template <typename T>
  378. Y_ABSL_ATTRIBUTE_CONST_FUNCTION Duration operator/(Duration lhs, T rhs) {
  379. return lhs /= rhs;
  380. }
  381. Y_ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t operator/(Duration lhs,
  382. Duration rhs) {
  383. return IDivDuration(lhs, rhs,
  384. &lhs); // trunc towards zero
  385. }
  386. Y_ABSL_ATTRIBUTE_CONST_FUNCTION inline Duration operator%(Duration lhs,
  387. Duration rhs) {
  388. return lhs %= rhs;
  389. }
  390. // ZeroDuration()
  391. //
  392. // Returns a zero-length duration. This function behaves just like the default
  393. // constructor, but the name helps make the semantics clear at call sites.
  394. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration ZeroDuration() {
  395. return Duration();
  396. }
  397. // AbsDuration()
  398. //
  399. // Returns the absolute value of a duration.
  400. Y_ABSL_ATTRIBUTE_CONST_FUNCTION inline Duration AbsDuration(Duration d) {
  401. return (d < ZeroDuration()) ? -d : d;
  402. }
  403. // Trunc()
  404. //
  405. // Truncates a duration (toward zero) to a multiple of a non-zero unit.
  406. //
  407. // Example:
  408. //
  409. // y_absl::Duration d = y_absl::Nanoseconds(123456789);
  410. // y_absl::Duration a = y_absl::Trunc(d, y_absl::Microseconds(1)); // 123456us
  411. Y_ABSL_ATTRIBUTE_CONST_FUNCTION Duration Trunc(Duration d, Duration unit);
  412. // Floor()
  413. //
  414. // Floors a duration using the passed duration unit to its largest value not
  415. // greater than the duration.
  416. //
  417. // Example:
  418. //
  419. // y_absl::Duration d = y_absl::Nanoseconds(123456789);
  420. // y_absl::Duration b = y_absl::Floor(d, y_absl::Microseconds(1)); // 123456us
  421. Y_ABSL_ATTRIBUTE_CONST_FUNCTION Duration Floor(Duration d, Duration unit);
  422. // Ceil()
  423. //
  424. // Returns the ceiling of a duration using the passed duration unit to its
  425. // smallest value not less than the duration.
  426. //
  427. // Example:
  428. //
  429. // y_absl::Duration d = y_absl::Nanoseconds(123456789);
  430. // y_absl::Duration c = y_absl::Ceil(d, y_absl::Microseconds(1)); // 123457us
  431. Y_ABSL_ATTRIBUTE_CONST_FUNCTION Duration Ceil(Duration d, Duration unit);
  432. // InfiniteDuration()
  433. //
  434. // Returns an infinite `Duration`. To get a `Duration` representing negative
  435. // infinity, use `-InfiniteDuration()`.
  436. //
  437. // Duration arithmetic overflows to +/- infinity and saturates. In general,
  438. // arithmetic with `Duration` infinities is similar to IEEE 754 infinities
  439. // except where IEEE 754 NaN would be involved, in which case +/-
  440. // `InfiniteDuration()` is used in place of a "nan" Duration.
  441. //
  442. // Examples:
  443. //
  444. // constexpr y_absl::Duration inf = y_absl::InfiniteDuration();
  445. // const y_absl::Duration d = ... any finite duration ...
  446. //
  447. // inf == inf + inf
  448. // inf == inf + d
  449. // inf == inf - inf
  450. // -inf == d - inf
  451. //
  452. // inf == d * 1e100
  453. // inf == inf / 2
  454. // 0 == d / inf
  455. // INT64_MAX == inf / d
  456. //
  457. // d < inf
  458. // -inf < d
  459. //
  460. // // Division by zero returns infinity, or INT64_MIN/MAX where appropriate.
  461. // inf == d / 0
  462. // INT64_MAX == d / y_absl::ZeroDuration()
  463. //
  464. // The examples involving the `/` operator above also apply to `IDivDuration()`
  465. // and `FDivDuration()`.
  466. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration InfiniteDuration();
  467. // Nanoseconds()
  468. // Microseconds()
  469. // Milliseconds()
  470. // Seconds()
  471. // Minutes()
  472. // Hours()
  473. //
  474. // Factory functions for constructing `Duration` values from an integral number
  475. // of the unit indicated by the factory function's name. The number must be
  476. // representable as int64_t.
  477. //
  478. // NOTE: no "Days()" factory function exists because "a day" is ambiguous.
  479. // Civil days are not always 24 hours long, and a 24-hour duration often does
  480. // not correspond with a civil day. If a 24-hour duration is needed, use
  481. // `y_absl::Hours(24)`. If you actually want a civil day, use y_absl::CivilDay
  482. // from civil_time.h.
  483. //
  484. // Example:
  485. //
  486. // y_absl::Duration a = y_absl::Seconds(60);
  487. // y_absl::Duration b = y_absl::Minutes(1); // b == a
  488. template <typename T, time_internal::EnableIfIntegral<T> = 0>
  489. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration Nanoseconds(T n) {
  490. return time_internal::FromInt64(n, std::nano{});
  491. }
  492. template <typename T, time_internal::EnableIfIntegral<T> = 0>
  493. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration Microseconds(T n) {
  494. return time_internal::FromInt64(n, std::micro{});
  495. }
  496. template <typename T, time_internal::EnableIfIntegral<T> = 0>
  497. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration Milliseconds(T n) {
  498. return time_internal::FromInt64(n, std::milli{});
  499. }
  500. template <typename T, time_internal::EnableIfIntegral<T> = 0>
  501. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration Seconds(T n) {
  502. return time_internal::FromInt64(n, std::ratio<1>{});
  503. }
  504. template <typename T, time_internal::EnableIfIntegral<T> = 0>
  505. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration Minutes(T n) {
  506. return time_internal::FromInt64(n, std::ratio<60>{});
  507. }
  508. template <typename T, time_internal::EnableIfIntegral<T> = 0>
  509. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration Hours(T n) {
  510. return time_internal::FromInt64(n, std::ratio<3600>{});
  511. }
  512. // Factory overloads for constructing `Duration` values from a floating-point
  513. // number of the unit indicated by the factory function's name. These functions
  514. // exist for convenience, but they are not as efficient as the integral
  515. // factories, which should be preferred.
  516. //
  517. // Example:
  518. //
  519. // auto a = y_absl::Seconds(1.5); // OK
  520. // auto b = y_absl::Milliseconds(1500); // BETTER
  521. template <typename T, time_internal::EnableIfFloat<T> = 0>
  522. Y_ABSL_ATTRIBUTE_CONST_FUNCTION Duration Nanoseconds(T n) {
  523. return n * Nanoseconds(1);
  524. }
  525. template <typename T, time_internal::EnableIfFloat<T> = 0>
  526. Y_ABSL_ATTRIBUTE_CONST_FUNCTION Duration Microseconds(T n) {
  527. return n * Microseconds(1);
  528. }
  529. template <typename T, time_internal::EnableIfFloat<T> = 0>
  530. Y_ABSL_ATTRIBUTE_CONST_FUNCTION Duration Milliseconds(T n) {
  531. return n * Milliseconds(1);
  532. }
  533. template <typename T, time_internal::EnableIfFloat<T> = 0>
  534. Y_ABSL_ATTRIBUTE_CONST_FUNCTION Duration Seconds(T n) {
  535. if (n >= 0) { // Note: `NaN >= 0` is false.
  536. if (n >= static_cast<T>((std::numeric_limits<int64_t>::max)())) {
  537. return InfiniteDuration();
  538. }
  539. return time_internal::MakePosDoubleDuration(n);
  540. } else {
  541. if (std::isnan(n))
  542. return std::signbit(n) ? -InfiniteDuration() : InfiniteDuration();
  543. if (n <= (std::numeric_limits<int64_t>::min)()) return -InfiniteDuration();
  544. return -time_internal::MakePosDoubleDuration(-n);
  545. }
  546. }
  547. template <typename T, time_internal::EnableIfFloat<T> = 0>
  548. Y_ABSL_ATTRIBUTE_CONST_FUNCTION Duration Minutes(T n) {
  549. return n * Minutes(1);
  550. }
  551. template <typename T, time_internal::EnableIfFloat<T> = 0>
  552. Y_ABSL_ATTRIBUTE_CONST_FUNCTION Duration Hours(T n) {
  553. return n * Hours(1);
  554. }
  555. // ToInt64Nanoseconds()
  556. // ToInt64Microseconds()
  557. // ToInt64Milliseconds()
  558. // ToInt64Seconds()
  559. // ToInt64Minutes()
  560. // ToInt64Hours()
  561. //
  562. // Helper functions that convert a Duration to an integral count of the
  563. // indicated unit. These return the same results as the `IDivDuration()`
  564. // function, though they usually do so more efficiently; see the
  565. // documentation of `IDivDuration()` for details about overflow, etc.
  566. //
  567. // Example:
  568. //
  569. // y_absl::Duration d = y_absl::Milliseconds(1500);
  570. // int64_t isec = y_absl::ToInt64Seconds(d); // isec == 1
  571. Y_ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToInt64Nanoseconds(Duration d);
  572. Y_ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToInt64Microseconds(Duration d);
  573. Y_ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToInt64Milliseconds(Duration d);
  574. Y_ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToInt64Seconds(Duration d);
  575. Y_ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToInt64Minutes(Duration d);
  576. Y_ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToInt64Hours(Duration d);
  577. // ToDoubleNanoseconds()
  578. // ToDoubleMicroseconds()
  579. // ToDoubleMilliseconds()
  580. // ToDoubleSeconds()
  581. // ToDoubleMinutes()
  582. // ToDoubleHours()
  583. //
  584. // Helper functions that convert a Duration to a floating point count of the
  585. // indicated unit. These functions are shorthand for the `FDivDuration()`
  586. // function above; see its documentation for details about overflow, etc.
  587. //
  588. // Example:
  589. //
  590. // y_absl::Duration d = y_absl::Milliseconds(1500);
  591. // double dsec = y_absl::ToDoubleSeconds(d); // dsec == 1.5
  592. Y_ABSL_ATTRIBUTE_CONST_FUNCTION double ToDoubleNanoseconds(Duration d);
  593. Y_ABSL_ATTRIBUTE_CONST_FUNCTION double ToDoubleMicroseconds(Duration d);
  594. Y_ABSL_ATTRIBUTE_CONST_FUNCTION double ToDoubleMilliseconds(Duration d);
  595. Y_ABSL_ATTRIBUTE_CONST_FUNCTION double ToDoubleSeconds(Duration d);
  596. Y_ABSL_ATTRIBUTE_CONST_FUNCTION double ToDoubleMinutes(Duration d);
  597. Y_ABSL_ATTRIBUTE_CONST_FUNCTION double ToDoubleHours(Duration d);
  598. // FromChrono()
  599. //
  600. // Converts any of the pre-defined std::chrono durations to an y_absl::Duration.
  601. //
  602. // Example:
  603. //
  604. // std::chrono::milliseconds ms(123);
  605. // y_absl::Duration d = y_absl::FromChrono(ms);
  606. Y_ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono(
  607. const std::chrono::nanoseconds& d);
  608. Y_ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono(
  609. const std::chrono::microseconds& d);
  610. Y_ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono(
  611. const std::chrono::milliseconds& d);
  612. Y_ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono(
  613. const std::chrono::seconds& d);
  614. Y_ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono(
  615. const std::chrono::minutes& d);
  616. Y_ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono(
  617. const std::chrono::hours& d);
  618. // ToChronoNanoseconds()
  619. // ToChronoMicroseconds()
  620. // ToChronoMilliseconds()
  621. // ToChronoSeconds()
  622. // ToChronoMinutes()
  623. // ToChronoHours()
  624. //
  625. // Converts an y_absl::Duration to any of the pre-defined std::chrono durations.
  626. // If overflow would occur, the returned value will saturate at the min/max
  627. // chrono duration value instead.
  628. //
  629. // Example:
  630. //
  631. // y_absl::Duration d = y_absl::Microseconds(123);
  632. // auto x = y_absl::ToChronoMicroseconds(d);
  633. // auto y = y_absl::ToChronoNanoseconds(d); // x == y
  634. // auto z = y_absl::ToChronoSeconds(y_absl::InfiniteDuration());
  635. // // z == std::chrono::seconds::max()
  636. Y_ABSL_ATTRIBUTE_CONST_FUNCTION std::chrono::nanoseconds ToChronoNanoseconds(
  637. Duration d);
  638. Y_ABSL_ATTRIBUTE_CONST_FUNCTION std::chrono::microseconds ToChronoMicroseconds(
  639. Duration d);
  640. Y_ABSL_ATTRIBUTE_CONST_FUNCTION std::chrono::milliseconds ToChronoMilliseconds(
  641. Duration d);
  642. Y_ABSL_ATTRIBUTE_CONST_FUNCTION std::chrono::seconds ToChronoSeconds(Duration d);
  643. Y_ABSL_ATTRIBUTE_CONST_FUNCTION std::chrono::minutes ToChronoMinutes(Duration d);
  644. Y_ABSL_ATTRIBUTE_CONST_FUNCTION std::chrono::hours ToChronoHours(Duration d);
  645. // FormatDuration()
  646. //
  647. // Returns a string representing the duration in the form "72h3m0.5s".
  648. // Returns "inf" or "-inf" for +/- `InfiniteDuration()`.
  649. Y_ABSL_ATTRIBUTE_CONST_FUNCTION TString FormatDuration(Duration d);
  650. // Output stream operator.
  651. inline std::ostream& operator<<(std::ostream& os, Duration d) {
  652. return os << FormatDuration(d);
  653. }
  654. // Support for StrFormat(), StrCat() etc.
  655. template <typename Sink>
  656. void AbslStringify(Sink& sink, Duration d) {
  657. sink.Append(FormatDuration(d));
  658. }
  659. // ParseDuration()
  660. //
  661. // Parses a duration string consisting of a possibly signed sequence of
  662. // decimal numbers, each with an optional fractional part and a unit
  663. // suffix. The valid suffixes are "ns", "us" "ms", "s", "m", and "h".
  664. // Simple examples include "300ms", "-1.5h", and "2h45m". Parses "0" as
  665. // `ZeroDuration()`. Parses "inf" and "-inf" as +/- `InfiniteDuration()`.
  666. bool ParseDuration(y_absl::string_view dur_string, Duration* d);
  667. // AbslParseFlag()
  668. //
  669. // Parses a command-line flag string representation `text` into a Duration
  670. // value. Duration flags must be specified in a format that is valid input for
  671. // `y_absl::ParseDuration()`.
  672. bool AbslParseFlag(y_absl::string_view text, Duration* dst, TString* error);
  673. // AbslUnparseFlag()
  674. //
  675. // Unparses a Duration value into a command-line string representation using
  676. // the format specified by `y_absl::ParseDuration()`.
  677. TString AbslUnparseFlag(Duration d);
  678. Y_ABSL_DEPRECATED("Use AbslParseFlag() instead.")
  679. bool ParseFlag(const TString& text, Duration* dst, TString* error);
  680. Y_ABSL_DEPRECATED("Use AbslUnparseFlag() instead.")
  681. TString UnparseFlag(Duration d);
  682. // Time
  683. //
  684. // An `y_absl::Time` represents a specific instant in time. Arithmetic operators
  685. // are provided for naturally expressing time calculations. Instances are
  686. // created using `y_absl::Now()` and the `y_absl::From*()` factory functions that
  687. // accept the gamut of other time representations. Formatting and parsing
  688. // functions are provided for conversion to and from strings. `y_absl::Time`
  689. // should be passed by value rather than const reference.
  690. //
  691. // `y_absl::Time` assumes there are 60 seconds in a minute, which means the
  692. // underlying time scales must be "smeared" to eliminate leap seconds.
  693. // See https://developers.google.com/time/smear.
  694. //
  695. // Even though `y_absl::Time` supports a wide range of timestamps, exercise
  696. // caution when using values in the distant past. `y_absl::Time` uses the
  697. // Proleptic Gregorian calendar, which extends the Gregorian calendar backward
  698. // to dates before its introduction in 1582.
  699. // See https://en.wikipedia.org/wiki/Proleptic_Gregorian_calendar
  700. // for more information. Use the ICU calendar classes to convert a date in
  701. // some other calendar (http://userguide.icu-project.org/datetime/calendar).
  702. //
  703. // Similarly, standardized time zones are a reasonably recent innovation, with
  704. // the Greenwich prime meridian being established in 1884. The TZ database
  705. // itself does not profess accurate offsets for timestamps prior to 1970. The
  706. // breakdown of future timestamps is subject to the whim of regional
  707. // governments.
  708. //
  709. // The `y_absl::Time` class represents an instant in time as a count of clock
  710. // ticks of some granularity (resolution) from some starting point (epoch).
  711. //
  712. // `y_absl::Time` uses a resolution that is high enough to avoid loss in
  713. // precision, and a range that is wide enough to avoid overflow, when
  714. // converting between tick counts in most Google time scales (i.e., resolution
  715. // of at least one nanosecond, and range +/-100 billion years). Conversions
  716. // between the time scales are performed by truncating (towards negative
  717. // infinity) to the nearest representable point.
  718. //
  719. // Examples:
  720. //
  721. // y_absl::Time t1 = ...;
  722. // y_absl::Time t2 = t1 + y_absl::Minutes(2);
  723. // y_absl::Duration d = t2 - t1; // == y_absl::Minutes(2)
  724. //
  725. class Time {
  726. public:
  727. // Value semantics.
  728. // Returns the Unix epoch. However, those reading your code may not know
  729. // or expect the Unix epoch as the default value, so make your code more
  730. // readable by explicitly initializing all instances before use.
  731. //
  732. // Example:
  733. // y_absl::Time t = y_absl::UnixEpoch();
  734. // y_absl::Time t = y_absl::Now();
  735. // y_absl::Time t = y_absl::TimeFromTimeval(tv);
  736. // y_absl::Time t = y_absl::InfinitePast();
  737. constexpr Time() = default;
  738. // Copyable.
  739. constexpr Time(const Time& t) = default;
  740. Time& operator=(const Time& t) = default;
  741. // Assignment operators.
  742. Time& operator+=(Duration d) {
  743. rep_ += d;
  744. return *this;
  745. }
  746. Time& operator-=(Duration d) {
  747. rep_ -= d;
  748. return *this;
  749. }
  750. // Time::Breakdown
  751. //
  752. // The calendar and wall-clock (aka "civil time") components of an
  753. // `y_absl::Time` in a certain `y_absl::TimeZone`. This struct is not
  754. // intended to represent an instant in time. So, rather than passing
  755. // a `Time::Breakdown` to a function, pass an `y_absl::Time` and an
  756. // `y_absl::TimeZone`.
  757. //
  758. // Deprecated. Use `y_absl::TimeZone::CivilInfo`.
  759. struct Y_ABSL_DEPRECATED("Use `y_absl::TimeZone::CivilInfo`.") Breakdown {
  760. int64_t year; // year (e.g., 2013)
  761. int month; // month of year [1:12]
  762. int day; // day of month [1:31]
  763. int hour; // hour of day [0:23]
  764. int minute; // minute of hour [0:59]
  765. int second; // second of minute [0:59]
  766. Duration subsecond; // [Seconds(0):Seconds(1)) if finite
  767. int weekday; // 1==Mon, ..., 7=Sun
  768. int yearday; // day of year [1:366]
  769. // Note: The following fields exist for backward compatibility
  770. // with older APIs. Accessing these fields directly is a sign of
  771. // imprudent logic in the calling code. Modern time-related code
  772. // should only access this data indirectly by way of FormatTime().
  773. // These fields are undefined for InfiniteFuture() and InfinitePast().
  774. int offset; // seconds east of UTC
  775. bool is_dst; // is offset non-standard?
  776. const char* zone_abbr; // time-zone abbreviation (e.g., "PST")
  777. };
  778. // Time::In()
  779. //
  780. // Returns the breakdown of this instant in the given TimeZone.
  781. //
  782. // Deprecated. Use `y_absl::TimeZone::At(Time)`.
  783. Y_ABSL_INTERNAL_DISABLE_DEPRECATED_DECLARATION_WARNING
  784. Y_ABSL_DEPRECATED("Use `y_absl::TimeZone::At(Time)`.")
  785. Breakdown In(TimeZone tz) const;
  786. Y_ABSL_INTERNAL_RESTORE_DEPRECATED_DECLARATION_WARNING
  787. template <typename H>
  788. friend H AbslHashValue(H h, Time t) {
  789. return H::combine(std::move(h), t.rep_);
  790. }
  791. private:
  792. friend constexpr Time time_internal::FromUnixDuration(Duration d);
  793. friend constexpr Duration time_internal::ToUnixDuration(Time t);
  794. #ifdef __cpp_lib_three_way_comparison
  795. friend constexpr std::strong_ordering operator<=>(Time lhs, Time rhs);
  796. #endif // __cpp_lib_three_way_comparison
  797. friend constexpr bool operator<(Time lhs, Time rhs);
  798. friend constexpr bool operator==(Time lhs, Time rhs);
  799. friend Duration operator-(Time lhs, Time rhs);
  800. friend constexpr Time UniversalEpoch();
  801. friend constexpr Time InfiniteFuture();
  802. friend constexpr Time InfinitePast();
  803. constexpr explicit Time(Duration rep) : rep_(rep) {}
  804. Duration rep_;
  805. };
  806. // Relational Operators
  807. #ifdef __cpp_lib_three_way_comparison
  808. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr std::strong_ordering operator<=>(
  809. Time lhs, Time rhs) {
  810. return lhs.rep_ <=> rhs.rep_;
  811. }
  812. #endif // __cpp_lib_three_way_comparison
  813. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator<(Time lhs, Time rhs) {
  814. return lhs.rep_ < rhs.rep_;
  815. }
  816. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator>(Time lhs, Time rhs) {
  817. return rhs < lhs;
  818. }
  819. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator>=(Time lhs, Time rhs) {
  820. return !(lhs < rhs);
  821. }
  822. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator<=(Time lhs, Time rhs) {
  823. return !(rhs < lhs);
  824. }
  825. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator==(Time lhs, Time rhs) {
  826. return lhs.rep_ == rhs.rep_;
  827. }
  828. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator!=(Time lhs, Time rhs) {
  829. return !(lhs == rhs);
  830. }
  831. // Additive Operators
  832. Y_ABSL_ATTRIBUTE_CONST_FUNCTION inline Time operator+(Time lhs, Duration rhs) {
  833. return lhs += rhs;
  834. }
  835. Y_ABSL_ATTRIBUTE_CONST_FUNCTION inline Time operator+(Duration lhs, Time rhs) {
  836. return rhs += lhs;
  837. }
  838. Y_ABSL_ATTRIBUTE_CONST_FUNCTION inline Time operator-(Time lhs, Duration rhs) {
  839. return lhs -= rhs;
  840. }
  841. Y_ABSL_ATTRIBUTE_CONST_FUNCTION inline Duration operator-(Time lhs, Time rhs) {
  842. return lhs.rep_ - rhs.rep_;
  843. }
  844. // UnixEpoch()
  845. //
  846. // Returns the `y_absl::Time` representing "1970-01-01 00:00:00.0 +0000".
  847. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time UnixEpoch() { return Time(); }
  848. // UniversalEpoch()
  849. //
  850. // Returns the `y_absl::Time` representing "0001-01-01 00:00:00.0 +0000", the
  851. // epoch of the ICU Universal Time Scale.
  852. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time UniversalEpoch() {
  853. // 719162 is the number of days from 0001-01-01 to 1970-01-01,
  854. // assuming the Gregorian calendar.
  855. return Time(
  856. time_internal::MakeDuration(-24 * 719162 * int64_t{3600}, uint32_t{0}));
  857. }
  858. // InfiniteFuture()
  859. //
  860. // Returns an `y_absl::Time` that is infinitely far in the future.
  861. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time InfiniteFuture() {
  862. return Time(time_internal::MakeDuration((std::numeric_limits<int64_t>::max)(),
  863. ~uint32_t{0}));
  864. }
  865. // InfinitePast()
  866. //
  867. // Returns an `y_absl::Time` that is infinitely far in the past.
  868. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time InfinitePast() {
  869. return Time(time_internal::MakeDuration((std::numeric_limits<int64_t>::min)(),
  870. ~uint32_t{0}));
  871. }
  872. // FromUnixNanos()
  873. // FromUnixMicros()
  874. // FromUnixMillis()
  875. // FromUnixSeconds()
  876. // FromTimeT()
  877. // FromUDate()
  878. // FromUniversal()
  879. //
  880. // Creates an `y_absl::Time` from a variety of other representations. See
  881. // https://unicode-org.github.io/icu/userguide/datetime/universaltimescale.html
  882. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromUnixNanos(int64_t ns);
  883. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromUnixMicros(int64_t us);
  884. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromUnixMillis(int64_t ms);
  885. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromUnixSeconds(int64_t s);
  886. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromTimeT(time_t t);
  887. Y_ABSL_ATTRIBUTE_CONST_FUNCTION Time FromUDate(double udate);
  888. Y_ABSL_ATTRIBUTE_CONST_FUNCTION Time FromUniversal(int64_t universal);
  889. // ToUnixNanos()
  890. // ToUnixMicros()
  891. // ToUnixMillis()
  892. // ToUnixSeconds()
  893. // ToTimeT()
  894. // ToUDate()
  895. // ToUniversal()
  896. //
  897. // Converts an `y_absl::Time` to a variety of other representations. See
  898. // https://unicode-org.github.io/icu/userguide/datetime/universaltimescale.html
  899. //
  900. // Note that these operations round down toward negative infinity where
  901. // necessary to adjust to the resolution of the result type. Beware of
  902. // possible time_t over/underflow in ToTime{T,val,spec}() on 32-bit platforms.
  903. Y_ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToUnixNanos(Time t);
  904. Y_ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToUnixMicros(Time t);
  905. Y_ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToUnixMillis(Time t);
  906. Y_ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToUnixSeconds(Time t);
  907. Y_ABSL_ATTRIBUTE_CONST_FUNCTION time_t ToTimeT(Time t);
  908. Y_ABSL_ATTRIBUTE_CONST_FUNCTION double ToUDate(Time t);
  909. Y_ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToUniversal(Time t);
  910. // DurationFromTimespec()
  911. // DurationFromTimeval()
  912. // ToTimespec()
  913. // ToTimeval()
  914. // TimeFromTimespec()
  915. // TimeFromTimeval()
  916. // ToTimespec()
  917. // ToTimeval()
  918. //
  919. // Some APIs use a timespec or a timeval as a Duration (e.g., nanosleep(2)
  920. // and select(2)), while others use them as a Time (e.g. clock_gettime(2)
  921. // and gettimeofday(2)), so conversion functions are provided for both cases.
  922. // The "to timespec/val" direction is easily handled via overloading, but
  923. // for "from timespec/val" the desired type is part of the function name.
  924. Y_ABSL_ATTRIBUTE_CONST_FUNCTION Duration DurationFromTimespec(timespec ts);
  925. Y_ABSL_ATTRIBUTE_CONST_FUNCTION Duration DurationFromTimeval(timeval tv);
  926. Y_ABSL_ATTRIBUTE_CONST_FUNCTION timespec ToTimespec(Duration d);
  927. Y_ABSL_ATTRIBUTE_CONST_FUNCTION timeval ToTimeval(Duration d);
  928. Y_ABSL_ATTRIBUTE_CONST_FUNCTION Time TimeFromTimespec(timespec ts);
  929. Y_ABSL_ATTRIBUTE_CONST_FUNCTION Time TimeFromTimeval(timeval tv);
  930. Y_ABSL_ATTRIBUTE_CONST_FUNCTION timespec ToTimespec(Time t);
  931. Y_ABSL_ATTRIBUTE_CONST_FUNCTION timeval ToTimeval(Time t);
  932. // FromChrono()
  933. //
  934. // Converts a std::chrono::system_clock::time_point to an y_absl::Time.
  935. //
  936. // Example:
  937. //
  938. // auto tp = std::chrono::system_clock::from_time_t(123);
  939. // y_absl::Time t = y_absl::FromChrono(tp);
  940. // // t == y_absl::FromTimeT(123)
  941. Y_ABSL_ATTRIBUTE_PURE_FUNCTION Time
  942. FromChrono(const std::chrono::system_clock::time_point& tp);
  943. // ToChronoTime()
  944. //
  945. // Converts an y_absl::Time to a std::chrono::system_clock::time_point. If
  946. // overflow would occur, the returned value will saturate at the min/max time
  947. // point value instead.
  948. //
  949. // Example:
  950. //
  951. // y_absl::Time t = y_absl::FromTimeT(123);
  952. // auto tp = y_absl::ToChronoTime(t);
  953. // // tp == std::chrono::system_clock::from_time_t(123);
  954. Y_ABSL_ATTRIBUTE_CONST_FUNCTION std::chrono::system_clock::time_point
  955. ToChronoTime(Time);
  956. // AbslParseFlag()
  957. //
  958. // Parses the command-line flag string representation `text` into a Time value.
  959. // Time flags must be specified in a format that matches y_absl::RFC3339_full.
  960. //
  961. // For example:
  962. //
  963. // --start_time=2016-01-02T03:04:05.678+08:00
  964. //
  965. // Note: A UTC offset (or 'Z' indicating a zero-offset from UTC) is required.
  966. //
  967. // Additionally, if you'd like to specify a time as a count of
  968. // seconds/milliseconds/etc from the Unix epoch, use an y_absl::Duration flag
  969. // and add that duration to y_absl::UnixEpoch() to get an y_absl::Time.
  970. bool AbslParseFlag(y_absl::string_view text, Time* t, TString* error);
  971. // AbslUnparseFlag()
  972. //
  973. // Unparses a Time value into a command-line string representation using
  974. // the format specified by `y_absl::ParseTime()`.
  975. TString AbslUnparseFlag(Time t);
  976. Y_ABSL_DEPRECATED("Use AbslParseFlag() instead.")
  977. bool ParseFlag(const TString& text, Time* t, TString* error);
  978. Y_ABSL_DEPRECATED("Use AbslUnparseFlag() instead.")
  979. TString UnparseFlag(Time t);
  980. // TimeZone
  981. //
  982. // The `y_absl::TimeZone` is an opaque, small, value-type class representing a
  983. // geo-political region within which particular rules are used for converting
  984. // between absolute and civil times (see https://git.io/v59Ly). `y_absl::TimeZone`
  985. // values are named using the TZ identifiers from the IANA Time Zone Database,
  986. // such as "America/Los_Angeles" or "Australia/Sydney". `y_absl::TimeZone` values
  987. // are created from factory functions such as `y_absl::LoadTimeZone()`. Note:
  988. // strings like "PST" and "EDT" are not valid TZ identifiers. Prefer to pass by
  989. // value rather than const reference.
  990. //
  991. // For more on the fundamental concepts of time zones, absolute times, and civil
  992. // times, see https://github.com/google/cctz#fundamental-concepts
  993. //
  994. // Examples:
  995. //
  996. // y_absl::TimeZone utc = y_absl::UTCTimeZone();
  997. // y_absl::TimeZone pst = y_absl::FixedTimeZone(-8 * 60 * 60);
  998. // y_absl::TimeZone loc = y_absl::LocalTimeZone();
  999. // y_absl::TimeZone lax;
  1000. // if (!y_absl::LoadTimeZone("America/Los_Angeles", &lax)) {
  1001. // // handle error case
  1002. // }
  1003. //
  1004. // See also:
  1005. // - https://github.com/google/cctz
  1006. // - https://www.iana.org/time-zones
  1007. // - https://en.wikipedia.org/wiki/Zoneinfo
  1008. class TimeZone {
  1009. public:
  1010. explicit TimeZone(time_internal::cctz::time_zone tz) : cz_(tz) {}
  1011. TimeZone() = default; // UTC, but prefer UTCTimeZone() to be explicit.
  1012. // Copyable.
  1013. TimeZone(const TimeZone&) = default;
  1014. TimeZone& operator=(const TimeZone&) = default;
  1015. explicit operator time_internal::cctz::time_zone() const { return cz_; }
  1016. TString name() const { return cz_.name(); }
  1017. // TimeZone::CivilInfo
  1018. //
  1019. // Information about the civil time corresponding to an absolute time.
  1020. // This struct is not intended to represent an instant in time. So, rather
  1021. // than passing a `TimeZone::CivilInfo` to a function, pass an `y_absl::Time`
  1022. // and an `y_absl::TimeZone`.
  1023. struct CivilInfo {
  1024. CivilSecond cs;
  1025. Duration subsecond;
  1026. // Note: The following fields exist for backward compatibility
  1027. // with older APIs. Accessing these fields directly is a sign of
  1028. // imprudent logic in the calling code. Modern time-related code
  1029. // should only access this data indirectly by way of FormatTime().
  1030. // These fields are undefined for InfiniteFuture() and InfinitePast().
  1031. int offset; // seconds east of UTC
  1032. bool is_dst; // is offset non-standard?
  1033. const char* zone_abbr; // time-zone abbreviation (e.g., "PST")
  1034. };
  1035. // TimeZone::At(Time)
  1036. //
  1037. // Returns the civil time for this TimeZone at a certain `y_absl::Time`.
  1038. // If the input time is infinite, the output civil second will be set to
  1039. // CivilSecond::max() or min(), and the subsecond will be infinite.
  1040. //
  1041. // Example:
  1042. //
  1043. // const auto epoch = lax.At(y_absl::UnixEpoch());
  1044. // // epoch.cs == 1969-12-31 16:00:00
  1045. // // epoch.subsecond == y_absl::ZeroDuration()
  1046. // // epoch.offset == -28800
  1047. // // epoch.is_dst == false
  1048. // // epoch.abbr == "PST"
  1049. CivilInfo At(Time t) const;
  1050. // TimeZone::TimeInfo
  1051. //
  1052. // Information about the absolute times corresponding to a civil time.
  1053. // (Subseconds must be handled separately.)
  1054. //
  1055. // It is possible for a caller to pass a civil-time value that does
  1056. // not represent an actual or unique instant in time (due to a shift
  1057. // in UTC offset in the TimeZone, which results in a discontinuity in
  1058. // the civil-time components). For example, a daylight-saving-time
  1059. // transition skips or repeats civil times---in the United States,
  1060. // March 13, 2011 02:15 never occurred, while November 6, 2011 01:15
  1061. // occurred twice---so requests for such times are not well-defined.
  1062. // To account for these possibilities, `y_absl::TimeZone::TimeInfo` is
  1063. // richer than just a single `y_absl::Time`.
  1064. struct TimeInfo {
  1065. enum CivilKind {
  1066. UNIQUE, // the civil time was singular (pre == trans == post)
  1067. SKIPPED, // the civil time did not exist (pre >= trans > post)
  1068. REPEATED, // the civil time was ambiguous (pre < trans <= post)
  1069. } kind;
  1070. Time pre; // time calculated using the pre-transition offset
  1071. Time trans; // when the civil-time discontinuity occurred
  1072. Time post; // time calculated using the post-transition offset
  1073. };
  1074. // TimeZone::At(CivilSecond)
  1075. //
  1076. // Returns an `y_absl::TimeInfo` containing the absolute time(s) for this
  1077. // TimeZone at an `y_absl::CivilSecond`. When the civil time is skipped or
  1078. // repeated, returns times calculated using the pre-transition and post-
  1079. // transition UTC offsets, plus the transition time itself.
  1080. //
  1081. // Examples:
  1082. //
  1083. // // A unique civil time
  1084. // const auto jan01 = lax.At(y_absl::CivilSecond(2011, 1, 1, 0, 0, 0));
  1085. // // jan01.kind == TimeZone::TimeInfo::UNIQUE
  1086. // // jan01.pre is 2011-01-01 00:00:00 -0800
  1087. // // jan01.trans is 2011-01-01 00:00:00 -0800
  1088. // // jan01.post is 2011-01-01 00:00:00 -0800
  1089. //
  1090. // // A Spring DST transition, when there is a gap in civil time
  1091. // const auto mar13 = lax.At(y_absl::CivilSecond(2011, 3, 13, 2, 15, 0));
  1092. // // mar13.kind == TimeZone::TimeInfo::SKIPPED
  1093. // // mar13.pre is 2011-03-13 03:15:00 -0700
  1094. // // mar13.trans is 2011-03-13 03:00:00 -0700
  1095. // // mar13.post is 2011-03-13 01:15:00 -0800
  1096. //
  1097. // // A Fall DST transition, when civil times are repeated
  1098. // const auto nov06 = lax.At(y_absl::CivilSecond(2011, 11, 6, 1, 15, 0));
  1099. // // nov06.kind == TimeZone::TimeInfo::REPEATED
  1100. // // nov06.pre is 2011-11-06 01:15:00 -0700
  1101. // // nov06.trans is 2011-11-06 01:00:00 -0800
  1102. // // nov06.post is 2011-11-06 01:15:00 -0800
  1103. TimeInfo At(CivilSecond ct) const;
  1104. // TimeZone::NextTransition()
  1105. // TimeZone::PrevTransition()
  1106. //
  1107. // Finds the time of the next/previous offset change in this time zone.
  1108. //
  1109. // By definition, `NextTransition(t, &trans)` returns false when `t` is
  1110. // `InfiniteFuture()`, and `PrevTransition(t, &trans)` returns false
  1111. // when `t` is `InfinitePast()`. If the zone has no transitions, the
  1112. // result will also be false no matter what the argument.
  1113. //
  1114. // Otherwise, when `t` is `InfinitePast()`, `NextTransition(t, &trans)`
  1115. // returns true and sets `trans` to the first recorded transition. Chains
  1116. // of calls to `NextTransition()/PrevTransition()` will eventually return
  1117. // false, but it is unspecified exactly when `NextTransition(t, &trans)`
  1118. // jumps to false, or what time is set by `PrevTransition(t, &trans)` for
  1119. // a very distant `t`.
  1120. //
  1121. // Note: Enumeration of time-zone transitions is for informational purposes
  1122. // only. Modern time-related code should not care about when offset changes
  1123. // occur.
  1124. //
  1125. // Example:
  1126. // y_absl::TimeZone nyc;
  1127. // if (!y_absl::LoadTimeZone("America/New_York", &nyc)) { ... }
  1128. // const auto now = y_absl::Now();
  1129. // auto t = y_absl::InfinitePast();
  1130. // y_absl::TimeZone::CivilTransition trans;
  1131. // while (t <= now && nyc.NextTransition(t, &trans)) {
  1132. // // transition: trans.from -> trans.to
  1133. // t = nyc.At(trans.to).trans;
  1134. // }
  1135. struct CivilTransition {
  1136. CivilSecond from; // the civil time we jump from
  1137. CivilSecond to; // the civil time we jump to
  1138. };
  1139. bool NextTransition(Time t, CivilTransition* trans) const;
  1140. bool PrevTransition(Time t, CivilTransition* trans) const;
  1141. template <typename H>
  1142. friend H AbslHashValue(H h, TimeZone tz) {
  1143. return H::combine(std::move(h), tz.cz_);
  1144. }
  1145. private:
  1146. friend bool operator==(TimeZone a, TimeZone b) { return a.cz_ == b.cz_; }
  1147. friend bool operator!=(TimeZone a, TimeZone b) { return a.cz_ != b.cz_; }
  1148. friend std::ostream& operator<<(std::ostream& os, TimeZone tz) {
  1149. return os << tz.name();
  1150. }
  1151. time_internal::cctz::time_zone cz_;
  1152. };
  1153. // LoadTimeZone()
  1154. //
  1155. // Loads the named zone. May perform I/O on the initial load of the named
  1156. // zone. If the name is invalid, or some other kind of error occurs, returns
  1157. // `false` and `*tz` is set to the UTC time zone.
  1158. inline bool LoadTimeZone(y_absl::string_view name, TimeZone* tz) {
  1159. if (name == "localtime") {
  1160. *tz = TimeZone(time_internal::cctz::local_time_zone());
  1161. return true;
  1162. }
  1163. time_internal::cctz::time_zone cz;
  1164. const bool b = time_internal::cctz::load_time_zone(TString(name), &cz);
  1165. *tz = TimeZone(cz);
  1166. return b;
  1167. }
  1168. // FixedTimeZone()
  1169. //
  1170. // Returns a TimeZone that is a fixed offset (seconds east) from UTC.
  1171. // Note: If the absolute value of the offset is greater than 24 hours
  1172. // you'll get UTC (i.e., no offset) instead.
  1173. inline TimeZone FixedTimeZone(int seconds) {
  1174. return TimeZone(
  1175. time_internal::cctz::fixed_time_zone(std::chrono::seconds(seconds)));
  1176. }
  1177. // UTCTimeZone()
  1178. //
  1179. // Convenience method returning the UTC time zone.
  1180. inline TimeZone UTCTimeZone() {
  1181. return TimeZone(time_internal::cctz::utc_time_zone());
  1182. }
  1183. // LocalTimeZone()
  1184. //
  1185. // Convenience method returning the local time zone, or UTC if there is
  1186. // no configured local zone. Warning: Be wary of using LocalTimeZone(),
  1187. // and particularly so in a server process, as the zone configured for the
  1188. // local machine should be irrelevant. Prefer an explicit zone name.
  1189. inline TimeZone LocalTimeZone() {
  1190. return TimeZone(time_internal::cctz::local_time_zone());
  1191. }
  1192. // ToCivilSecond()
  1193. // ToCivilMinute()
  1194. // ToCivilHour()
  1195. // ToCivilDay()
  1196. // ToCivilMonth()
  1197. // ToCivilYear()
  1198. //
  1199. // Helpers for TimeZone::At(Time) to return particularly aligned civil times.
  1200. //
  1201. // Example:
  1202. //
  1203. // y_absl::Time t = ...;
  1204. // y_absl::TimeZone tz = ...;
  1205. // const auto cd = y_absl::ToCivilDay(t, tz);
  1206. Y_ABSL_ATTRIBUTE_PURE_FUNCTION inline CivilSecond ToCivilSecond(Time t,
  1207. TimeZone tz) {
  1208. return tz.At(t).cs; // already a CivilSecond
  1209. }
  1210. Y_ABSL_ATTRIBUTE_PURE_FUNCTION inline CivilMinute ToCivilMinute(Time t,
  1211. TimeZone tz) {
  1212. return CivilMinute(tz.At(t).cs);
  1213. }
  1214. Y_ABSL_ATTRIBUTE_PURE_FUNCTION inline CivilHour ToCivilHour(Time t, TimeZone tz) {
  1215. return CivilHour(tz.At(t).cs);
  1216. }
  1217. Y_ABSL_ATTRIBUTE_PURE_FUNCTION inline CivilDay ToCivilDay(Time t, TimeZone tz) {
  1218. return CivilDay(tz.At(t).cs);
  1219. }
  1220. Y_ABSL_ATTRIBUTE_PURE_FUNCTION inline CivilMonth ToCivilMonth(Time t,
  1221. TimeZone tz) {
  1222. return CivilMonth(tz.At(t).cs);
  1223. }
  1224. Y_ABSL_ATTRIBUTE_PURE_FUNCTION inline CivilYear ToCivilYear(Time t, TimeZone tz) {
  1225. return CivilYear(tz.At(t).cs);
  1226. }
  1227. // FromCivil()
  1228. //
  1229. // Helper for TimeZone::At(CivilSecond) that provides "order-preserving
  1230. // semantics." If the civil time maps to a unique time, that time is
  1231. // returned. If the civil time is repeated in the given time zone, the
  1232. // time using the pre-transition offset is returned. Otherwise, the
  1233. // civil time is skipped in the given time zone, and the transition time
  1234. // is returned. This means that for any two civil times, ct1 and ct2,
  1235. // (ct1 < ct2) => (FromCivil(ct1) <= FromCivil(ct2)), the equal case
  1236. // being when two non-existent civil times map to the same transition time.
  1237. //
  1238. // Note: Accepts civil times of any alignment.
  1239. Y_ABSL_ATTRIBUTE_PURE_FUNCTION inline Time FromCivil(CivilSecond ct,
  1240. TimeZone tz) {
  1241. const auto ti = tz.At(ct);
  1242. if (ti.kind == TimeZone::TimeInfo::SKIPPED) return ti.trans;
  1243. return ti.pre;
  1244. }
  1245. // TimeConversion
  1246. //
  1247. // An `y_absl::TimeConversion` represents the conversion of year, month, day,
  1248. // hour, minute, and second values (i.e., a civil time), in a particular
  1249. // `y_absl::TimeZone`, to a time instant (an absolute time), as returned by
  1250. // `y_absl::ConvertDateTime()`. Legacy version of `y_absl::TimeZone::TimeInfo`.
  1251. //
  1252. // Deprecated. Use `y_absl::TimeZone::TimeInfo`.
  1253. struct Y_ABSL_DEPRECATED("Use `y_absl::TimeZone::TimeInfo`.") TimeConversion {
  1254. Time pre; // time calculated using the pre-transition offset
  1255. Time trans; // when the civil-time discontinuity occurred
  1256. Time post; // time calculated using the post-transition offset
  1257. enum Kind {
  1258. UNIQUE, // the civil time was singular (pre == trans == post)
  1259. SKIPPED, // the civil time did not exist
  1260. REPEATED, // the civil time was ambiguous
  1261. };
  1262. Kind kind;
  1263. bool normalized; // input values were outside their valid ranges
  1264. };
  1265. // ConvertDateTime()
  1266. //
  1267. // Legacy version of `y_absl::TimeZone::At(y_absl::CivilSecond)` that takes
  1268. // the civil time as six, separate values (YMDHMS).
  1269. //
  1270. // The input month, day, hour, minute, and second values can be outside
  1271. // of their valid ranges, in which case they will be "normalized" during
  1272. // the conversion.
  1273. //
  1274. // Example:
  1275. //
  1276. // // "October 32" normalizes to "November 1".
  1277. // y_absl::TimeConversion tc =
  1278. // y_absl::ConvertDateTime(2013, 10, 32, 8, 30, 0, lax);
  1279. // // tc.kind == TimeConversion::UNIQUE && tc.normalized == true
  1280. // // y_absl::ToCivilDay(tc.pre, tz).month() == 11
  1281. // // y_absl::ToCivilDay(tc.pre, tz).day() == 1
  1282. //
  1283. // Deprecated. Use `y_absl::TimeZone::At(CivilSecond)`.
  1284. Y_ABSL_INTERNAL_DISABLE_DEPRECATED_DECLARATION_WARNING
  1285. Y_ABSL_DEPRECATED("Use `y_absl::TimeZone::At(CivilSecond)`.")
  1286. TimeConversion ConvertDateTime(int64_t year, int mon, int day, int hour,
  1287. int min, int sec, TimeZone tz);
  1288. Y_ABSL_INTERNAL_RESTORE_DEPRECATED_DECLARATION_WARNING
  1289. // FromDateTime()
  1290. //
  1291. // A convenience wrapper for `y_absl::ConvertDateTime()` that simply returns
  1292. // the "pre" `y_absl::Time`. That is, the unique result, or the instant that
  1293. // is correct using the pre-transition offset (as if the transition never
  1294. // happened).
  1295. //
  1296. // Example:
  1297. //
  1298. // y_absl::Time t = y_absl::FromDateTime(2017, 9, 26, 9, 30, 0, lax);
  1299. // // t = 2017-09-26 09:30:00 -0700
  1300. //
  1301. // Deprecated. Use `y_absl::FromCivil(CivilSecond, TimeZone)`. Note that the
  1302. // behavior of `FromCivil()` differs from `FromDateTime()` for skipped civil
  1303. // times. If you care about that see `y_absl::TimeZone::At(y_absl::CivilSecond)`.
  1304. Y_ABSL_DEPRECATED("Use `y_absl::FromCivil(CivilSecond, TimeZone)`.")
  1305. inline Time FromDateTime(int64_t year, int mon, int day, int hour, int min,
  1306. int sec, TimeZone tz) {
  1307. Y_ABSL_INTERNAL_DISABLE_DEPRECATED_DECLARATION_WARNING
  1308. return ConvertDateTime(year, mon, day, hour, min, sec, tz).pre;
  1309. Y_ABSL_INTERNAL_RESTORE_DEPRECATED_DECLARATION_WARNING
  1310. }
  1311. // FromTM()
  1312. //
  1313. // Converts the `tm_year`, `tm_mon`, `tm_mday`, `tm_hour`, `tm_min`, and
  1314. // `tm_sec` fields to an `y_absl::Time` using the given time zone. See ctime(3)
  1315. // for a description of the expected values of the tm fields. If the civil time
  1316. // is unique (see `y_absl::TimeZone::At(y_absl::CivilSecond)` above), the matching
  1317. // time instant is returned. Otherwise, the `tm_isdst` field is consulted to
  1318. // choose between the possible results. For a repeated civil time, `tm_isdst !=
  1319. // 0` returns the matching DST instant, while `tm_isdst == 0` returns the
  1320. // matching non-DST instant. For a skipped civil time there is no matching
  1321. // instant, so `tm_isdst != 0` returns the DST instant, and `tm_isdst == 0`
  1322. // returns the non-DST instant, that would have matched if the transition never
  1323. // happened.
  1324. Y_ABSL_ATTRIBUTE_PURE_FUNCTION Time FromTM(const struct tm& tm, TimeZone tz);
  1325. // ToTM()
  1326. //
  1327. // Converts the given `y_absl::Time` to a struct tm using the given time zone.
  1328. // See ctime(3) for a description of the values of the tm fields.
  1329. Y_ABSL_ATTRIBUTE_PURE_FUNCTION struct tm ToTM(Time t, TimeZone tz);
  1330. // RFC3339_full
  1331. // RFC3339_sec
  1332. //
  1333. // FormatTime()/ParseTime() format specifiers for RFC3339 date/time strings,
  1334. // with trailing zeros trimmed or with fractional seconds omitted altogether.
  1335. //
  1336. // Note that RFC3339_sec[] matches an ISO 8601 extended format for date and
  1337. // time with UTC offset. Also note the use of "%Y": RFC3339 mandates that
  1338. // years have exactly four digits, but we allow them to take their natural
  1339. // width.
  1340. Y_ABSL_DLL extern const char RFC3339_full[]; // %Y-%m-%d%ET%H:%M:%E*S%Ez
  1341. Y_ABSL_DLL extern const char RFC3339_sec[]; // %Y-%m-%d%ET%H:%M:%S%Ez
  1342. // RFC1123_full
  1343. // RFC1123_no_wday
  1344. //
  1345. // FormatTime()/ParseTime() format specifiers for RFC1123 date/time strings.
  1346. Y_ABSL_DLL extern const char RFC1123_full[]; // %a, %d %b %E4Y %H:%M:%S %z
  1347. Y_ABSL_DLL extern const char RFC1123_no_wday[]; // %d %b %E4Y %H:%M:%S %z
  1348. // FormatTime()
  1349. //
  1350. // Formats the given `y_absl::Time` in the `y_absl::TimeZone` according to the
  1351. // provided format string. Uses strftime()-like formatting options, with
  1352. // the following extensions:
  1353. //
  1354. // - %Ez - RFC3339-compatible numeric UTC offset (+hh:mm or -hh:mm)
  1355. // - %E*z - Full-resolution numeric UTC offset (+hh:mm:ss or -hh:mm:ss)
  1356. // - %E#S - Seconds with # digits of fractional precision
  1357. // - %E*S - Seconds with full fractional precision (a literal '*')
  1358. // - %E#f - Fractional seconds with # digits of precision
  1359. // - %E*f - Fractional seconds with full precision (a literal '*')
  1360. // - %E4Y - Four-character years (-999 ... -001, 0000, 0001 ... 9999)
  1361. // - %ET - The RFC3339 "date-time" separator "T"
  1362. //
  1363. // Note that %E0S behaves like %S, and %E0f produces no characters. In
  1364. // contrast %E*f always produces at least one digit, which may be '0'.
  1365. //
  1366. // Note that %Y produces as many characters as it takes to fully render the
  1367. // year. A year outside of [-999:9999] when formatted with %E4Y will produce
  1368. // more than four characters, just like %Y.
  1369. //
  1370. // We recommend that format strings include the UTC offset (%z, %Ez, or %E*z)
  1371. // so that the result uniquely identifies a time instant.
  1372. //
  1373. // Example:
  1374. //
  1375. // y_absl::CivilSecond cs(2013, 1, 2, 3, 4, 5);
  1376. // y_absl::Time t = y_absl::FromCivil(cs, lax);
  1377. // TString f = y_absl::FormatTime("%H:%M:%S", t, lax); // "03:04:05"
  1378. // f = y_absl::FormatTime("%H:%M:%E3S", t, lax); // "03:04:05.000"
  1379. //
  1380. // Note: If the given `y_absl::Time` is `y_absl::InfiniteFuture()`, the returned
  1381. // string will be exactly "infinite-future". If the given `y_absl::Time` is
  1382. // `y_absl::InfinitePast()`, the returned string will be exactly "infinite-past".
  1383. // In both cases the given format string and `y_absl::TimeZone` are ignored.
  1384. //
  1385. Y_ABSL_ATTRIBUTE_PURE_FUNCTION TString FormatTime(y_absl::string_view format,
  1386. Time t, TimeZone tz);
  1387. // Convenience functions that format the given time using the RFC3339_full
  1388. // format. The first overload uses the provided TimeZone, while the second
  1389. // uses LocalTimeZone().
  1390. Y_ABSL_ATTRIBUTE_PURE_FUNCTION TString FormatTime(Time t, TimeZone tz);
  1391. Y_ABSL_ATTRIBUTE_PURE_FUNCTION TString FormatTime(Time t);
  1392. // Output stream operator.
  1393. inline std::ostream& operator<<(std::ostream& os, Time t) {
  1394. return os << FormatTime(t);
  1395. }
  1396. // Support for StrFormat(), StrCat() etc.
  1397. template <typename Sink>
  1398. void AbslStringify(Sink& sink, Time t) {
  1399. sink.Append(FormatTime(t));
  1400. }
  1401. // ParseTime()
  1402. //
  1403. // Parses an input string according to the provided format string and
  1404. // returns the corresponding `y_absl::Time`. Uses strftime()-like formatting
  1405. // options, with the same extensions as FormatTime(), but with the
  1406. // exceptions that %E#S is interpreted as %E*S, and %E#f as %E*f. %Ez
  1407. // and %E*z also accept the same inputs, which (along with %z) includes
  1408. // 'z' and 'Z' as synonyms for +00:00. %ET accepts either 'T' or 't'.
  1409. //
  1410. // %Y consumes as many numeric characters as it can, so the matching data
  1411. // should always be terminated with a non-numeric. %E4Y always consumes
  1412. // exactly four characters, including any sign.
  1413. //
  1414. // Unspecified fields are taken from the default date and time of ...
  1415. //
  1416. // "1970-01-01 00:00:00.0 +0000"
  1417. //
  1418. // For example, parsing a string of "15:45" (%H:%M) will return an y_absl::Time
  1419. // that represents "1970-01-01 15:45:00.0 +0000".
  1420. //
  1421. // Note that since ParseTime() returns time instants, it makes the most sense
  1422. // to parse fully-specified date/time strings that include a UTC offset (%z,
  1423. // %Ez, or %E*z).
  1424. //
  1425. // Note also that `y_absl::ParseTime()` only heeds the fields year, month, day,
  1426. // hour, minute, (fractional) second, and UTC offset. Other fields, like
  1427. // weekday (%a or %A), while parsed for syntactic validity, are ignored
  1428. // in the conversion.
  1429. //
  1430. // Date and time fields that are out-of-range will be treated as errors
  1431. // rather than normalizing them like `y_absl::CivilSecond` does. For example,
  1432. // it is an error to parse the date "Oct 32, 2013" because 32 is out of range.
  1433. //
  1434. // A leap second of ":60" is normalized to ":00" of the following minute
  1435. // with fractional seconds discarded. The following table shows how the
  1436. // given seconds and subseconds will be parsed:
  1437. //
  1438. // "59.x" -> 59.x // exact
  1439. // "60.x" -> 00.0 // normalized
  1440. // "00.x" -> 00.x // exact
  1441. //
  1442. // Errors are indicated by returning false and assigning an error message
  1443. // to the "err" out param if it is non-null.
  1444. //
  1445. // Note: If the input string is exactly "infinite-future", the returned
  1446. // `y_absl::Time` will be `y_absl::InfiniteFuture()` and `true` will be returned.
  1447. // If the input string is "infinite-past", the returned `y_absl::Time` will be
  1448. // `y_absl::InfinitePast()` and `true` will be returned.
  1449. //
  1450. bool ParseTime(y_absl::string_view format, y_absl::string_view input, Time* time,
  1451. TString* err);
  1452. // Like ParseTime() above, but if the format string does not contain a UTC
  1453. // offset specification (%z/%Ez/%E*z) then the input is interpreted in the
  1454. // given TimeZone. This means that the input, by itself, does not identify a
  1455. // unique instant. Being time-zone dependent, it also admits the possibility
  1456. // of ambiguity or non-existence, in which case the "pre" time (as defined
  1457. // by TimeZone::TimeInfo) is returned. For these reasons we recommend that
  1458. // all date/time strings include a UTC offset so they're context independent.
  1459. bool ParseTime(y_absl::string_view format, y_absl::string_view input, TimeZone tz,
  1460. Time* time, TString* err);
  1461. // ============================================================================
  1462. // Implementation Details Follow
  1463. // ============================================================================
  1464. namespace time_internal {
  1465. // Creates a Duration with a given representation.
  1466. // REQUIRES: hi,lo is a valid representation of a Duration as specified
  1467. // in time/duration.cc.
  1468. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration MakeDuration(int64_t hi,
  1469. uint32_t lo = 0) {
  1470. return Duration(hi, lo);
  1471. }
  1472. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration MakeDuration(int64_t hi,
  1473. int64_t lo) {
  1474. return MakeDuration(hi, static_cast<uint32_t>(lo));
  1475. }
  1476. // Make a Duration value from a floating-point number, as long as that number
  1477. // is in the range [ 0 .. numeric_limits<int64_t>::max ), that is, as long as
  1478. // it's positive and can be converted to int64_t without risk of UB.
  1479. Y_ABSL_ATTRIBUTE_CONST_FUNCTION inline Duration MakePosDoubleDuration(double n) {
  1480. const int64_t int_secs = static_cast<int64_t>(n);
  1481. const uint32_t ticks = static_cast<uint32_t>(
  1482. std::round((n - static_cast<double>(int_secs)) * kTicksPerSecond));
  1483. return ticks < kTicksPerSecond
  1484. ? MakeDuration(int_secs, ticks)
  1485. : MakeDuration(int_secs + 1, ticks - kTicksPerSecond);
  1486. }
  1487. // Creates a normalized Duration from an almost-normalized (sec,ticks)
  1488. // pair. sec may be positive or negative. ticks must be in the range
  1489. // -kTicksPerSecond < *ticks < kTicksPerSecond. If ticks is negative it
  1490. // will be normalized to a positive value in the resulting Duration.
  1491. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration MakeNormalizedDuration(
  1492. int64_t sec, int64_t ticks) {
  1493. return (ticks < 0) ? MakeDuration(sec - 1, ticks + kTicksPerSecond)
  1494. : MakeDuration(sec, ticks);
  1495. }
  1496. // Provide access to the Duration representation.
  1497. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr int64_t GetRepHi(Duration d) {
  1498. return d.rep_hi_.Get();
  1499. }
  1500. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr uint32_t GetRepLo(Duration d) {
  1501. return d.rep_lo_;
  1502. }
  1503. // Returns true iff d is positive or negative infinity.
  1504. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool IsInfiniteDuration(Duration d) {
  1505. return GetRepLo(d) == ~uint32_t{0};
  1506. }
  1507. // Returns an infinite Duration with the opposite sign.
  1508. // REQUIRES: IsInfiniteDuration(d)
  1509. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration OppositeInfinity(Duration d) {
  1510. return GetRepHi(d) < 0
  1511. ? MakeDuration((std::numeric_limits<int64_t>::max)(), ~uint32_t{0})
  1512. : MakeDuration((std::numeric_limits<int64_t>::min)(),
  1513. ~uint32_t{0});
  1514. }
  1515. // Returns (-n)-1 (equivalently -(n+1)) without avoidable overflow.
  1516. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr int64_t NegateAndSubtractOne(
  1517. int64_t n) {
  1518. // Note: Good compilers will optimize this expression to ~n when using
  1519. // a two's-complement representation (which is required for int64_t).
  1520. return (n < 0) ? -(n + 1) : (-n) - 1;
  1521. }
  1522. // Map between a Time and a Duration since the Unix epoch. Note that these
  1523. // functions depend on the above mentioned choice of the Unix epoch for the
  1524. // Time representation (and both need to be Time friends). Without this
  1525. // knowledge, we would need to add-in/subtract-out UnixEpoch() respectively.
  1526. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromUnixDuration(Duration d) {
  1527. return Time(d);
  1528. }
  1529. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration ToUnixDuration(Time t) {
  1530. return t.rep_;
  1531. }
  1532. template <std::intmax_t N>
  1533. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration FromInt64(int64_t v,
  1534. std::ratio<1, N>) {
  1535. static_assert(0 < N && N <= 1000 * 1000 * 1000, "Unsupported ratio");
  1536. // Subsecond ratios cannot overflow.
  1537. return MakeNormalizedDuration(
  1538. v / N, v % N * kTicksPerNanosecond * 1000 * 1000 * 1000 / N);
  1539. }
  1540. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration FromInt64(int64_t v,
  1541. std::ratio<60>) {
  1542. return (v <= (std::numeric_limits<int64_t>::max)() / 60 &&
  1543. v >= (std::numeric_limits<int64_t>::min)() / 60)
  1544. ? MakeDuration(v * 60)
  1545. : v > 0 ? InfiniteDuration() : -InfiniteDuration();
  1546. }
  1547. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration FromInt64(int64_t v,
  1548. std::ratio<3600>) {
  1549. return (v <= (std::numeric_limits<int64_t>::max)() / 3600 &&
  1550. v >= (std::numeric_limits<int64_t>::min)() / 3600)
  1551. ? MakeDuration(v * 3600)
  1552. : v > 0 ? InfiniteDuration() : -InfiniteDuration();
  1553. }
  1554. // IsValidRep64<T>(0) is true if the expression `int64_t{std::declval<T>()}` is
  1555. // valid. That is, if a T can be assigned to an int64_t without narrowing.
  1556. template <typename T>
  1557. constexpr auto IsValidRep64(int) -> decltype(int64_t{std::declval<T>()} == 0) {
  1558. return true;
  1559. }
  1560. template <typename T>
  1561. constexpr auto IsValidRep64(char) -> bool {
  1562. return false;
  1563. }
  1564. // Converts a std::chrono::duration to an y_absl::Duration.
  1565. template <typename Rep, typename Period>
  1566. Y_ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono(
  1567. const std::chrono::duration<Rep, Period>& d) {
  1568. static_assert(IsValidRep64<Rep>(0), "duration::rep is invalid");
  1569. return FromInt64(int64_t{d.count()}, Period{});
  1570. }
  1571. template <typename Ratio>
  1572. Y_ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToInt64(Duration d, Ratio) {
  1573. // Note: This may be used on MSVC, which may have a system_clock period of
  1574. // std::ratio<1, 10 * 1000 * 1000>
  1575. return ToInt64Seconds(d * Ratio::den / Ratio::num);
  1576. }
  1577. // Fastpath implementations for the 6 common duration units.
  1578. Y_ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64(Duration d, std::nano) {
  1579. return ToInt64Nanoseconds(d);
  1580. }
  1581. Y_ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64(Duration d, std::micro) {
  1582. return ToInt64Microseconds(d);
  1583. }
  1584. Y_ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64(Duration d, std::milli) {
  1585. return ToInt64Milliseconds(d);
  1586. }
  1587. Y_ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64(Duration d,
  1588. std::ratio<1>) {
  1589. return ToInt64Seconds(d);
  1590. }
  1591. Y_ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64(Duration d,
  1592. std::ratio<60>) {
  1593. return ToInt64Minutes(d);
  1594. }
  1595. Y_ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64(Duration d,
  1596. std::ratio<3600>) {
  1597. return ToInt64Hours(d);
  1598. }
  1599. // Converts an y_absl::Duration to a chrono duration of type T.
  1600. template <typename T>
  1601. Y_ABSL_ATTRIBUTE_CONST_FUNCTION T ToChronoDuration(Duration d) {
  1602. using Rep = typename T::rep;
  1603. using Period = typename T::period;
  1604. static_assert(IsValidRep64<Rep>(0), "duration::rep is invalid");
  1605. if (time_internal::IsInfiniteDuration(d))
  1606. return d < ZeroDuration() ? (T::min)() : (T::max)();
  1607. const auto v = ToInt64(d, Period{});
  1608. if (v > (std::numeric_limits<Rep>::max)()) return (T::max)();
  1609. if (v < (std::numeric_limits<Rep>::min)()) return (T::min)();
  1610. return T{v};
  1611. }
  1612. } // namespace time_internal
  1613. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator<(Duration lhs,
  1614. Duration rhs) {
  1615. return time_internal::GetRepHi(lhs) != time_internal::GetRepHi(rhs)
  1616. ? time_internal::GetRepHi(lhs) < time_internal::GetRepHi(rhs)
  1617. : time_internal::GetRepHi(lhs) == (std::numeric_limits<int64_t>::min)()
  1618. ? time_internal::GetRepLo(lhs) + 1 <
  1619. time_internal::GetRepLo(rhs) + 1
  1620. : time_internal::GetRepLo(lhs) < time_internal::GetRepLo(rhs);
  1621. }
  1622. #ifdef __cpp_lib_three_way_comparison
  1623. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr std::strong_ordering operator<=>(
  1624. Duration lhs, Duration rhs) {
  1625. const int64_t lhs_hi = time_internal::GetRepHi(lhs);
  1626. const int64_t rhs_hi = time_internal::GetRepHi(rhs);
  1627. if (auto c = lhs_hi <=> rhs_hi; c != std::strong_ordering::equal) {
  1628. return c;
  1629. }
  1630. const uint32_t lhs_lo = time_internal::GetRepLo(lhs);
  1631. const uint32_t rhs_lo = time_internal::GetRepLo(rhs);
  1632. return (lhs_hi == (std::numeric_limits<int64_t>::min)())
  1633. ? (lhs_lo + 1) <=> (rhs_lo + 1)
  1634. : lhs_lo <=> rhs_lo;
  1635. }
  1636. #endif // __cpp_lib_three_way_comparison
  1637. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator==(Duration lhs,
  1638. Duration rhs) {
  1639. return time_internal::GetRepHi(lhs) == time_internal::GetRepHi(rhs) &&
  1640. time_internal::GetRepLo(lhs) == time_internal::GetRepLo(rhs);
  1641. }
  1642. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration operator-(Duration d) {
  1643. // This is a little interesting because of the special cases.
  1644. //
  1645. // If rep_lo_ is zero, we have it easy; it's safe to negate rep_hi_, we're
  1646. // dealing with an integral number of seconds, and the only special case is
  1647. // the maximum negative finite duration, which can't be negated.
  1648. //
  1649. // Infinities stay infinite, and just change direction.
  1650. //
  1651. // Finally we're in the case where rep_lo_ is non-zero, and we can borrow
  1652. // a second's worth of ticks and avoid overflow (as negating int64_t-min + 1
  1653. // is safe).
  1654. return time_internal::GetRepLo(d) == 0
  1655. ? time_internal::GetRepHi(d) ==
  1656. (std::numeric_limits<int64_t>::min)()
  1657. ? InfiniteDuration()
  1658. : time_internal::MakeDuration(-time_internal::GetRepHi(d))
  1659. : time_internal::IsInfiniteDuration(d)
  1660. ? time_internal::OppositeInfinity(d)
  1661. : time_internal::MakeDuration(
  1662. time_internal::NegateAndSubtractOne(
  1663. time_internal::GetRepHi(d)),
  1664. time_internal::kTicksPerSecond -
  1665. time_internal::GetRepLo(d));
  1666. }
  1667. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration InfiniteDuration() {
  1668. return time_internal::MakeDuration((std::numeric_limits<int64_t>::max)(),
  1669. ~uint32_t{0});
  1670. }
  1671. Y_ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono(
  1672. const std::chrono::nanoseconds& d) {
  1673. return time_internal::FromChrono(d);
  1674. }
  1675. Y_ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono(
  1676. const std::chrono::microseconds& d) {
  1677. return time_internal::FromChrono(d);
  1678. }
  1679. Y_ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono(
  1680. const std::chrono::milliseconds& d) {
  1681. return time_internal::FromChrono(d);
  1682. }
  1683. Y_ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono(
  1684. const std::chrono::seconds& d) {
  1685. return time_internal::FromChrono(d);
  1686. }
  1687. Y_ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono(
  1688. const std::chrono::minutes& d) {
  1689. return time_internal::FromChrono(d);
  1690. }
  1691. Y_ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono(
  1692. const std::chrono::hours& d) {
  1693. return time_internal::FromChrono(d);
  1694. }
  1695. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromUnixNanos(int64_t ns) {
  1696. return time_internal::FromUnixDuration(Nanoseconds(ns));
  1697. }
  1698. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromUnixMicros(int64_t us) {
  1699. return time_internal::FromUnixDuration(Microseconds(us));
  1700. }
  1701. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromUnixMillis(int64_t ms) {
  1702. return time_internal::FromUnixDuration(Milliseconds(ms));
  1703. }
  1704. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromUnixSeconds(int64_t s) {
  1705. return time_internal::FromUnixDuration(Seconds(s));
  1706. }
  1707. Y_ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromTimeT(time_t t) {
  1708. return time_internal::FromUnixDuration(Seconds(t));
  1709. }
  1710. Y_ABSL_NAMESPACE_END
  1711. } // namespace y_absl
  1712. #endif // Y_ABSL_TIME_TIME_H_