|
| 1 | +// -*- C++ -*- |
| 2 | +//===----------------------------------------------------------------------===// |
| 3 | +// |
| 4 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | +// See https://llvm.org/LICENSE.txt for license information. |
| 6 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | + |
| 10 | +// For information see https://libcxx.llvm.org/DesignDocs/TimeZone.html |
| 11 | + |
| 12 | +#ifndef _LIBCPP___CHRONO_EXCEPTION_H |
| 13 | +#define _LIBCPP___CHRONO_EXCEPTION_H |
| 14 | + |
| 15 | +#include <version> |
| 16 | +// Enable the contents of the header only when libc++ was built with experimental features enabled. |
| 17 | +#if !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_TZDB) |
| 18 | + |
| 19 | +# include <__availability> |
| 20 | +# include <__chrono/calendar.h> |
| 21 | +# include <__chrono/local_info.h> |
| 22 | +# include <__chrono/time_point.h> |
| 23 | +# include <__config> |
| 24 | +# include <__verbose_abort> |
| 25 | +# include <format> |
| 26 | +# include <stdexcept> |
| 27 | +# include <string> |
| 28 | + |
| 29 | +# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 30 | +# pragma GCC system_header |
| 31 | +# endif |
| 32 | + |
| 33 | +_LIBCPP_BEGIN_NAMESPACE_STD |
| 34 | + |
| 35 | +# if _LIBCPP_STD_VER >= 20 |
| 36 | + |
| 37 | +namespace chrono { |
| 38 | + |
| 39 | +class nonexistent_local_time : public runtime_error { |
| 40 | +public: |
| 41 | + template <class _Duration> |
| 42 | + _LIBCPP_HIDE_FROM_ABI nonexistent_local_time(const local_time<_Duration>& __time, const local_info& __info) |
| 43 | + : runtime_error{__create_message(__time, __info)} { |
| 44 | + // [time.zone.exception.nonexist]/2 |
| 45 | + // Preconditions: i.result == local_info::nonexistent is true. |
| 46 | + // The value of __info.result is not used. |
| 47 | + _LIBCPP_ASSERT_PEDANTIC(__info.result == local_info::nonexistent, |
| 48 | + "creating an nonexistent_local_time from a local_info that is not non-existent"); |
| 49 | + } |
| 50 | + |
| 51 | + _LIBCPP_AVAILABILITY_TZDB _LIBCPP_EXPORTED_FROM_ABI ~nonexistent_local_time() override; // exported as key function |
| 52 | + |
| 53 | +private: |
| 54 | + template <class _Duration> |
| 55 | + _LIBCPP_HIDE_FROM_ABI string __create_message(const local_time<_Duration>& __time, const local_info& __info) { |
| 56 | + return std::format( |
| 57 | + R"({} is in a gap between |
| 58 | +{} {} and |
| 59 | +{} {} which are both equivalent to |
| 60 | +{} UTC)", |
| 61 | + __time, |
| 62 | + local_seconds{__info.first.end.time_since_epoch()} + __info.first.offset, |
| 63 | + __info.first.abbrev, |
| 64 | + local_seconds{__info.second.begin.time_since_epoch()} + __info.second.offset, |
| 65 | + __info.second.abbrev, |
| 66 | + __info.first.end); |
| 67 | + } |
| 68 | +}; |
| 69 | + |
| 70 | +template <class _Duration> |
| 71 | +_LIBCPP_NORETURN _LIBCPP_AVAILABILITY_TZDB _LIBCPP_HIDE_FROM_ABI void __throw_nonexistent_local_time( |
| 72 | + [[maybe_unused]] const local_time<_Duration>& __time, [[maybe_unused]] const local_info& __info) { |
| 73 | +# ifndef _LIBCPP_HAS_NO_EXCEPTIONS |
| 74 | + throw nonexistent_local_time(__time, __info); |
| 75 | +# else |
| 76 | + _LIBCPP_VERBOSE_ABORT("nonexistent_local_time was thrown in -fno-exceptions mode"); |
| 77 | +# endif |
| 78 | +} |
| 79 | + |
| 80 | +class ambiguous_local_time : public runtime_error { |
| 81 | +public: |
| 82 | + template <class _Duration> |
| 83 | + _LIBCPP_HIDE_FROM_ABI ambiguous_local_time(const local_time<_Duration>& __time, const local_info& __info) |
| 84 | + : runtime_error{__create_message(__time, __info)} { |
| 85 | + // [time.zone.exception.ambig]/2 |
| 86 | + // Preconditions: i.result == local_info::ambiguous is true. |
| 87 | + // The value of __info.result is not used. |
| 88 | + _LIBCPP_ASSERT_PEDANTIC(__info.result == local_info::ambiguous, |
| 89 | + "creating an ambiguous_local_time from a local_info that is not ambiguous"); |
| 90 | + } |
| 91 | + |
| 92 | + _LIBCPP_AVAILABILITY_TZDB _LIBCPP_EXPORTED_FROM_ABI ~ambiguous_local_time() override; // exported as key function |
| 93 | + |
| 94 | +private: |
| 95 | + template <class _Duration> |
| 96 | + _LIBCPP_HIDE_FROM_ABI string __create_message(const local_time<_Duration>& __time, const local_info& __info) { |
| 97 | + return std::format( |
| 98 | + // There are two spaces after the full-stop; this has been verified |
| 99 | + // in the sources of the Standard. |
| 100 | + R"({0} is ambiguous. It could be |
| 101 | +{0} {1} == {2} UTC or |
| 102 | +{0} {3} == {4} UTC)", |
| 103 | + __time, |
| 104 | + __info.first.abbrev, |
| 105 | + __time - __info.first.offset, |
| 106 | + __info.second.abbrev, |
| 107 | + __time - __info.second.offset); |
| 108 | + } |
| 109 | +}; |
| 110 | + |
| 111 | +template <class _Duration> |
| 112 | +_LIBCPP_NORETURN _LIBCPP_AVAILABILITY_TZDB _LIBCPP_HIDE_FROM_ABI void __throw_ambiguous_local_time( |
| 113 | + [[maybe_unused]] const local_time<_Duration>& __time, [[maybe_unused]] const local_info& __info) { |
| 114 | +# ifndef _LIBCPP_HAS_NO_EXCEPTIONS |
| 115 | + throw ambiguous_local_time(__time, __info); |
| 116 | +# else |
| 117 | + _LIBCPP_VERBOSE_ABORT("ambiguous_local_time was thrown in -fno-exceptions mode"); |
| 118 | +# endif |
| 119 | +} |
| 120 | + |
| 121 | +} // namespace chrono |
| 122 | + |
| 123 | +# endif // _LIBCPP_STD_VER >= 20 |
| 124 | + |
| 125 | +_LIBCPP_END_NAMESPACE_STD |
| 126 | + |
| 127 | +#endif // !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_TZDB) |
| 128 | + |
| 129 | +#endif // _LIBCPP___CHRONO_EXCEPTION_H |
0 commit comments