| 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_ZONED_TIME_H |
| 13 | #define _LIBCPP___CHRONO_ZONED_TIME_H |
| 14 | |
| 15 | #include <version> |
| 16 | // Enable the contents of the header only when libc++ was built with experimental features enabled. |
| 17 | #if _LIBCPP_HAS_EXPERIMENTAL_TZDB |
| 18 | |
| 19 | # include <__chrono/calendar.h> |
| 20 | # include <__chrono/duration.h> |
| 21 | # include <__chrono/sys_info.h> |
| 22 | # include <__chrono/system_clock.h> |
| 23 | # include <__chrono/time_zone.h> |
| 24 | # include <__chrono/tzdb_list.h> |
| 25 | # include <__concepts/constructible.h> |
| 26 | # include <__config> |
| 27 | # include <__cstddef/size_t.h> |
| 28 | # include <__functional/hash.h> |
| 29 | # include <__type_traits/common_type.h> |
| 30 | # include <__type_traits/conditional.h> |
| 31 | # include <__type_traits/remove_cvref.h> |
| 32 | # include <__utility/declval.h> |
| 33 | # include <__utility/move.h> |
| 34 | # include <string_view> |
| 35 | |
| 36 | # if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 37 | # pragma GCC system_header |
| 38 | # endif |
| 39 | |
| 40 | _LIBCPP_PUSH_MACROS |
| 41 | # include <__undef_macros> |
| 42 | |
| 43 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 44 | |
| 45 | # if _LIBCPP_STD_VER >= 20 && _LIBCPP_HAS_TIME_ZONE_DATABASE && _LIBCPP_HAS_FILESYSTEM && _LIBCPP_HAS_LOCALIZATION |
| 46 | |
| 47 | namespace chrono { |
| 48 | |
| 49 | template <class> |
| 50 | struct zoned_traits {}; |
| 51 | |
| 52 | template <> |
| 53 | struct zoned_traits<const time_zone*> { |
| 54 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static const time_zone* default_zone() { return chrono::locate_zone(name: "UTC" ); } |
| 55 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static const time_zone* locate_zone(string_view __name) { |
| 56 | return chrono::locate_zone(__name); |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | template <class _Duration, class _TimeZonePtr = const time_zone*> |
| 61 | class zoned_time { |
| 62 | // [time.zone.zonedtime.ctor]/2 |
| 63 | static_assert(__is_duration_v<_Duration>, |
| 64 | "the program is ill-formed since _Duration is not a specialization of std::chrono::duration" ); |
| 65 | |
| 66 | // The wording uses the constraints like |
| 67 | // constructible_from<zoned_time, decltype(__traits::locate_zone(string_view{}))> |
| 68 | // Using these constraints in the code causes the compiler to give an |
| 69 | // error that the constraint depends on itself. To avoid that issue use |
| 70 | // the fact it is possible to create this object from a _TimeZonePtr. |
| 71 | using __traits _LIBCPP_NODEBUG = zoned_traits<_TimeZonePtr>; |
| 72 | |
| 73 | public: |
| 74 | using duration = common_type_t<_Duration, seconds>; |
| 75 | |
| 76 | _LIBCPP_HIDE_FROM_ABI zoned_time() |
| 77 | requires requires { __traits::default_zone(); } |
| 78 | : __zone_{__traits::default_zone()}, __tp_{} {} |
| 79 | |
| 80 | _LIBCPP_HIDE_FROM_ABI zoned_time(const zoned_time&) = default; |
| 81 | _LIBCPP_HIDE_FROM_ABI zoned_time& operator=(const zoned_time&) = default; |
| 82 | |
| 83 | _LIBCPP_HIDE_FROM_ABI zoned_time(const sys_time<_Duration>& __tp) |
| 84 | requires requires { __traits::default_zone(); } |
| 85 | : __zone_{__traits::default_zone()}, __tp_{__tp} {} |
| 86 | |
| 87 | _LIBCPP_HIDE_FROM_ABI explicit zoned_time(_TimeZonePtr __zone) : __zone_{std::move(__zone)}, __tp_{} {} |
| 88 | |
| 89 | _LIBCPP_HIDE_FROM_ABI explicit zoned_time(string_view __name) |
| 90 | requires(requires { __traits::locate_zone(string_view{}); } && |
| 91 | constructible_from<_TimeZonePtr, decltype(__traits::locate_zone(string_view{}))>) |
| 92 | : __zone_{__traits::locate_zone(__name)}, __tp_{} {} |
| 93 | |
| 94 | template <class _Duration2> |
| 95 | _LIBCPP_HIDE_FROM_ABI zoned_time(const zoned_time<_Duration2, _TimeZonePtr>& __zt) |
| 96 | requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>> |
| 97 | : __zone_{__zt.get_time_zone()}, __tp_{__zt.get_sys_time()} {} |
| 98 | |
| 99 | _LIBCPP_HIDE_FROM_ABI zoned_time(_TimeZonePtr __zone, const sys_time<_Duration>& __tp) |
| 100 | : __zone_{std::move(__zone)}, __tp_{__tp} {} |
| 101 | |
| 102 | _LIBCPP_HIDE_FROM_ABI zoned_time(string_view __name, const sys_time<_Duration>& __tp) |
| 103 | requires requires { _TimeZonePtr{__traits::locate_zone(string_view{})}; } |
| 104 | : zoned_time{__traits::locate_zone(__name), __tp} {} |
| 105 | |
| 106 | _LIBCPP_HIDE_FROM_ABI zoned_time(_TimeZonePtr __zone, const local_time<_Duration>& __tp) |
| 107 | requires(is_convertible_v<decltype(std::declval<_TimeZonePtr&>() -> to_sys(local_time<_Duration>{})), |
| 108 | sys_time<duration>>) |
| 109 | : __zone_{std::move(__zone)}, __tp_{__zone_->to_sys(__tp)} {} |
| 110 | |
| 111 | _LIBCPP_HIDE_FROM_ABI zoned_time(string_view __name, const local_time<_Duration>& __tp) |
| 112 | requires(requires { |
| 113 | _TimeZonePtr{__traits::locate_zone(string_view{})}; |
| 114 | } && is_convertible_v<decltype(std::declval<_TimeZonePtr&>() -> to_sys(local_time<_Duration>{})), |
| 115 | sys_time<duration>>) |
| 116 | : zoned_time{__traits::locate_zone(__name), __tp} {} |
| 117 | |
| 118 | _LIBCPP_HIDE_FROM_ABI zoned_time(_TimeZonePtr __zone, const local_time<_Duration>& __tp, choose __c) |
| 119 | requires(is_convertible_v< |
| 120 | decltype(std::declval<_TimeZonePtr&>() -> to_sys(local_time<_Duration>{}, choose::earliest)), |
| 121 | sys_time<duration>>) |
| 122 | : __zone_{std::move(__zone)}, __tp_{__zone_->to_sys(__tp, __c)} {} |
| 123 | |
| 124 | _LIBCPP_HIDE_FROM_ABI zoned_time(string_view __name, const local_time<_Duration>& __tp, choose __c) |
| 125 | requires(requires { |
| 126 | _TimeZonePtr{__traits::locate_zone(string_view{})}; |
| 127 | } && is_convertible_v<decltype(std::declval<_TimeZonePtr&>() -> to_sys(local_time<_Duration>{}, choose::earliest)), |
| 128 | sys_time<duration>>) |
| 129 | : zoned_time{__traits::locate_zone(__name), __tp, __c} {} |
| 130 | |
| 131 | template <class _Duration2, class _TimeZonePtr2> |
| 132 | _LIBCPP_HIDE_FROM_ABI zoned_time(_TimeZonePtr __zone, const zoned_time<_Duration2, _TimeZonePtr2>& __zt) |
| 133 | requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>> |
| 134 | : __zone_{std::move(__zone)}, __tp_{__zt.get_sys_time()} {} |
| 135 | |
| 136 | // per wording choose has no effect |
| 137 | template <class _Duration2, class _TimeZonePtr2> |
| 138 | _LIBCPP_HIDE_FROM_ABI zoned_time(_TimeZonePtr __zone, const zoned_time<_Duration2, _TimeZonePtr2>& __zt, choose) |
| 139 | requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>> |
| 140 | : __zone_{std::move(__zone)}, __tp_{__zt.get_sys_time()} {} |
| 141 | |
| 142 | template <class _Duration2, class _TimeZonePtr2> |
| 143 | _LIBCPP_HIDE_FROM_ABI zoned_time(string_view __name, const zoned_time<_Duration2, _TimeZonePtr2>& __zt) |
| 144 | requires(requires { |
| 145 | _TimeZonePtr{__traits::locate_zone(string_view{})}; |
| 146 | } && is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>) |
| 147 | : zoned_time{__traits::locate_zone(__name), __zt} {} |
| 148 | |
| 149 | template <class _Duration2, class _TimeZonePtr2> |
| 150 | _LIBCPP_HIDE_FROM_ABI zoned_time(string_view __name, const zoned_time<_Duration2, _TimeZonePtr2>& __zt, choose __c) |
| 151 | requires(requires { |
| 152 | _TimeZonePtr{__traits::locate_zone(string_view{})}; |
| 153 | } && is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>) |
| 154 | : zoned_time{__traits::locate_zone(__name), __zt, __c} {} |
| 155 | |
| 156 | _LIBCPP_HIDE_FROM_ABI zoned_time& operator=(const sys_time<_Duration>& __tp) { |
| 157 | __tp_ = __tp; |
| 158 | return *this; |
| 159 | } |
| 160 | |
| 161 | _LIBCPP_HIDE_FROM_ABI zoned_time& operator=(const local_time<_Duration>& __tp) { |
| 162 | // TODO TZDB This seems wrong. |
| 163 | // Assigning a non-existent or ambiguous time will throw and not satisfy |
| 164 | // the post condition. This seems quite odd; I constructed an object with |
| 165 | // choose::earliest and that choice is not respected. |
| 166 | // what did LEWG do with this. |
| 167 | // MSVC STL and libstdc++ behave the same |
| 168 | __tp_ = __zone_->to_sys(__tp); |
| 169 | return *this; |
| 170 | } |
| 171 | |
| 172 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI operator sys_time<duration>() const { return get_sys_time(); } |
| 173 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI explicit operator local_time<duration>() const { return get_local_time(); } |
| 174 | |
| 175 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI _TimeZonePtr get_time_zone() const { return __zone_; } |
| 176 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI local_time<duration> get_local_time() const { return __zone_->to_local(__tp_); } |
| 177 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI sys_time<duration> get_sys_time() const { return __tp_; } |
| 178 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI sys_info get_info() const { return __zone_->get_info(__tp_); } |
| 179 | |
| 180 | private: |
| 181 | _TimeZonePtr __zone_; |
| 182 | sys_time<duration> __tp_; |
| 183 | }; |
| 184 | |
| 185 | zoned_time() -> zoned_time<seconds>; |
| 186 | |
| 187 | template <class _Duration> |
| 188 | zoned_time(sys_time<_Duration>) -> zoned_time<common_type_t<_Duration, seconds>>; |
| 189 | |
| 190 | template <class _TimeZonePtrOrName> |
| 191 | using __time_zone_representation _LIBCPP_NODEBUG = |
| 192 | conditional_t<is_convertible_v<_TimeZonePtrOrName, string_view>, |
| 193 | const time_zone*, |
| 194 | remove_cvref_t<_TimeZonePtrOrName>>; |
| 195 | |
| 196 | template <class _TimeZonePtrOrName> |
| 197 | zoned_time(_TimeZonePtrOrName&&) -> zoned_time<seconds, __time_zone_representation<_TimeZonePtrOrName>>; |
| 198 | |
| 199 | template <class _TimeZonePtrOrName, class _Duration> |
| 200 | zoned_time(_TimeZonePtrOrName&&, sys_time<_Duration>) |
| 201 | -> zoned_time<common_type_t<_Duration, seconds>, __time_zone_representation<_TimeZonePtrOrName>>; |
| 202 | |
| 203 | template <class _TimeZonePtrOrName, class _Duration> |
| 204 | zoned_time(_TimeZonePtrOrName&&, local_time<_Duration>, choose = choose::earliest) |
| 205 | -> zoned_time<common_type_t<_Duration, seconds>, __time_zone_representation<_TimeZonePtrOrName>>; |
| 206 | |
| 207 | template <class _Duration, class _TimeZonePtrOrName, class _TimeZonePtr2> |
| 208 | zoned_time(_TimeZonePtrOrName&&, zoned_time<_Duration, _TimeZonePtr2>, choose = choose::earliest) |
| 209 | -> zoned_time<common_type_t<_Duration, seconds>, __time_zone_representation<_TimeZonePtrOrName>>; |
| 210 | |
| 211 | using zoned_seconds = zoned_time<seconds>; |
| 212 | |
| 213 | template <class _Duration1, class _Duration2, class _TimeZonePtr> |
| 214 | _LIBCPP_HIDE_FROM_ABI bool |
| 215 | operator==(const zoned_time<_Duration1, _TimeZonePtr>& __lhs, const zoned_time<_Duration2, _TimeZonePtr>& __rhs) { |
| 216 | return __lhs.get_time_zone() == __rhs.get_time_zone() && __lhs.get_sys_time() == __rhs.get_sys_time(); |
| 217 | } |
| 218 | |
| 219 | } // namespace chrono |
| 220 | |
| 221 | # if _LIBCPP_STD_VER >= 26 |
| 222 | |
| 223 | template <class _Duration, class _TimeZonePtr> |
| 224 | requires __has_enabled_hash<_Duration>::value && __has_enabled_hash<_TimeZonePtr>::value |
| 225 | struct hash<chrono::zoned_time<_Duration, _TimeZonePtr>> { |
| 226 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static size_t |
| 227 | operator()(const chrono::zoned_time<_Duration, _TimeZonePtr>& __zt) { |
| 228 | return std::__hash_combine( |
| 229 | hash<chrono::sys_time<_Duration>>{}(__zt.get_sys_time()), hash<_TimeZonePtr>{}(__zt.get_time_zone())); |
| 230 | } |
| 231 | }; |
| 232 | |
| 233 | # endif // _LIBCPP_STD_VER >= 26 |
| 234 | |
| 235 | # endif // _LIBCPP_STD_VER >= 20 && _LIBCPP_HAS_TIME_ZONE_DATABASE && _LIBCPP_HAS_FILESYSTEM && |
| 236 | // _LIBCPP_HAS_LOCALIZATION |
| 237 | |
| 238 | _LIBCPP_END_NAMESPACE_STD |
| 239 | |
| 240 | _LIBCPP_POP_MACROS |
| 241 | |
| 242 | #endif // _LIBCPP_HAS_EXPERIMENTAL_TZDB |
| 243 | |
| 244 | #endif // _LIBCPP___CHRONO_ZONED_TIME_H |
| 245 | |