Skip to content

Commit 9977622

Browse files
committed
[libc++][chrono] implements UTC clock.
While implementing this feature and its associated LWG issues it turns out - LWG3316 Correctly define epoch for utc_clock / utc_timepoint only added non-normative wording to the standard. Implements parts of: - P0355 Extending <chrono> to Calendars and Time Zones - P1361 Integration of chrono with text formatting - LWG3359 <chrono> leap second support should allow for negative leap seconds
1 parent f5b9e11 commit 9977622

File tree

26 files changed

+2459
-6
lines changed

26 files changed

+2459
-6
lines changed

libcxx/benchmarks/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ set(BENCHMARK_TESTS
231231
system_error.bench.cpp
232232
to_chars.bench.cpp
233233
unordered_set_operations.bench.cpp
234+
utc_clock.bench.cpp
234235
util_smartptr.bench.cpp
235236
variant_visit_1.bench.cpp
236237
variant_visit_2.bench.cpp

libcxx/benchmarks/utc_clock.bench.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//===----------------------------------------------------------------------===//
2+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3+
// See https://llvm.org/LICENSE.txt for license information.
4+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
//
6+
//===----------------------------------------------------------------------===//
7+
8+
#include <chrono>
9+
10+
#include "benchmark/benchmark.h"
11+
12+
// Benchmarks the performance of the UTC <-> system time conversions. These
13+
// operations determine the sum of leap second insertions at a specific time.
14+
15+
static void BM_from_sys(benchmark::State& state) {
16+
std::chrono::sys_days date{std::chrono::July / 1 / state.range(0)};
17+
for (auto _ : state)
18+
benchmark::DoNotOptimize(std::chrono::utc_clock::from_sys(date));
19+
}
20+
21+
BENCHMARK(BM_from_sys)
22+
->Arg(1970) // before the first leap seconds
23+
->Arg(1979) // in the first half of inserted leap seconds
24+
->Arg(1993) // in the second half of inserted leap seconds
25+
->Arg(2100); // after the last leap second
26+
27+
BENCHMARK(BM_from_sys)->Arg(1970)->Arg(1979)->Arg(1993)->Arg(2100)->Threads(4);
28+
BENCHMARK(BM_from_sys)->Arg(1970)->Arg(1979)->Arg(1993)->Arg(2100)->Threads(16);
29+
30+
static void BM_to_sys(benchmark::State& state) {
31+
// 59 sec offset means we pass th UTC offset for the leap second; assuming
32+
// there won't be more than 59 leap seconds ever.
33+
std::chrono::utc_seconds date{
34+
std::chrono::sys_days{std::chrono::July / 1 / state.range(0)}.time_since_epoch() + std::chrono::seconds{59}};
35+
for (auto _ : state)
36+
benchmark::DoNotOptimize(std::chrono::utc_clock::to_sys(date));
37+
}
38+
39+
BENCHMARK(BM_to_sys)
40+
->Arg(1970) // before the first leap seconds
41+
->Arg(1979) // in the first half of inserted leap seconds
42+
->Arg(1993) // in the second half of inserted leap seconds
43+
->Arg(2100); // after the last leap second
44+
45+
BENCHMARK(BM_to_sys)->Arg(1970)->Arg(1979)->Arg(1993)->Arg(2100)->Threads(4);
46+
BENCHMARK(BM_to_sys)->Arg(1970)->Arg(1979)->Arg(1993)->Arg(2100)->Threads(16);
47+
48+
int main(int argc, char** argv) {
49+
benchmark::Initialize(&argc, argv);
50+
if (benchmark::ReportUnrecognizedArguments(argc, argv))
51+
return 1;
52+
53+
benchmark::RunSpecifiedBenchmarks();
54+
}

libcxx/docs/Status/Cxx20.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ Paper Status
5454
* ``Input parsers`` not done
5555
* ``Stream output`` Obsolete due to `P1361R2 <https://wg21.link/P1361R2>`_ "Integration of chrono with text formatting"
5656
* ``Time zone and leap seconds`` In Progress
57+
* ``UTC clock`` Clang 19
5758
* ``TAI clock`` not done
5859
* ``GPS clock`` not done
59-
* ``UTC clock`` not done
6060
6161
.. _issues-status-cxx20:
6262

libcxx/docs/Status/Cxx20Issues.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@
238238
"`3313 <https://wg21.link/LWG3313>`__","``join_view::iterator::operator--``\ is incorrectly constrained","Prague","|Complete|","14.0","|ranges|"
239239
"`3314 <https://wg21.link/LWG3314>`__","Is stream insertion behavior locale dependent when ``Period::type``\ is ``micro``\ ?","Prague","|Complete|","16.0","|chrono|"
240240
"`3315 <https://wg21.link/LWG3315>`__","Correct Allocator Default Behavior","Prague","",""
241-
"`3316 <https://wg21.link/LWG3316>`__","Correctly define epoch for ``utc_clock``\ / ``utc_timepoint``\ ","Prague","","","|chrono|"
241+
"`3316 <https://wg21.link/LWG3316>`__","Correctly define epoch for ``utc_clock``\ / ``utc_timepoint``\ ","Prague","|Nothing To Do|","","|chrono|"
242242
"`3317 <https://wg21.link/LWG3317>`__","Incorrect ``operator<<``\ for floating-point durations","Prague","","","|chrono|"
243243
"`3318 <https://wg21.link/LWG3318>`__","Clarify whether clocks can represent time before their epoch","Prague","","","|chrono|"
244244
"`3319 <https://wg21.link/LWG3319>`__","Properly reference specification of IANA time zone database","Prague","|Nothing To Do|","","|chrono|"

libcxx/docs/Status/FormatPaper.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Section,Description,Dependencies,Assignee,Status,First released version
22
`P1361 <https://wg21.link/P1361>`__ `P2372 <https://wg21.link/P2372>`__,"Formatting chrono"
33
`[time.syn] <https://wg21.link/time.syn>`_,"Formatter ``chrono::duration<Rep, Period>``",,Mark de Wever,|Complete|,16.0
44
`[time.syn] <https://wg21.link/time.syn>`_,"Formatter ``chrono::sys_time<Duration>``",,Mark de Wever,|Complete|,17.0
5-
`[time.syn] <https://wg21.link/time.syn>`_,"Formatter ``chrono::utc_time<Duration>``",A ``<chrono>`` implementation,Mark de Wever,,,
5+
`[time.syn] <https://wg21.link/time.syn>`_,"Formatter ``chrono::utc_time<Duration>``",,Mark de Wever,|Complete|,19.0
66
`[time.syn] <https://wg21.link/time.syn>`_,"Formatter ``chrono::tai_time<Duration>``",A ``<chrono>`` implementation,Mark de Wever,,,
77
`[time.syn] <https://wg21.link/time.syn>`_,"Formatter ``chrono::gps_time<Duration>``",A ``<chrono>`` implementation,Mark de Wever,,,
88
`[time.syn] <https://wg21.link/time.syn>`_,"Formatter ``chrono::file_time<Duration>``",,Mark de Wever,|Complete|,17.0

libcxx/include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ set(files
271271
__chrono/time_zone_link.h
272272
__chrono/tzdb.h
273273
__chrono/tzdb_list.h
274+
__chrono/utc_clock.h
274275
__chrono/weekday.h
275276
__chrono/year.h
276277
__chrono/year_month.h

libcxx/include/__chrono/convert_to_tm.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <__chrono/sys_info.h>
2525
#include <__chrono/system_clock.h>
2626
#include <__chrono/time_point.h>
27+
#include <__chrono/utc_clock.h>
2728
#include <__chrono/weekday.h>
2829
#include <__chrono/year.h>
2930
#include <__chrono/year_month.h>
@@ -96,6 +97,25 @@ _LIBCPP_HIDE_FROM_ABI _Tm __convert_to_tm(const chrono::sys_time<_Duration> __tp
9697
return __result;
9798
}
9899

100+
# if !defined(_LIBCPP_HAS_NO_TIME_ZONE_DATABASE) && !defined(_LIBCPP_HAS_NO_FILESYSTEM) && \
101+
!defined(_LIBCPP_HAS_NO_LOCALIZATION)
102+
# if !defined(_LIBCPP_HAS_NO_INCOMPLETE_TZDB)
103+
104+
template <class _Tm, class _Duration>
105+
_LIBCPP_HIDE_FROM_ABI _Tm __convert_to_tm(chrono::utc_time<_Duration> __tp) {
106+
_Tm __result = std::__convert_to_tm<_Tm>(chrono::utc_clock::to_sys(__tp));
107+
108+
if (chrono::get_leap_second_info(__tp).is_leap_second)
109+
++__result.tm_sec;
110+
111+
return __result;
112+
}
113+
114+
# endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_TZDB)
115+
116+
# endif // !defined(_LIBCPP_HAS_NO_TIME_ZONE_DATABASE) && !defined(_LIBCPP_HAS_NO_FILESYSTEM) &&
117+
// !defined(_LIBCPP_HAS_NO_LOCALIZATION)
118+
99119
// Convert a chrono (calendar) time point, or dururation to the given _Tm type,
100120
// which must have the same properties as std::tm.
101121
template <class _Tm, class _ChronoT>
@@ -108,6 +128,8 @@ _LIBCPP_HIDE_FROM_ABI _Tm __convert_to_tm(const _ChronoT& __value) {
108128
if constexpr (__is_time_point<_ChronoT>) {
109129
if constexpr (same_as<typename _ChronoT::clock, chrono::system_clock>)
110130
return std::__convert_to_tm<_Tm>(__value);
131+
else if constexpr (same_as<typename _ChronoT::clock, chrono::utc_clock>)
132+
return std::__convert_to_tm<_Tm>(__value);
111133
else if constexpr (same_as<typename _ChronoT::clock, chrono::file_clock>)
112134
return std::__convert_to_tm<_Tm>(_ChronoT::clock::to_sys(__value));
113135
else if constexpr (same_as<typename _ChronoT::clock, chrono::local_t>)

libcxx/include/__chrono/formatter.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <__chrono/sys_info.h>
2929
#include <__chrono/system_clock.h>
3030
#include <__chrono/time_point.h>
31+
#include <__chrono/utc_clock.h>
3132
#include <__chrono/weekday.h>
3233
#include <__chrono/year.h>
3334
#include <__chrono/year_month.h>
@@ -673,6 +674,23 @@ struct _LIBCPP_TEMPLATE_VIS formatter<chrono::sys_time<_Duration>, _CharT> : pub
673674
}
674675
};
675676

677+
# if !defined(_LIBCPP_HAS_NO_TIME_ZONE_DATABASE) && !defined(_LIBCPP_HAS_NO_FILESYSTEM) && \
678+
!defined(_LIBCPP_HAS_NO_LOCALIZATION)
679+
680+
template <class _Duration, __fmt_char_type _CharT>
681+
struct _LIBCPP_TEMPLATE_VIS formatter<chrono::utc_time<_Duration>, _CharT> : public __formatter_chrono<_CharT> {
682+
public:
683+
using _Base = __formatter_chrono<_CharT>;
684+
685+
template <class _ParseContext>
686+
_LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
687+
return _Base::__parse(__ctx, __format_spec::__fields_chrono, __format_spec::__flags::__clock);
688+
}
689+
};
690+
691+
# endif // !defined(_LIBCPP_HAS_NO_TIME_ZONE_DATABASE) && !defined(_LIBCPP_HAS_NO_FILESYSTEM) &&
692+
// !defined(_LIBCPP_HAS_NO_LOCALIZATION)
693+
676694
template <class _Duration, __fmt_char_type _CharT>
677695
struct _LIBCPP_TEMPLATE_VIS formatter<chrono::file_time<_Duration>, _CharT> : public __formatter_chrono<_CharT> {
678696
public:

libcxx/include/__chrono/ostream.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <__chrono/statically_widen.h>
2323
#include <__chrono/sys_info.h>
2424
#include <__chrono/system_clock.h>
25+
#include <__chrono/utc_clock.h>
2526
#include <__chrono/weekday.h>
2627
#include <__chrono/year.h>
2728
#include <__chrono/year_month.h>
@@ -56,6 +57,17 @@ operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_days& __dp) {
5657
return __os << year_month_day{__dp};
5758
}
5859

60+
# if !defined(_LIBCPP_HAS_NO_TIME_ZONE_DATABASE) && !defined(_LIBCPP_HAS_NO_FILESYSTEM) && \
61+
!defined(_LIBCPP_HAS_NO_LOCALIZATION)
62+
63+
template <class _CharT, class _Traits, class _Duration>
64+
_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
65+
operator<<(basic_ostream<_CharT, _Traits>& __os, const utc_time<_Duration>& __tp) {
66+
return __os << std::format(__os.getloc(), _LIBCPP_STATICALLY_WIDEN(_CharT, "{:L%F %T}"), __tp);
67+
}
68+
# endif // !defined(_LIBCPP_HAS_NO_TIME_ZONE_DATABASE) && !defined(_LIBCPP_HAS_NO_FILESYSTEM) &&
69+
// !defined(_LIBCPP_HAS_NO_LOCALIZATION)
70+
5971
template <class _CharT, class _Traits, class _Duration>
6072
_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
6173
operator<<(basic_ostream<_CharT, _Traits>& __os, const file_time<_Duration> __tp) {

libcxx/include/__chrono/utc_clock.h

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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 !defined(_LIBCPP_HAS_NO_INCOMPLETE_TZDB)
16+
17+
# include <__chrono/duration.h>
18+
# include <__chrono/system_clock.h>
19+
# include <__chrono/time_point.h>
20+
# include <__chrono/tzdb.h>
21+
# include <__chrono/tzdb_list.h>
22+
# include <__config>
23+
# include <__type_traits/common_type.h>
24+
25+
# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26+
# pragma GCC system_header
27+
# endif
28+
29+
_LIBCPP_BEGIN_NAMESPACE_STD
30+
31+
# if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_TIME_ZONE_DATABASE) && !defined(_LIBCPP_HAS_NO_FILESYSTEM) && \
32+
!defined(_LIBCPP_HAS_NO_LOCALIZATION)
33+
34+
namespace chrono {
35+
36+
class utc_clock;
37+
38+
template <class _Duration>
39+
using utc_time = time_point<utc_clock, _Duration>;
40+
using utc_seconds = utc_time<seconds>;
41+
42+
class utc_clock {
43+
public:
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]] _LIBCPP_HIDE_FROM_ABI static time_point now() { return from_sys(system_clock::now()); }
51+
52+
template <class _Duration>
53+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI static sys_time<common_type_t<_Duration, seconds>>
54+
to_sys(const utc_time<_Duration>& __time);
55+
56+
template <class _Duration>
57+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI static utc_time<common_type_t<_Duration, seconds>>
58+
from_sys(const sys_time<_Duration>& __time) {
59+
using _Rp = utc_time<common_type_t<_Duration, seconds>>;
60+
// TODO TZDB investigate optimizations.
61+
//
62+
// The leap second database stores all transitions, this mean to calculate
63+
// the current number of leap seconds the code needs to iterate over all
64+
// leap seconds to accumulate the sum. Then the sum can be used to determine
65+
// the sys_time. Accessing the database involves acquiring a mutex.
66+
//
67+
// The historic entries in the database are immutable. Hard-coding these
68+
// values in a table would allow:
69+
// - To store the sum, allowing a binary search on the data.
70+
// - Avoid acquiring a mutex.
71+
// The disadvantage are:
72+
// - A slightly larger code size.
73+
//
74+
// There are two optimization directions
75+
// - hard-code the database and do a linear search for future entries. This
76+
// search can start at the back, and should probably contain very few
77+
// entries. (Adding leap seconds is quite rare and new release of libc++
78+
// can add the new entries; they are announced half a year before they are
79+
// added.)
80+
// - During parsing the leap seconds store an additional database in the
81+
// dylib with the list of the sum of the leap seconds. In that case there
82+
// can be a private function __get_utc_to_sys_table that returns the
83+
// table.
84+
//
85+
// Note for to_sys there are no optimizations to be done; it uses
86+
// get_leap_second_info. The function get_leap_second_info could benefit
87+
// from optimizations as described above; again both options apply.
88+
89+
// Both UTC and the system clock use the same epoch. The Standard
90+
// specifies from 1970-01-01 even when UTC starts at
91+
// 1972-01-01 00:00:10 TAI. So when the sys_time is before epoch we can be
92+
// sure there both clocks return the same value.
93+
94+
const tzdb& __tzdb = chrono::get_tzdb();
95+
_Rp __result{__time.time_since_epoch()};
96+
for (const auto& __leap_second : __tzdb.leap_seconds) {
97+
if (__time < __leap_second)
98+
return __result;
99+
100+
__result += __leap_second.value();
101+
}
102+
return __result;
103+
}
104+
};
105+
106+
struct leap_second_info {
107+
bool is_leap_second;
108+
seconds elapsed;
109+
};
110+
111+
template <class _Duration>
112+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI leap_second_info get_leap_second_info(const utc_time<_Duration>& __time) {
113+
const tzdb& __tzdb = chrono::get_tzdb();
114+
if (__tzdb.leap_seconds.empty())
115+
return {false, chrono::seconds{0}};
116+
117+
sys_seconds __sys{chrono::floor<seconds>(__time).time_since_epoch()};
118+
seconds __elapsed{0};
119+
for (const auto& __leap_second : __tzdb.leap_seconds) {
120+
if (__sys == __leap_second.date() + __elapsed)
121+
return {__leap_second.value() > 0s, // only positive leap seconds are considered leap seconds
122+
__elapsed + __leap_second.value()};
123+
124+
if (__sys < __leap_second.date() + __elapsed)
125+
return {false, __elapsed};
126+
127+
__elapsed += __leap_second.value();
128+
}
129+
130+
return {false, __elapsed};
131+
}
132+
133+
template <class _Duration>
134+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI sys_time<common_type_t<_Duration, seconds>>
135+
utc_clock::to_sys(const utc_time<_Duration>& __time) {
136+
using _Dp = common_type_t<_Duration, seconds>;
137+
leap_second_info __info = get_leap_second_info(__time);
138+
139+
sys_time<common_type_t<_Duration, seconds>> __result{__time.time_since_epoch() - __info.elapsed};
140+
if (__info.is_leap_second)
141+
return chrono::floor<seconds>(__result) + chrono::seconds{1} - _Dp{1};
142+
143+
return __result;
144+
}
145+
146+
} // namespace chrono
147+
148+
# endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_TIME_ZONE_DATABASE) && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
149+
// && !defined(_LIBCPP_HAS_NO_LOCALIZATION)
150+
151+
_LIBCPP_END_NAMESPACE_STD
152+
153+
#endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_TZDB)
154+
155+
#endif // _LIBCPP___CHRONO_UTC_CLOCK_H

0 commit comments

Comments
 (0)