129 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++14, c++1710// UNSUPPORTED: no-filesystem, no-localization, no-tzdb11 12// XFAIL: libcpp-has-no-experimental-tzdb13// XFAIL: availability-tzdb-missing14 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 want49 // 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 else69 // branch is never executed. The private test for this function tests70 // negative leap seconds and uses the else branch.71 72 if (positive) {73 // Every transition has the following tests74 // - 1ns before the start of the transition is_leap_second -> false, elapsed -> elapsed75 // - at the start of the transition is_leap_second -> true, elapsed -> elapsed + 176 // - 1ns after the start of the transition is_leap_second -> true, elapsed -> elapsed + 177 // - 1ns before the end of the transition is_leap_second -> true, elapsed -> elapsed + 178 // - at the end of the transition is_leap_second -> false, elapsed -> elapsed + 179 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 tests87 // - 1ns before the transition is_leap_second -> false, elapsed -> elapsed88 // - at the transition is_leap_second -> false elapsed -> elapsed - 189 // - 1ns after the transition is_leap_second -> false, elapsed -> elapsed - 190 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 number98 // here are incremented by x "leap seconds".99 test_transition(get_utc_time(2287785600 + 0), 0s, true); // 1 Jul 1972100 test_transition(get_utc_time(2303683200 + 1), 1s, true); // 1 Jan 1973101 test_transition(get_utc_time(2335219200 + 2), 2s, true); // 1 Jan 1974102 test_transition(get_utc_time(2366755200 + 3), 3s, true); // 1 Jan 1975103 test_transition(get_utc_time(2398291200 + 4), 4s, true); // 1 Jan 1976104 test_transition(get_utc_time(2429913600 + 5), 5s, true); // 1 Jan 1977105 test_transition(get_utc_time(2461449600 + 6), 6s, true); // 1 Jan 1978106 test_transition(get_utc_time(2492985600 + 7), 7s, true); // 1 Jan 1979107 test_transition(get_utc_time(2524521600 + 8), 8s, true); // 1 Jan 1980108 test_transition(get_utc_time(2571782400 + 9), 9s, true); // 1 Jul 1981109 test_transition(get_utc_time(2603318400 + 10), 10s, true); // 1 Jul 1982110 test_transition(get_utc_time(2634854400 + 11), 11s, true); // 1 Jul 1983111 test_transition(get_utc_time(2698012800 + 12), 12s, true); // 1 Jul 1985112 test_transition(get_utc_time(2776982400 + 13), 13s, true); // 1 Jan 1988113 test_transition(get_utc_time(2840140800 + 14), 14s, true); // 1 Jan 1990114 test_transition(get_utc_time(2871676800 + 15), 15s, true); // 1 Jan 1991115 test_transition(get_utc_time(2918937600 + 16), 16s, true); // 1 Jul 1992116 test_transition(get_utc_time(2950473600 + 17), 17s, true); // 1 Jul 1993117 test_transition(get_utc_time(2982009600 + 18), 18s, true); // 1 Jul 1994118 test_transition(get_utc_time(3029443200 + 19), 19s, true); // 1 Jan 1996119 test_transition(get_utc_time(3076704000 + 20), 20s, true); // 1 Jul 1997120 test_transition(get_utc_time(3124137600 + 21), 21s, true); // 1 Jan 1999121 test_transition(get_utc_time(3345062400 + 22), 22s, true); // 1 Jan 2006122 test_transition(get_utc_time(3439756800 + 23), 23s, true); // 1 Jan 2009123 test_transition(get_utc_time(3550089600 + 24), 24s, true); // 1 Jul 2012124 test_transition(get_utc_time(3644697600 + 25), 25s, true); // 1 Jul 2015125 test_transition(get_utc_time(3692217600 + 26), 26s, true); // 1 Jan 2017126 127 return 0;128}129