1*700637cbSDimitry Andric // -*- C++ -*-
2*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
3*700637cbSDimitry Andric //
4*700637cbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5*700637cbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
6*700637cbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7*700637cbSDimitry Andric //
8*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
9*700637cbSDimitry Andric
10*700637cbSDimitry Andric #ifndef _LIBCPP___CHRONO_UTC_CLOCK_H
11*700637cbSDimitry Andric #define _LIBCPP___CHRONO_UTC_CLOCK_H
12*700637cbSDimitry Andric
13*700637cbSDimitry Andric #include <version>
14*700637cbSDimitry Andric // Enable the contents of the header only when libc++ was built with experimental features enabled.
15*700637cbSDimitry Andric #if _LIBCPP_HAS_EXPERIMENTAL_TZDB
16*700637cbSDimitry Andric
17*700637cbSDimitry Andric # include <__chrono/duration.h>
18*700637cbSDimitry Andric # include <__chrono/leap_second.h>
19*700637cbSDimitry Andric # include <__chrono/system_clock.h>
20*700637cbSDimitry Andric # include <__chrono/time_point.h>
21*700637cbSDimitry Andric # include <__chrono/tzdb.h>
22*700637cbSDimitry Andric # include <__chrono/tzdb_list.h>
23*700637cbSDimitry Andric # include <__config>
24*700637cbSDimitry Andric # include <__type_traits/common_type.h>
25*700637cbSDimitry Andric
26*700637cbSDimitry Andric # if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
27*700637cbSDimitry Andric # pragma GCC system_header
28*700637cbSDimitry Andric # endif
29*700637cbSDimitry Andric
30*700637cbSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
31*700637cbSDimitry Andric
32*700637cbSDimitry Andric # if _LIBCPP_STD_VER >= 20 && _LIBCPP_HAS_TIME_ZONE_DATABASE && _LIBCPP_HAS_FILESYSTEM && _LIBCPP_HAS_LOCALIZATION
33*700637cbSDimitry Andric
34*700637cbSDimitry Andric namespace chrono {
35*700637cbSDimitry Andric
36*700637cbSDimitry Andric class utc_clock;
37*700637cbSDimitry Andric
38*700637cbSDimitry Andric template <class _Duration>
39*700637cbSDimitry Andric using utc_time = time_point<utc_clock, _Duration>;
40*700637cbSDimitry Andric using utc_seconds = utc_time<seconds>;
41*700637cbSDimitry Andric
42*700637cbSDimitry Andric class utc_clock {
43*700637cbSDimitry Andric public:
44*700637cbSDimitry Andric using rep = system_clock::rep;
45*700637cbSDimitry Andric using period = system_clock::period;
46*700637cbSDimitry Andric using duration = chrono::duration<rep, period>;
47*700637cbSDimitry Andric using time_point = chrono::time_point<utc_clock>;
48*700637cbSDimitry Andric static constexpr bool is_steady = false; // The system_clock is not steady.
49*700637cbSDimitry Andric
now()50*700637cbSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static time_point now() { return from_sys(system_clock::now()); }
51*700637cbSDimitry Andric
52*700637cbSDimitry Andric template <class _Duration>
53*700637cbSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static sys_time<common_type_t<_Duration, seconds>>
54*700637cbSDimitry Andric to_sys(const utc_time<_Duration>& __time);
55*700637cbSDimitry Andric
56*700637cbSDimitry Andric template <class _Duration>
57*700637cbSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static utc_time<common_type_t<_Duration, seconds>>
from_sys(const sys_time<_Duration> & __time)58*700637cbSDimitry Andric from_sys(const sys_time<_Duration>& __time) {
59*700637cbSDimitry Andric using _Rp = utc_time<common_type_t<_Duration, seconds>>;
60*700637cbSDimitry Andric // TODO TZDB investigate optimizations.
61*700637cbSDimitry Andric //
62*700637cbSDimitry Andric // The leap second database stores all transitions, this mean to calculate
63*700637cbSDimitry Andric // the current number of leap seconds the code needs to iterate over all
64*700637cbSDimitry Andric // leap seconds to accumulate the sum. Then the sum can be used to determine
65*700637cbSDimitry Andric // the sys_time. Accessing the database involves acquiring a mutex.
66*700637cbSDimitry Andric //
67*700637cbSDimitry Andric // The historic entries in the database are immutable. Hard-coding these
68*700637cbSDimitry Andric // values in a table would allow:
69*700637cbSDimitry Andric // - To store the sum, allowing a binary search on the data.
70*700637cbSDimitry Andric // - Avoid acquiring a mutex.
71*700637cbSDimitry Andric // The disadvantage are:
72*700637cbSDimitry Andric // - A slightly larger code size.
73*700637cbSDimitry Andric //
74*700637cbSDimitry Andric // There are two optimization directions
75*700637cbSDimitry Andric // - hard-code the database and do a linear search for future entries. This
76*700637cbSDimitry Andric // search can start at the back, and should probably contain very few
77*700637cbSDimitry Andric // entries. (Adding leap seconds is quite rare and new release of libc++
78*700637cbSDimitry Andric // can add the new entries; they are announced half a year before they are
79*700637cbSDimitry Andric // added.)
80*700637cbSDimitry Andric // - During parsing the leap seconds store an additional database in the
81*700637cbSDimitry Andric // dylib with the list of the sum of the leap seconds. In that case there
82*700637cbSDimitry Andric // can be a private function __get_utc_to_sys_table that returns the
83*700637cbSDimitry Andric // table.
84*700637cbSDimitry Andric //
85*700637cbSDimitry Andric // Note for to_sys there are no optimizations to be done; it uses
86*700637cbSDimitry Andric // get_leap_second_info. The function get_leap_second_info could benefit
87*700637cbSDimitry Andric // from optimizations as described above; again both options apply.
88*700637cbSDimitry Andric
89*700637cbSDimitry Andric // Both UTC and the system clock use the same epoch. The Standard
90*700637cbSDimitry Andric // specifies from 1970-01-01 even when UTC starts at
91*700637cbSDimitry Andric // 1972-01-01 00:00:10 TAI. So when the sys_time is before epoch we can be
92*700637cbSDimitry Andric // sure there both clocks return the same value.
93*700637cbSDimitry Andric
94*700637cbSDimitry Andric const tzdb& __tzdb = chrono::get_tzdb();
95*700637cbSDimitry Andric _Rp __result{__time.time_since_epoch()};
96*700637cbSDimitry Andric for (const auto& __leap_second : __tzdb.leap_seconds) {
97*700637cbSDimitry Andric if (__leap_second > __time)
98*700637cbSDimitry Andric return __result;
99*700637cbSDimitry Andric
100*700637cbSDimitry Andric __result += __leap_second.value();
101*700637cbSDimitry Andric }
102*700637cbSDimitry Andric return __result;
103*700637cbSDimitry Andric }
104*700637cbSDimitry Andric };
105*700637cbSDimitry Andric
106*700637cbSDimitry Andric struct leap_second_info {
107*700637cbSDimitry Andric bool is_leap_second;
108*700637cbSDimitry Andric seconds elapsed;
109*700637cbSDimitry Andric };
110*700637cbSDimitry Andric
111*700637cbSDimitry Andric template <class _Duration>
get_leap_second_info(const utc_time<_Duration> & __time)112*700637cbSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI leap_second_info get_leap_second_info(const utc_time<_Duration>& __time) {
113*700637cbSDimitry Andric const tzdb& __tzdb = chrono::get_tzdb();
114*700637cbSDimitry Andric if (__tzdb.leap_seconds.empty()) [[unlikely]]
115*700637cbSDimitry Andric return {false, chrono::seconds{0}};
116*700637cbSDimitry Andric
117*700637cbSDimitry Andric sys_seconds __sys{chrono::floor<seconds>(__time).time_since_epoch()};
118*700637cbSDimitry Andric seconds __elapsed{0};
119*700637cbSDimitry Andric for (const auto& __leap_second : __tzdb.leap_seconds) {
120*700637cbSDimitry Andric if (__sys == __leap_second.date() + __elapsed)
121*700637cbSDimitry Andric // A time point may only be a leap second during a positive leap second
122*700637cbSDimitry Andric // insertion, since time points that occur during a (theoretical)
123*700637cbSDimitry Andric // negative leap second don't exist.
124*700637cbSDimitry Andric return {__leap_second.value() > 0s, __elapsed + __leap_second.value()};
125*700637cbSDimitry Andric
126*700637cbSDimitry Andric if (__sys < __leap_second.date() + __elapsed)
127*700637cbSDimitry Andric return {false, __elapsed};
128*700637cbSDimitry Andric
129*700637cbSDimitry Andric __elapsed += __leap_second.value();
130*700637cbSDimitry Andric }
131*700637cbSDimitry Andric
132*700637cbSDimitry Andric return {false, __elapsed};
133*700637cbSDimitry Andric }
134*700637cbSDimitry Andric
135*700637cbSDimitry Andric template <class _Duration>
136*700637cbSDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI sys_time<common_type_t<_Duration, seconds>>
to_sys(const utc_time<_Duration> & __time)137*700637cbSDimitry Andric utc_clock::to_sys(const utc_time<_Duration>& __time) {
138*700637cbSDimitry Andric using _Dp = common_type_t<_Duration, seconds>;
139*700637cbSDimitry Andric leap_second_info __info = chrono::get_leap_second_info(__time);
140*700637cbSDimitry Andric
141*700637cbSDimitry Andric // [time.clock.utc.members]/2
142*700637cbSDimitry Andric // Returns: A sys_time t, such that from_sys(t) == u if such a mapping
143*700637cbSDimitry Andric // exists. Otherwise u represents a time_point during a positive leap
144*700637cbSDimitry Andric // second insertion, the conversion counts that leap second as not
145*700637cbSDimitry Andric // inserted, and the last representable value of sys_time prior to the
146*700637cbSDimitry Andric // insertion of the leap second is returned.
147*700637cbSDimitry Andric sys_time<common_type_t<_Duration, seconds>> __result{__time.time_since_epoch() - __info.elapsed};
148*700637cbSDimitry Andric if (__info.is_leap_second)
149*700637cbSDimitry Andric return chrono::floor<seconds>(__result) + chrono::seconds{1} - _Dp{1};
150*700637cbSDimitry Andric
151*700637cbSDimitry Andric return __result;
152*700637cbSDimitry Andric }
153*700637cbSDimitry Andric
154*700637cbSDimitry Andric } // namespace chrono
155*700637cbSDimitry Andric
156*700637cbSDimitry Andric # endif // _LIBCPP_STD_VER >= 20 && _LIBCPP_HAS_TIME_ZONE_DATABASE && _LIBCPP_HAS_FILESYSTEM &&
157*700637cbSDimitry Andric // _LIBCPP_HAS_LOCALIZATION
158*700637cbSDimitry Andric
159*700637cbSDimitry Andric _LIBCPP_END_NAMESPACE_STD
160*700637cbSDimitry Andric
161*700637cbSDimitry Andric #endif // _LIBCPP_HAS_EXPERIMENTAL_TZDB
162*700637cbSDimitry Andric
163*700637cbSDimitry Andric #endif // _LIBCPP___CHRONO_UTC_CLOCK_H
164