Skip to content

Commit d0b2016

Browse files
committed
[libc++][TZDB] Adds basics of zoned_time class.
This implements the class, its non-templated constructors and its getters to verify the construction. Completes - LWG3224 zoned_time constructor from TimeZonePtr does not specify initialization of tp_ Implements parts of: - P0355 Extending chrono to Calendars and Time Zones
1 parent 151bd7c commit d0b2016

File tree

16 files changed

+830
-2
lines changed

16 files changed

+830
-2
lines changed

libcxx/docs/Status/Cxx20Issues.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@
165165
"`3225 <https://wg21.link/LWG3225>`__","``zoned_time``\ converting constructor shall not be ``noexcept``\ ","Belfast","","","|chrono|"
166166
"`3190 <https://wg21.link/LWG3190>`__","``std::allocator::allocate``\ sometimes returns too little storage","Belfast","|Complete|","14.0"
167167
"`3218 <https://wg21.link/LWG3218>`__","Modifier for ``%d``\ parse flag does not match POSIX and ``format``\ specification","Belfast","","","|chrono| |format|"
168-
"`3224 <https://wg21.link/LWG3224>`__","``zoned_time``\ constructor from ``TimeZonePtr``\ does not specify initialization of ``tp_``\ ","Belfast","","","|chrono|"
168+
"`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|"
170170
"`3232 <https://wg21.link/LWG3232>`__","Inconsistency in ``zoned_time``\ deduction guides","Belfast","","","|chrono|"
171171
"`3222 <https://wg21.link/LWG3222>`__","P0574R1 introduced preconditions on non-existent parameters","Belfast","",""

libcxx/include/__chrono/zoned_time.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,22 @@
1616
// Enable the contents of the header only when libc++ was built with experimental features enabled.
1717
#if !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_TZDB)
1818

19+
# include <__chrono/duration.h>
20+
# include <__chrono/system_clock.h>
1921
# include <__chrono/time_zone.h>
2022
# include <__chrono/tzdb_list.h>
2123
# include <__config>
2224
# include <__fwd/string_view.h>
25+
# include <__type_traits/common_type.h>
26+
# include <__utility/move.h>
2327

2428
# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2529
# pragma GCC system_header
2630
# endif
2731

32+
_LIBCPP_PUSH_MACROS
33+
# include <__undef_macros>
34+
2835
_LIBCPP_BEGIN_NAMESPACE_STD
2936

3037
# if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_TIME_ZONE_DATABASE) && !defined(_LIBCPP_HAS_NO_FILESYSTEM) && \
@@ -43,13 +50,55 @@ struct zoned_traits<const time_zone*> {
4350
}
4451
};
4552

53+
template <class _Duration, class _TimeZonePtr = const time_zone*>
54+
class zoned_time {
55+
// [time.zone.zonedtime.ctor]/2
56+
static_assert(__is_duration<_Duration>::value,
57+
"the program is ill-formed since _Duration is not a specialization of std::chrono::duration");
58+
59+
using __traits = zoned_traits<_TimeZonePtr>;
60+
61+
public:
62+
using duration = common_type_t<_Duration, seconds>;
63+
64+
_LIBCPP_HIDE_FROM_ABI zoned_time()
65+
requires requires { __traits::default_zone(); }
66+
: __zone_{__traits::default_zone()}, __tp_{} {}
67+
68+
_LIBCPP_HIDE_FROM_ABI zoned_time(const zoned_time&) = default;
69+
_LIBCPP_HIDE_FROM_ABI zoned_time& operator=(const zoned_time&) = default;
70+
71+
_LIBCPP_HIDE_FROM_ABI zoned_time(const sys_time<_Duration>& __tp)
72+
requires requires { __traits::default_zone(); }
73+
: __zone_{__traits::default_zone()}, __tp_{__tp} {}
74+
75+
_LIBCPP_HIDE_FROM_ABI explicit zoned_time(_TimeZonePtr __zone) : __zone_{std::move(__zone)}, __tp_{} {}
76+
77+
_LIBCPP_HIDE_FROM_ABI explicit zoned_time(string_view __name)
78+
requires(requires { __traits::locate_zone(string_view{}); } &&
79+
// constructible_from<zoned_time, decltype(__traits::locate_zone(string_view{}))>
80+
// would create a dependency on itself. Instead depend on the fact
81+
// a constructor taking a _TimeZonePtr exists.
82+
constructible_from<_TimeZonePtr, decltype(__traits::locate_zone(string_view{}))>)
83+
: __zone_{__traits::locate_zone(__name)}, __tp_{} {}
84+
85+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI _TimeZonePtr get_time_zone() const { return __zone_; }
86+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI sys_time<duration> get_sys_time() const { return __tp_; }
87+
88+
private:
89+
_TimeZonePtr __zone_;
90+
sys_time<duration> __tp_;
91+
};
92+
4693
} // namespace chrono
4794

4895
# endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_TIME_ZONE_DATABASE) && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
4996
// && !defined(_LIBCPP_HAS_NO_LOCALIZATION)
5097

5198
_LIBCPP_END_NAMESPACE_STD
5299

100+
_LIBCPP_POP_MACROS
101+
53102
#endif // !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_TZDB)
54103

55104
#endif // _LIBCPP___CHRONO_ZONED_TIME_H

libcxx/include/chrono

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,10 @@ strong_ordering operator<=>(const time_zone& x, const time_zone& y) noexcept;
789789
// [time.zone.zonedtraits], class template zoned_traits
790790
template<class T> struct zoned_traits; // C++20
791791
792+
// [time.zone.zonedtime], class template zoned_time
793+
template<class Duration, class TimeZonePtr = const time_zone*> // C++20
794+
class zoned_time;
795+
792796
// [time.zone.leap], leap second support
793797
class leap_second { // C++20
794798
public:

libcxx/modules/std/chrono.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,10 @@ export namespace std {
230230
// [time.zone.zonedtraits], class template zoned_traits
231231
using std::chrono::zoned_traits;
232232

233-
# if 0
234233
// [time.zone.zonedtime], class template zoned_time
235234
using std::chrono::zoned_time;
236235

236+
# if 0
237237
using std::chrono::zoned_seconds;
238238
# endif // if 0
239239

libcxx/test/libcxx/diagnostics/chrono.nodiscard.verify.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,10 @@ void test() {
7878
t::default_zone(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
7979
t::locate_zone(""); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
8080
}
81+
82+
{
83+
std::chrono::zoned_time<std::chrono::seconds> zt;
84+
zt.get_time_zone(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
85+
zt.get_sys_time(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
86+
}
8187
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
// template<class Duration, class TimeZonePtr = const time_zone*>
18+
// class zoned_time;
19+
//
20+
// zoned_time& operator=(const zoned_time&) = default;
21+
22+
#include <cassert>
23+
#include <chrono>
24+
25+
int main(int, char**) {
26+
std::chrono::zoned_time<std::chrono::seconds> zt{std::chrono::sys_seconds{std::chrono::seconds{42}}};
27+
assert(zt.get_time_zone() == std::chrono::locate_zone("UTC"));
28+
assert(zt.get_sys_time() == std::chrono::sys_seconds{std::chrono::seconds{42}});
29+
30+
std::chrono::zoned_time<std::chrono::seconds> copy;
31+
copy = zt;
32+
assert(copy.get_time_zone() == std::chrono::locate_zone("UTC"));
33+
assert(copy.get_sys_time() == std::chrono::sys_seconds{std::chrono::seconds{42}});
34+
35+
return 0;
36+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
// template<class Duration, class TimeZonePtr = const time_zone*>
18+
// class zoned_time;
19+
//
20+
// zoned_time(const zoned_time&) = default;
21+
22+
#include <cassert>
23+
#include <chrono>
24+
25+
int main(int, char**) {
26+
std::chrono::zoned_time<std::chrono::seconds> zt{std::chrono::sys_seconds{std::chrono::seconds{42}}};
27+
assert(zt.get_time_zone() == std::chrono::locate_zone("UTC"));
28+
assert(zt.get_sys_time() == std::chrono::sys_seconds{std::chrono::seconds{42}});
29+
30+
{
31+
std::chrono::zoned_time<std::chrono::seconds> copy{zt};
32+
assert(copy.get_time_zone() == std::chrono::locate_zone("UTC"));
33+
assert(copy.get_sys_time() == std::chrono::sys_seconds{std::chrono::seconds{42}});
34+
}
35+
36+
{
37+
std::chrono::zoned_time<std::chrono::seconds> copy = zt;
38+
assert(copy.get_time_zone() == std::chrono::locate_zone("UTC"));
39+
assert(copy.get_sys_time() == std::chrono::sys_seconds{std::chrono::seconds{42}});
40+
}
41+
42+
return 0;
43+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
// template<class Duration, class TimeZonePtr = const time_zone*>
18+
// class zoned_time;
19+
//
20+
// zoned_time();
21+
22+
#include <chrono>
23+
#include <concepts>
24+
#include <type_traits>
25+
26+
#include "test_offset_time_zone.h"
27+
28+
// Verify the results of the default constructed object,
29+
// and whether the constructor's constrains are satisfied.
30+
int main(int, char**) {
31+
{
32+
static_assert(std::default_initializable<std::chrono::zoned_time<std::chrono::seconds>>);
33+
std::chrono::zoned_time<std::chrono::seconds> zt;
34+
assert(zt.get_time_zone() == std::chrono::locate_zone("UTC"));
35+
assert(zt.get_sys_time() == std::chrono::sys_seconds{});
36+
}
37+
38+
static_assert(!std::default_initializable<
39+
std::chrono::zoned_time<std::chrono::seconds, offset_time_zone<offset_time_zone_flags::none>>>);
40+
41+
{
42+
using type = offset_time_zone<offset_time_zone_flags::has_default_zone>;
43+
static_assert(std::default_initializable<std::chrono::zoned_time<std::chrono::seconds, type>>);
44+
45+
std::chrono::zoned_time<std::chrono::seconds, type> zt;
46+
47+
assert(zt.get_time_zone().offset() == std::chrono::seconds{0});
48+
assert(zt.get_sys_time() == std::chrono::sys_seconds{});
49+
}
50+
51+
static_assert(
52+
!std::default_initializable<
53+
std::chrono::zoned_time<std::chrono::seconds, offset_time_zone<offset_time_zone_flags::has_locate_zone>>>);
54+
55+
{
56+
using type = offset_time_zone<offset_time_zone_flags::both>;
57+
static_assert(std::default_initializable<std::chrono::zoned_time<std::chrono::seconds, type>>);
58+
59+
std::chrono::zoned_time<std::chrono::seconds, type> zt;
60+
61+
assert(zt.get_time_zone().offset() == std::chrono::seconds{0});
62+
assert(zt.get_sys_time() == std::chrono::sys_seconds{});
63+
}
64+
65+
return 0;
66+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
// template<class Duration, class TimeZonePtr = const time_zone*>
18+
// class zoned_time;
19+
//
20+
// explicit zoned_time(string_view name);
21+
22+
#include <chrono>
23+
#include <concepts>
24+
25+
#include "test_offset_time_zone.h"
26+
27+
// Verify the results of the constructed object.
28+
int main(int, char**) {
29+
{
30+
using ptr = const std::chrono::time_zone*;
31+
static_assert(std::constructible_from<std::chrono::zoned_time<std::chrono::seconds, ptr>, std::string_view>);
32+
static_assert(!std::convertible_to<std::string_view, std::chrono::zoned_time<std::chrono::seconds, ptr>>);
33+
34+
std::chrono::zoned_time<std::chrono::seconds> zt{"UTC"};
35+
36+
assert(zt.get_time_zone() == std::chrono::locate_zone("UTC"));
37+
assert(zt.get_sys_time() == std::chrono::sys_seconds{});
38+
}
39+
40+
{
41+
using ptr = offset_time_zone<offset_time_zone_flags::none>;
42+
static_assert(!std::constructible_from<std::chrono::zoned_time<std::chrono::seconds, ptr>, std::string_view>);
43+
static_assert(!std::convertible_to<std::string_view, std::chrono::zoned_time<std::chrono::seconds, ptr>>);
44+
}
45+
46+
{
47+
using ptr = offset_time_zone<offset_time_zone_flags::has_default_zone>;
48+
static_assert(!std::constructible_from<std::chrono::zoned_time<std::chrono::seconds, ptr>, std::string_view>);
49+
static_assert(!std::convertible_to<std::string_view, std::chrono::zoned_time<std::chrono::seconds, ptr>>);
50+
}
51+
52+
{
53+
using ptr = offset_time_zone<offset_time_zone_flags::has_locate_zone>;
54+
static_assert(std::constructible_from<std::chrono::zoned_time<std::chrono::seconds, ptr>, std::string_view>);
55+
static_assert(!std::convertible_to<std::string_view, std::chrono::zoned_time<std::chrono::seconds, ptr>>);
56+
57+
ptr tz;
58+
std::chrono::zoned_time<std::chrono::seconds, ptr> zt{"42"};
59+
60+
assert(zt.get_time_zone().offset() == std::chrono::seconds{42});
61+
assert(zt.get_sys_time() == std::chrono::sys_seconds{});
62+
}
63+
64+
{
65+
using ptr = offset_time_zone<offset_time_zone_flags::both>;
66+
static_assert(std::constructible_from<std::chrono::zoned_time<std::chrono::seconds, ptr>, std::string_view>);
67+
static_assert(!std::convertible_to<std::string_view, std::chrono::zoned_time<std::chrono::seconds, ptr>>);
68+
69+
ptr tz;
70+
std::chrono::zoned_time<std::chrono::seconds, ptr> zt{"42"};
71+
72+
assert(zt.get_time_zone().offset() == std::chrono::seconds{42});
73+
assert(zt.get_sys_time() == std::chrono::sys_seconds{});
74+
}
75+
76+
return 0;
77+
}

0 commit comments

Comments
 (0)