Skip to content

Commit e77b295

Browse files
authored
[libc++][TZDB] Adds zoned_time deduction guides. (#95139)
Completes - LWG3232 Inconsistency in zoned_time deduction guides - LWG3294 zoned_time deduction guides misinterprets stringchar* Implements parts of: - P0355 Extending to chrono Calendars and Time Zones
1 parent abde52a commit e77b295

File tree

3 files changed

+278
-2
lines changed

3 files changed

+278
-2
lines changed

libcxx/docs/Status/Cxx20Issues.csv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@
167167
"`3218 <https://wg21.link/LWG3218>`__","Modifier for ``%d``\ parse flag does not match POSIX and ``format``\ specification","Belfast","","","|chrono| |format|"
168168
"`3224 <https://wg21.link/LWG3224>`__","``zoned_time``\ constructor from ``TimeZonePtr``\ does not specify initialization of ``tp_``\ ","Belfast","|Complete|","19.0","|chrono|"
169169
"`3230 <https://wg21.link/LWG3230>`__","Format specifier ``%y/%Y``\ is missing locale alternative versions","Belfast","|Complete|","16.0","|chrono| |format|"
170-
"`3232 <https://wg21.link/LWG3232>`__","Inconsistency in ``zoned_time``\ deduction guides","Belfast","","","|chrono|"
170+
"`3232 <https://wg21.link/LWG3232>`__","Inconsistency in ``zoned_time``\ deduction guides","Belfast","|Complete|","19.0","|chrono|"
171171
"`3222 <https://wg21.link/LWG3222>`__","P0574R1 introduced preconditions on non-existent parameters","Belfast","",""
172172
"`3221 <https://wg21.link/LWG3221>`__","Result of ``year_month``\ arithmetic with ``months``\ is ambiguous","Belfast","|Complete|","8.0"
173173
"`3235 <https://wg21.link/LWG3235>`__","``parse``\ manipulator without abbreviation is not callable","Belfast","",""
@@ -225,7 +225,7 @@
225225
"`3286 <https://wg21.link/LWG3286>`__","``ranges::size``\ is not required to be valid after a call to ``ranges::begin``\ on an input range","Prague","|Complete|","15.0","|ranges|"
226226
"`3291 <https://wg21.link/LWG3291>`__","``iota_view::iterator``\ has the wrong ``iterator_category``\ ","Prague","|Complete|","15.0","|ranges|"
227227
"`3292 <https://wg21.link/LWG3292>`__","``iota_view``\ is under-constrained","Prague","|Complete|","15.0","|ranges|"
228-
"`3294 <https://wg21.link/LWG3294>`__","``zoned_time``\ deduction guides misinterprets ``string``\ /``char*``\ ","Prague","","","|chrono|"
228+
"`3294 <https://wg21.link/LWG3294>`__","``zoned_time``\ deduction guides misinterprets ``string``\ /``char*``\ ","Prague","|Complete|","19.0","|chrono|"
229229
"`3296 <https://wg21.link/LWG3296>`__","Inconsistent default argument for ``basic_regex<>::assign``\ ","Prague","|Complete|",""
230230
"`3299 <https://wg21.link/LWG3299>`__","Pointers don't need customized iterator behavior","Prague","|Complete|","15.0","|ranges|"
231231
"`3300 <https://wg21.link/LWG3300>`__","Non-array ``ssize``\ overload is underconstrained","Prague","|Nothing To Do|",""

libcxx/include/__chrono/zoned_time.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
# include <__config>
2626
# include <__fwd/string_view.h>
2727
# include <__type_traits/common_type.h>
28+
# include <__type_traits/conditional.h>
29+
# include <__type_traits/remove_cvref.h>
2830
# include <__utility/move.h>
2931

3032
# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -177,6 +179,32 @@ class zoned_time {
177179
sys_time<duration> __tp_;
178180
};
179181

182+
zoned_time() -> zoned_time<seconds>;
183+
184+
template <class _Duration>
185+
zoned_time(sys_time<_Duration>) -> zoned_time<common_type_t<_Duration, seconds>>;
186+
187+
template <class _TimeZonePtrOrName>
188+
using __time_zone_representation =
189+
conditional_t<is_convertible_v<_TimeZonePtrOrName, string_view>,
190+
const time_zone*,
191+
remove_cvref_t<_TimeZonePtrOrName>>;
192+
193+
template <class _TimeZonePtrOrName>
194+
zoned_time(_TimeZonePtrOrName&&) -> zoned_time<seconds, __time_zone_representation<_TimeZonePtrOrName>>;
195+
196+
template <class _TimeZonePtrOrName, class _Duration>
197+
zoned_time(_TimeZonePtrOrName&&, sys_time<_Duration>)
198+
-> zoned_time<common_type_t<_Duration, seconds>, __time_zone_representation<_TimeZonePtrOrName>>;
199+
200+
template <class _TimeZonePtrOrName, class _Duration>
201+
zoned_time(_TimeZonePtrOrName&&, local_time<_Duration>, choose = choose::earliest)
202+
-> zoned_time<common_type_t<_Duration, seconds>, __time_zone_representation<_TimeZonePtrOrName>>;
203+
204+
template <class _Duration, class _TimeZonePtrOrName, class TimeZonePtr2>
205+
zoned_time(_TimeZonePtrOrName&&, zoned_time<_Duration, TimeZonePtr2>, choose = choose::earliest)
206+
-> zoned_time<common_type_t<_Duration, seconds>, __time_zone_representation<_TimeZonePtrOrName>>;
207+
180208
} // namespace chrono
181209

182210
# endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_TIME_ZONE_DATABASE) && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
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+
// UNSUPPORTED: c++03, c++11, c++14, c++17
10+
// UNSUPPORTED: no-filesystem, no-localization, no-tzdb
11+
12+
// XFAIL: libcpp-has-no-experimental-tzdb
13+
// XFAIL: availability-tzdb-missing
14+
15+
// <chrono>
16+
17+
// zoned_time() -> zoned_time<seconds>;
18+
//
19+
// template<class Duration>
20+
// zoned_time(sys_time<Duration>)
21+
// -> zoned_time<common_type_t<Duration, seconds>>;
22+
//
23+
// template<class TimeZonePtrOrName>
24+
// using time-zone-representation = // exposition only
25+
// conditional_t<is_convertible_v<TimeZonePtrOrName, string_view>,
26+
// const time_zone*,
27+
// remove_cvref_t<TimeZonePtrOrName>>;
28+
//
29+
// template<class TimeZonePtrOrName>
30+
// zoned_time(TimeZonePtrOrName&&)
31+
// -> zoned_time<seconds, time-zone-representation<TimeZonePtrOrName>>;
32+
//
33+
// template<class TimeZonePtrOrName, class Duration>
34+
// zoned_time(TimeZonePtrOrName&&, sys_time<Duration>)
35+
// -> zoned_time<common_type_t<Duration, seconds>,
36+
// time-zone-representation<TimeZonePtrOrName>>;
37+
//
38+
// template<class TimeZonePtrOrName, class Duration>
39+
// zoned_time(TimeZonePtrOrName&&, local_time<Duration>,
40+
// choose = choose::earliest)
41+
// -> zoned_time<common_type_t<Duration, seconds>,
42+
// time-zone-representation<TimeZonePtrOrName>>;
43+
//
44+
// template<class Duration, class TimeZonePtrOrName, class TimeZonePtr2>
45+
// zoned_time(TimeZonePtrOrName&&, zoned_time<Duration, TimeZonePtr2>,
46+
// choose = choose::earliest)
47+
// -> zoned_time<common_type_t<Duration, seconds>,
48+
// time-zone-representation<TimeZonePtrOrName>>;
49+
50+
#include <chrono>
51+
#include <concepts>
52+
#include <string>
53+
54+
#include "test_offset_time_zone.h"
55+
56+
namespace cr = std::chrono;
57+
58+
// Verify the results of the constructed object.
59+
int main(int, char**) {
60+
{
61+
// zoned_time() -> zoned_time<seconds>;
62+
cr::zoned_time zt;
63+
static_assert(std::same_as<decltype(zt), cr::zoned_time<cr::seconds, const cr::time_zone*>>);
64+
}
65+
66+
{
67+
// template<class Duration>
68+
// zoned_time(sys_time<Duration>)
69+
// -> zoned_time<common_type_t<Duration, seconds>>;
70+
{
71+
cr::zoned_time zt{cr::sys_time<cr::nanoseconds>{cr::nanoseconds{0}}};
72+
static_assert(std::same_as<decltype(zt), cr::zoned_time<cr::nanoseconds, const cr::time_zone*>>);
73+
}
74+
{
75+
cr::zoned_time zt{cr::sys_time<cr::seconds>{cr::seconds{0}}};
76+
static_assert(std::same_as<decltype(zt), cr::zoned_time<cr::seconds, const cr::time_zone*>>);
77+
}
78+
{
79+
cr::zoned_time zt{cr::sys_time<cr::days>{cr::days{0}}};
80+
static_assert(std::same_as<decltype(zt), cr::zoned_time<cr::seconds, const cr::time_zone*>>);
81+
}
82+
}
83+
84+
{
85+
// template<class TimeZonePtrOrName>
86+
// zoned_time(TimeZonePtrOrName&&)
87+
// -> zoned_time<seconds, time-zone-representation<TimeZonePtrOrName>>;
88+
{ // Name
89+
{
90+
cr::zoned_time zt{"UTC"};
91+
static_assert(std::same_as<decltype(zt), cr::zoned_time<cr::seconds, const cr::time_zone*>>);
92+
}
93+
{
94+
cr::zoned_time zt{std::string{"UTC"}};
95+
static_assert(std::same_as<decltype(zt), cr::zoned_time<cr::seconds, const cr::time_zone*>>);
96+
}
97+
{
98+
cr::zoned_time zt{std::string_view{"UTC"}};
99+
static_assert(std::same_as<decltype(zt), cr::zoned_time<cr::seconds, const cr::time_zone*>>);
100+
}
101+
}
102+
{ // TimeZonePtr
103+
{
104+
cr::zoned_time zt{static_cast<const cr::time_zone*>(nullptr)};
105+
static_assert(std::same_as<decltype(zt), cr::zoned_time<cr::seconds, const cr::time_zone*>>);
106+
}
107+
{
108+
cr::zoned_time zt{offset_time_zone<offset_time_zone_flags::none>{}};
109+
static_assert(
110+
std::same_as< decltype(zt), cr::zoned_time<cr::seconds, offset_time_zone<offset_time_zone_flags::none>>>);
111+
}
112+
{
113+
cr::zoned_time zt{offset_time_zone<offset_time_zone_flags::has_default_zone>{}};
114+
static_assert(
115+
std::same_as<decltype(zt),
116+
cr::zoned_time<cr::seconds, offset_time_zone<offset_time_zone_flags::has_default_zone>>>);
117+
}
118+
{
119+
cr::zoned_time zt{offset_time_zone<offset_time_zone_flags::has_locate_zone>{}};
120+
static_assert(
121+
std::same_as<decltype(zt),
122+
cr::zoned_time<cr::seconds, offset_time_zone<offset_time_zone_flags::has_locate_zone>>>);
123+
}
124+
{
125+
cr::zoned_time zt{offset_time_zone<offset_time_zone_flags::both>{}};
126+
static_assert(
127+
std::same_as< decltype(zt), cr::zoned_time<cr::seconds, offset_time_zone<offset_time_zone_flags::both>>>);
128+
}
129+
130+
// There are no requirements on the TimeZonePtr type.
131+
{
132+
cr::zoned_time zt{0};
133+
static_assert(std::same_as<decltype(zt), cr::zoned_time<cr::seconds, int>>);
134+
}
135+
{
136+
cr::zoned_time zt{0.0};
137+
static_assert(std::same_as<decltype(zt), cr::zoned_time<cr::seconds, double>>);
138+
}
139+
{
140+
cr::zoned_time zt{cr::seconds{}};
141+
static_assert(std::same_as<decltype(zt), cr::zoned_time<cr::seconds, cr::seconds>>);
142+
}
143+
}
144+
}
145+
146+
{
147+
// template<class TimeZonePtrOrName, class Duration>
148+
// zoned_time(TimeZonePtrOrName&&, sys_time<Duration>)
149+
// -> zoned_time<common_type_t<Duration, seconds>,
150+
// time-zone-representation<TimeZonePtrOrName>>;
151+
{ // Name
152+
{
153+
cr::zoned_time zt{"UTC", cr::sys_time<cr::nanoseconds>{}};
154+
static_assert(std::same_as<decltype(zt), cr::zoned_time<cr::nanoseconds, const cr::time_zone*>>);
155+
}
156+
{
157+
cr::zoned_time zt{"UTC", cr::sys_time<cr::days>{}};
158+
static_assert(std::same_as<decltype(zt), cr::zoned_time<cr::seconds, const cr::time_zone*>>);
159+
}
160+
}
161+
162+
{ // TimeZonePtr
163+
{
164+
cr::zoned_time zt{static_cast<const cr::time_zone*>(nullptr), cr::sys_time<cr::nanoseconds>{}};
165+
static_assert(std::same_as<decltype(zt), cr::zoned_time<cr::nanoseconds, const cr::time_zone*>>);
166+
}
167+
{
168+
cr::zoned_time zt{static_cast<const cr::time_zone*>(nullptr), cr::sys_time<cr::days>{}};
169+
static_assert(std::same_as<decltype(zt), cr::zoned_time<cr::seconds, const cr::time_zone*>>);
170+
}
171+
{
172+
cr::zoned_time zt{offset_time_zone<offset_time_zone_flags::none>{}, cr::sys_time<cr::nanoseconds>{}};
173+
static_assert(std::same_as< decltype(zt),
174+
cr::zoned_time<cr::nanoseconds, offset_time_zone<offset_time_zone_flags::none>>>);
175+
}
176+
{
177+
cr::zoned_time zt{offset_time_zone<offset_time_zone_flags::none>{}, cr::sys_time<cr::days>{}};
178+
static_assert(
179+
std::same_as< decltype(zt), cr::zoned_time<cr::seconds, offset_time_zone<offset_time_zone_flags::none>>>);
180+
}
181+
}
182+
}
183+
184+
{
185+
// template<class TimeZonePtrOrName, class Duration>
186+
// zoned_time(TimeZonePtrOrName&&, local_time<Duration>,
187+
// choose = choose::earliest)
188+
// -> zoned_time<common_type_t<Duration, seconds>,
189+
// time-zone-representation<TimeZonePtrOrName>>;
190+
{ // Name
191+
{
192+
cr::zoned_time zt{"UTC", cr::local_time<cr::nanoseconds>{}};
193+
static_assert(std::same_as<decltype(zt), cr::zoned_time<cr::nanoseconds, const cr::time_zone*>>);
194+
}
195+
{
196+
cr::zoned_time zt{"UTC", cr::local_time<cr::days>{}, cr::choose::earliest};
197+
static_assert(std::same_as<decltype(zt), cr::zoned_time<cr::seconds, const cr::time_zone*>>);
198+
}
199+
}
200+
{ // TimeZonePtr
201+
{
202+
cr::zoned_time zt{cr::locate_zone("UTC"), cr::local_time<cr::nanoseconds>{}};
203+
static_assert(std::same_as<decltype(zt), cr::zoned_time<cr::nanoseconds, const cr::time_zone*>>);
204+
}
205+
{
206+
cr::zoned_time zt{cr::locate_zone("UTC"), cr::local_time<cr::days>{}, cr::choose::earliest};
207+
static_assert(std::same_as<decltype(zt), cr::zoned_time<cr::seconds, const cr::time_zone*>>);
208+
}
209+
}
210+
}
211+
212+
{
213+
// template<class Duration, class TimeZonePtrOrName, class TimeZonePtr2>
214+
// zoned_time(TimeZonePtrOrName&&, zoned_time<Duration, TimeZonePtr2>,
215+
// choose = choose::earliest)
216+
// -> zoned_time<common_type_t<Duration, seconds>,
217+
// time-zone-representation<TimeZonePtrOrName>>;
218+
{ // Name
219+
{
220+
cr::zoned_time zt{
221+
"UTC", cr::zoned_time<cr::nanoseconds, offset_time_zone<offset_time_zone_flags::has_default_zone>>{}};
222+
static_assert(std::same_as<decltype(zt), cr::zoned_time<cr::nanoseconds, const cr::time_zone*>>);
223+
}
224+
{
225+
cr::zoned_time zt{"UTC",
226+
cr::zoned_time<cr::days, offset_time_zone<offset_time_zone_flags::has_default_zone>>{},
227+
cr::choose::earliest};
228+
static_assert(std::same_as<decltype(zt), cr::zoned_time<cr::seconds, const cr::time_zone*>>);
229+
}
230+
}
231+
{ // TimeZonePtr
232+
{
233+
cr::zoned_time zt{
234+
cr::locate_zone("UTC"),
235+
cr::zoned_time<cr::nanoseconds, offset_time_zone<offset_time_zone_flags::has_default_zone>>{}};
236+
static_assert(std::same_as<decltype(zt), cr::zoned_time<cr::nanoseconds, const cr::time_zone*>>);
237+
}
238+
{
239+
cr::zoned_time zt{cr::locate_zone("UTC"),
240+
cr::zoned_time<cr::days, offset_time_zone<offset_time_zone_flags::has_default_zone>>{},
241+
cr::choose::earliest};
242+
static_assert(std::same_as<decltype(zt), cr::zoned_time<cr::seconds, const cr::time_zone*>>);
243+
}
244+
}
245+
}
246+
247+
return 0;
248+
}

0 commit comments

Comments
 (0)