| 1 | // { dg-do run { target c++20 } } |
| 2 | // { dg-additional-options "-DHAVE_TZDB" { target tzdb } } |
| 3 | |
| 4 | #include <chrono> |
| 5 | #include <testsuite_hooks.h> |
| 6 | |
| 7 | using namespace std::chrono_literals; |
| 8 | |
| 9 | void |
| 10 | test_before() |
| 11 | { |
| 12 | // No leaps seconds defined before the epoch. |
| 13 | auto s = std::chrono::utc_seconds(-1s); |
| 14 | auto lsi = get_leap_second_info(s); |
| 15 | VERIFY( lsi.is_leap_second == false ); |
| 16 | VERIFY( lsi.elapsed == 0s ); |
| 17 | |
| 18 | auto ms = std::chrono::utc_time<std::chrono::milliseconds>(s - 5500ms); |
| 19 | lsi = get_leap_second_info(ms); |
| 20 | VERIFY( lsi.is_leap_second == false ); |
| 21 | VERIFY( lsi.elapsed == 0s ); |
| 22 | } |
| 23 | |
| 24 | void |
| 25 | test_after() |
| 26 | { |
| 27 | #ifdef HAVE_TZDB |
| 28 | const auto& leaps = std::chrono::get_tzdb().leap_seconds; |
| 29 | std::chrono::seconds sum(0); |
| 30 | for (auto leap : leaps) |
| 31 | sum += leap.value(); |
| 32 | |
| 33 | // After the last defined leap second. |
| 34 | auto last = leaps.back().date().time_since_epoch(); |
| 35 | auto ut = std::chrono::utc_time<std::chrono::milliseconds>(last + 72h + 10ms); |
| 36 | auto lsi = get_leap_second_info(ut); |
| 37 | VERIFY( lsi.is_leap_second == false ); |
| 38 | VERIFY( lsi.elapsed == sum ); |
| 39 | #endif |
| 40 | } |
| 41 | |
| 42 | void |
| 43 | test_between() |
| 44 | { |
| 45 | std::chrono::sys_days st(1995y/9/4); |
| 46 | auto ut = std::chrono::clock_cast<std::chrono::utc_clock>(st); |
| 47 | auto lsi = get_leap_second_info(ut); |
| 48 | VERIFY( lsi.is_leap_second == false ); |
| 49 | VERIFY( lsi.elapsed == 19s ); |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | test_during() |
| 54 | { |
| 55 | #ifdef HAVE_TZDB |
| 56 | // Verify that leap_second_info::is_leap_second is true for each leap second. |
| 57 | const auto& leaps = std::chrono::get_tzdb().leap_seconds; |
| 58 | for (const auto& leap : leaps) |
| 59 | { |
| 60 | // N.B. this assumes all leap seconds are positive: |
| 61 | std::chrono::seconds elapsed(&leap - &leaps.front()); |
| 62 | std::chrono::utc_seconds ut(leap.date().time_since_epoch() + elapsed); |
| 63 | auto lsi = get_leap_second_info(ut); |
| 64 | VERIFY( lsi.is_leap_second == true ); |
| 65 | VERIFY( lsi.elapsed == elapsed + 1s ); |
| 66 | lsi = get_leap_second_info(ut + 999ms); |
| 67 | VERIFY( lsi.is_leap_second == true ); |
| 68 | VERIFY( lsi.elapsed == elapsed + 1s ); |
| 69 | } |
| 70 | #endif |
| 71 | } |
| 72 | |
| 73 | int main() |
| 74 | { |
| 75 | test_before(); |
| 76 | test_after(); |
| 77 | test_between(); |
| 78 | test_during(); |
| 79 | } |