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// UNSUPPORTED: c++03, c++11, c++14, c++17
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 utc_clock;
18
19// template<class Duration>
20// leap_second_info get_leap_second_info(const utc_time<Duration>& ut);
21
22#include <chrono>
23#include <cassert>
24
25#include "test_macros.h"
26#include "assert_macros.h"
27#include "concat_macros.h"
28
29template <class Duration>
30static void test_leap_second_info(
31 std::chrono::time_point<std::chrono::utc_clock, Duration> time, bool is_leap_second, std::chrono::seconds elapsed) {
32 std::chrono::leap_second_info result = std::chrono::get_leap_second_info(time);
33 TEST_REQUIRE(
34 result.is_leap_second == is_leap_second && result.elapsed == elapsed,
35 TEST_WRITE_CONCATENATED(
36 "\nExpected output [",
37 is_leap_second,
38 ", ",
39 elapsed,
40 "]\nActual output [",
41 result.is_leap_second,
42 ", ",
43 result.elapsed,
44 "]\n"));
45}
46
47static std::chrono::utc_seconds get_utc_time(long long seconds_since_1900) {
48 // The file leap-seconds.list stores dates since 1 January 1900, 00:00:00, we want
49 // seconds since 1 January 1970.
50 constexpr auto offset =
51 std::chrono::sys_days{std::chrono::January / 1 / 1970} - std::chrono::sys_days{std::chrono::January / 1 / 1900};
52 return std::chrono::utc_seconds{std::chrono::seconds{seconds_since_1900} - offset};
53}
54
55// Tests set of existing database entries at the time of writing.
56int main(int, const char**) {
57 using namespace std::literals::chrono_literals;
58
59 test_leap_second_info(std::chrono::utc_seconds::min(), false, 0s);
60
61 // Epoch transition no transitions.
62 test_leap_second_info(std::chrono::utc_seconds{-1s}, false, 0s);
63 test_leap_second_info(std::chrono::utc_seconds{0s}, false, 0s);
64 test_leap_second_info(std::chrono::utc_seconds{1s}, false, 0s);
65
66 // Transitions from the start of UTC.
67 auto test_transition = [](std::chrono::utc_seconds time, std::chrono::seconds elapsed, bool positive) {
68 // Note at the time of writing all leap seconds are positive so the else
69 // branch is never executed. The private test for this function tests
70 // negative leap seconds and uses the else branch.
71
72 if (positive) {
73 // Every transition has the following tests
74 // - 1ns before the start of the transition is_leap_second -> false, elapsed -> elapsed
75 // - at the start of the transition is_leap_second -> true, elapsed -> elapsed + 1
76 // - 1ns after the start of the transition is_leap_second -> true, elapsed -> elapsed + 1
77 // - 1ns before the end of the transition is_leap_second -> true, elapsed -> elapsed + 1
78 // - at the end of the transition is_leap_second -> false, elapsed -> elapsed + 1
79
80 test_leap_second_info(time - 1ns, false, elapsed);
81 test_leap_second_info(time, true, elapsed + 1s);
82 test_leap_second_info(time + 1ns, true, elapsed + 1s);
83 test_leap_second_info(time + 1s - 1ns, true, elapsed + 1s);
84 test_leap_second_info(time + 1s, false, elapsed + 1s);
85 } else {
86 // Every transition has the following tests
87 // - 1ns before the transition is_leap_second -> false, elapsed -> elapsed
88 // - at the transition is_leap_second -> false elapsed -> elapsed - 1
89 // - 1ns after the transition is_leap_second -> false, elapsed -> elapsed - 1
90 test_leap_second_info(time - 1ns, false, elapsed);
91 test_leap_second_info(time, false, elapsed - 1s);
92 test_leap_second_info(time + 1ns, false, elapsed - 1s);
93 }
94 };
95
96 // The timestamps are from leap-seconds.list in the IANA database.
97 // Note the times stamps are timestamps without leap seconds so the number
98 // here are incremented by x "leap seconds".
99 test_transition(get_utc_time(2287785600 + 0), 0s, true); // 1 Jul 1972
100 test_transition(get_utc_time(2303683200 + 1), 1s, true); // 1 Jan 1973
101 test_transition(get_utc_time(2335219200 + 2), 2s, true); // 1 Jan 1974
102 test_transition(get_utc_time(2366755200 + 3), 3s, true); // 1 Jan 1975
103 test_transition(get_utc_time(2398291200 + 4), 4s, true); // 1 Jan 1976
104 test_transition(get_utc_time(2429913600 + 5), 5s, true); // 1 Jan 1977
105 test_transition(get_utc_time(2461449600 + 6), 6s, true); // 1 Jan 1978
106 test_transition(get_utc_time(2492985600 + 7), 7s, true); // 1 Jan 1979
107 test_transition(get_utc_time(2524521600 + 8), 8s, true); // 1 Jan 1980
108 test_transition(get_utc_time(2571782400 + 9), 9s, true); // 1 Jul 1981
109 test_transition(get_utc_time(2603318400 + 10), 10s, true); // 1 Jul 1982
110 test_transition(get_utc_time(2634854400 + 11), 11s, true); // 1 Jul 1983
111 test_transition(get_utc_time(2698012800 + 12), 12s, true); // 1 Jul 1985
112 test_transition(get_utc_time(2776982400 + 13), 13s, true); // 1 Jan 1988
113 test_transition(get_utc_time(2840140800 + 14), 14s, true); // 1 Jan 1990
114 test_transition(get_utc_time(2871676800 + 15), 15s, true); // 1 Jan 1991
115 test_transition(get_utc_time(2918937600 + 16), 16s, true); // 1 Jul 1992
116 test_transition(get_utc_time(2950473600 + 17), 17s, true); // 1 Jul 1993
117 test_transition(get_utc_time(2982009600 + 18), 18s, true); // 1 Jul 1994
118 test_transition(get_utc_time(3029443200 + 19), 19s, true); // 1 Jan 1996
119 test_transition(get_utc_time(3076704000 + 20), 20s, true); // 1 Jul 1997
120 test_transition(get_utc_time(3124137600 + 21), 21s, true); // 1 Jan 1999
121 test_transition(get_utc_time(3345062400 + 22), 22s, true); // 1 Jan 2006
122 test_transition(get_utc_time(3439756800 + 23), 23s, true); // 1 Jan 2009
123 test_transition(get_utc_time(3550089600 + 24), 24s, true); // 1 Jul 2012
124 test_transition(get_utc_time(3644697600 + 25), 25s, true); // 1 Jul 2015
125 test_transition(get_utc_time(3692217600 + 26), 26s, true); // 1 Jan 2017
126
127 return 0;
128}
129

source code of libcxx/test/std/time/time.clock/time.clock.utc/get_leap_second_info.pass.cpp