|
| 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 | +// REQUIRES: libcpp-hardening-mode={{extensive|debug}} |
| 16 | +// REQUIRES: has-unix-headers |
| 17 | +// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing |
| 18 | + |
| 19 | +// <chrono> |
| 20 | +// |
| 21 | +// class gps_clock; |
| 22 | + |
| 23 | +// static gps_time<common_type_t<_Duration, seconds>> |
| 24 | +// from_utc(const utc_time<_Duration>& t) noexcept; |
| 25 | + |
| 26 | +#include <chrono> |
| 27 | + |
| 28 | +#include "check_assertion.h" |
| 29 | + |
| 30 | +// The function is specified as |
| 31 | +// gps_time<common_type_t<Duration, seconds>>{t.time_since_epoch()} + 378691210s |
| 32 | +// When t == t.max() there will be a signed integral overflow (other values too). |
| 33 | +int main(int, char**) { |
| 34 | + using namespace std::literals::chrono_literals; |
| 35 | + constexpr std::chrono::seconds offset{315964809}; |
| 36 | + |
| 37 | + (void)std::chrono::gps_clock::from_utc(std::chrono::utc_time<std::chrono::nanoseconds>::max() - offset); |
| 38 | + TEST_LIBCPP_ASSERT_FAILURE( |
| 39 | + std::chrono::gps_clock::from_utc(std::chrono::utc_time<std::chrono::nanoseconds>::max() - offset + 1ns), |
| 40 | + "the UTC to GPS conversion would overflow"); |
| 41 | + |
| 42 | + (void)std::chrono::gps_clock::from_utc(std::chrono::utc_time<std::chrono::microseconds>::max() - offset); |
| 43 | + TEST_LIBCPP_ASSERT_FAILURE( |
| 44 | + std::chrono::gps_clock::from_utc(std::chrono::utc_time<std::chrono::microseconds>::max() - offset + 1us), |
| 45 | + "the UTC to GPS conversion would overflow"); |
| 46 | + |
| 47 | + (void)std::chrono::gps_clock::from_utc(std::chrono::utc_time<std::chrono::milliseconds>::max() - offset); |
| 48 | + TEST_LIBCPP_ASSERT_FAILURE( |
| 49 | + std::chrono::gps_clock::from_utc(std::chrono::utc_time<std::chrono::milliseconds>::max() - offset + 1ms), |
| 50 | + "the UTC to GPS conversion would overflow"); |
| 51 | + |
| 52 | + (void)std::chrono::gps_clock::from_utc(std::chrono::utc_seconds::max() - offset); |
| 53 | + TEST_LIBCPP_ASSERT_FAILURE(std::chrono::gps_clock::from_utc(std::chrono::utc_seconds::max() - offset + 1s), |
| 54 | + "the UTC to GPS conversion would overflow"); |
| 55 | + |
| 56 | + // The conversion uses `common_type_t<Duration, seconds>` so types "larger" |
| 57 | + // than seconds are converted to seconds. Types "larger" than seconds are |
| 58 | + // stored in "smaller" intergral and the overflow can never occur. |
| 59 | + |
| 60 | + // Validate the types can never overflow on all current (and future) supported platforms. |
| 61 | + static_assert(std::chrono::utc_time<std::chrono::days>::max() <= std::chrono::utc_seconds::max() - offset); |
| 62 | + static_assert(std::chrono::utc_time<std::chrono::weeks>::max() <= std::chrono::utc_seconds::max() - offset); |
| 63 | + static_assert(std::chrono::utc_time<std::chrono::months>::max() <= std::chrono::utc_seconds::max() - offset); |
| 64 | + static_assert(std::chrono::utc_time<std::chrono::years>::max() <= std::chrono::utc_seconds::max() - offset); |
| 65 | + |
| 66 | + // Validate the run-time conversion works. |
| 67 | + (void)std::chrono::gps_clock::from_utc(std::chrono::utc_time<std::chrono::days>::max()); |
| 68 | + (void)std::chrono::gps_clock::from_utc(std::chrono::utc_time<std::chrono::weeks>::max()); |
| 69 | + (void)std::chrono::gps_clock::from_utc(std::chrono::utc_time<std::chrono::months>::max()); |
| 70 | + (void)std::chrono::gps_clock::from_utc(std::chrono::utc_time<std::chrono::years>::max()); |
| 71 | + |
| 72 | + return 0; |
| 73 | +} |
0 commit comments