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