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