1 | //===----------------------------------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | // REQUIRES: std-at-least-c++20 |
10 | // UNSUPPORTED: no-filesystem, no-localization, no-tzdb |
11 | |
12 | // XFAIL: libcpp-has-no-experimental-tzdb |
13 | // XFAIL: availability-tzdb-missing |
14 | |
15 | // <chrono> |
16 | // |
17 | // class gps_clock; |
18 | |
19 | // static gps_time<common_type_t<_Duration, seconds>> |
20 | // from_utc(const utc<_Duration>& __time) noexcept; |
21 | |
22 | #include <chrono> |
23 | #include <cassert> |
24 | #include <source_location> |
25 | |
26 | #include "test_macros.h" |
27 | #include "assert_macros.h" |
28 | #include "concat_macros.h" |
29 | |
30 | static void test_known_values() { |
31 | namespace cr = std::chrono; |
32 | using namespace std::literals::chrono_literals; |
33 | |
34 | // [time.clock.gps.overview]/1 |
35 | // The clock gps_clock measures seconds since the first Sunday of January, |
36 | // 1980 00:00:00 UTC. |
37 | // The first Sunday is 1980-1-6 (so January sixth) |
38 | // ... 1980-01-06 00:00:00 GPS is equivalent to 1980-01-06 00:00:00 UTC |
39 | |
40 | assert(cr::gps_clock::from_utc(cr::utc_clock::from_sys(cr::sys_days{cr::January / 6 / 1980})) == cr::gps_seconds{0s}); |
41 | } |
42 | |
43 | template <class Duration> |
44 | static void test_leap_seconds(std::chrono::utc_time<Duration> utc, |
45 | std::chrono::gps_time<Duration> expected, |
46 | std::source_location loc = std::source_location::current()) { |
47 | auto gps = std::chrono::gps_clock::from_utc(utc); |
48 | TEST_REQUIRE(gps == expected, |
49 | TEST_WRITE_CONCATENATED(loc, "\nExpected output " , expected, "\nActual output " , gps, '\n')); |
50 | } |
51 | |
52 | // Tests set if existing database entries at the time of writing. |
53 | static void test_transitions() { |
54 | using namespace std::literals::chrono_literals; |
55 | namespace cr = std::chrono; |
56 | |
57 | // "sys" is the time of the transition to the next leap second. |
58 | // "elapsed" is the number of leap seconds before the transition. |
59 | auto test_transition = [](cr::sys_days sys, cr::seconds elapsed) { |
60 | constexpr auto unix_to_gps_epoch_offset = |
61 | cr::sys_days{cr::January / 1 / 1970} - cr::sys_days{cr::January / 6 / 1980}; |
62 | cr::gps_seconds gps{sys.time_since_epoch() + unix_to_gps_epoch_offset + elapsed}; |
63 | |
64 | test_leap_seconds(cr::utc_clock::from_sys(sys - 1ns), gps - 1ns); |
65 | test_leap_seconds(cr::utc_clock::from_sys(sys), gps + 1s); |
66 | test_leap_seconds(cr::utc_clock::from_sys(sys) + 1ns, gps + 1s + 1ns); |
67 | }; |
68 | |
69 | // Transitions from the start of UTC. |
70 | test_transition(cr::sys_days{cr::July / 1 / 1972}, -9s); |
71 | test_transition(cr::sys_days{cr::January / 1 / 1973}, -8s); |
72 | test_transition(cr::sys_days{cr::January / 1 / 1974}, -7s); |
73 | test_transition(cr::sys_days{cr::January / 1 / 1975}, -6s); |
74 | test_transition(cr::sys_days{cr::January / 1 / 1976}, -5s); |
75 | test_transition(cr::sys_days{cr::January / 1 / 1977}, -4s); |
76 | test_transition(cr::sys_days{cr::January / 1 / 1978}, -3s); |
77 | test_transition(cr::sys_days{cr::January / 1 / 1979}, -2s); |
78 | test_transition(cr::sys_days{cr::January / 1 / 1980}, -1s); |
79 | test_transition(cr::sys_days{cr::July / 1 / 1981}, 0s); |
80 | test_transition(cr::sys_days{cr::July / 1 / 1982}, 1s); |
81 | test_transition(cr::sys_days{cr::July / 1 / 1983}, 2s); |
82 | test_transition(cr::sys_days{cr::July / 1 / 1985}, 3s); |
83 | test_transition(cr::sys_days{cr::January / 1 / 1988}, 4s); |
84 | test_transition(cr::sys_days{cr::January / 1 / 1990}, 5s); |
85 | test_transition(cr::sys_days{cr::January / 1 / 1991}, 6s); |
86 | test_transition(cr::sys_days{cr::July / 1 / 1992}, 7s); |
87 | test_transition(cr::sys_days{cr::July / 1 / 1993}, 8s); |
88 | test_transition(cr::sys_days{cr::July / 1 / 1994}, 9s); |
89 | test_transition(cr::sys_days{cr::January / 1 / 1996}, 10s); |
90 | test_transition(cr::sys_days{cr::July / 1 / 1997}, 11s); |
91 | test_transition(cr::sys_days{cr::January / 1 / 1999}, 12s); |
92 | test_transition(cr::sys_days{cr::January / 1 / 2006}, 13s); |
93 | test_transition(cr::sys_days{cr::January / 1 / 2009}, 14s); |
94 | test_transition(cr::sys_days{cr::July / 1 / 2012}, 15s); |
95 | test_transition(cr::sys_days{cr::July / 1 / 2015}, 16s); |
96 | test_transition(cr::sys_days{cr::January / 1 / 2017}, 17s); |
97 | } |
98 | |
99 | // Tests whether the return type is the expected type. |
100 | static void test_return_type() { |
101 | using namespace std::literals::chrono_literals; |
102 | namespace cr = std::chrono; |
103 | |
104 | { |
105 | [[maybe_unused]] std::same_as<cr::gps_time<cr::nanoseconds>> decltype(auto) _ = |
106 | cr::gps_clock::from_utc(cr::utc_time<cr::nanoseconds>{0ns}); |
107 | } |
108 | { |
109 | [[maybe_unused]] std::same_as<cr::gps_time<cr::microseconds>> decltype(auto) _ = |
110 | cr::gps_clock::from_utc(cr::utc_time<cr::microseconds>{0us}); |
111 | } |
112 | { |
113 | [[maybe_unused]] std::same_as<cr::gps_time<cr::milliseconds>> decltype(auto) _ = |
114 | cr::gps_clock::from_utc(cr::utc_time<cr::milliseconds>{0ms}); |
115 | } |
116 | |
117 | { |
118 | [[maybe_unused]] std::same_as<cr::gps_time<cr::seconds>> decltype(auto) _ = |
119 | cr::gps_clock::from_utc(cr::utc_time<cr::seconds>{cr::seconds{0}}); |
120 | } |
121 | |
122 | { |
123 | [[maybe_unused]] std::same_as<cr::gps_time<cr::seconds>> decltype(auto) _ = |
124 | cr::gps_clock::from_utc(cr::utc_time<cr::minutes>{cr::minutes{0}}); |
125 | } |
126 | { |
127 | [[maybe_unused]] std::same_as<cr::gps_time<cr::seconds>> decltype(auto) _ = |
128 | cr::gps_clock::from_utc(cr::utc_time<cr::hours>{cr::hours{0}}); |
129 | } |
130 | { |
131 | [[maybe_unused]] std::same_as<cr::gps_time<cr::seconds>> decltype(auto) _ = |
132 | cr::gps_clock::from_utc(cr::utc_time<cr::days>{cr::days{0}}); |
133 | } |
134 | { |
135 | [[maybe_unused]] std::same_as<cr::gps_time<cr::seconds>> decltype(auto) _ = |
136 | cr::gps_clock::from_utc(cr::utc_time<cr::weeks>{cr::weeks{0}}); |
137 | } |
138 | { |
139 | [[maybe_unused]] std::same_as<cr::gps_time<cr::seconds>> decltype(auto) _ = |
140 | cr::gps_clock::from_utc(cr::utc_time<cr::months>{cr::months{0}}); |
141 | } |
142 | { |
143 | [[maybe_unused]] std::same_as<cr::gps_time<cr::seconds>> decltype(auto) _ = |
144 | cr::gps_clock::from_utc(cr::utc_time<cr::years>{cr::years{0}}); |
145 | } |
146 | } |
147 | |
148 | int main(int, const char**) { |
149 | using namespace std::literals::chrono_literals; |
150 | std::chrono::utc_seconds time = std::chrono::utc_seconds{0s}; |
151 | static_assert(noexcept(std::chrono::gps_clock::from_utc(time))); |
152 | |
153 | test_known_values(); |
154 | test_transitions(); |
155 | test_return_type(); |
156 | |
157 | return 0; |
158 | } |
159 | |