Skip to content

Commit 6beadca

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 9d6b68b commit 6beadca

File tree

16 files changed

+842
-2
lines changed

16 files changed

+842
-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: 51 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,57 @@ 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+
// The wording uses the constraint
80+
// constructible_from<zoned_time, decltype(__traits::locate_zone(string_view{}))>
81+
// Using this constraint in the code causes the compiler to give an
82+
// error the constraint depends on itself. To avoid that issue use
83+
// the fact it is possible to create this object from a _TimeZonePtr.
84+
constructible_from<_TimeZonePtr, decltype(__traits::locate_zone(string_view{}))>)
85+
: __zone_{__traits::locate_zone(__name)}, __tp_{} {}
86+
87+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI _TimeZonePtr get_time_zone() const { return __zone_; }
88+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI sys_time<duration> get_sys_time() const { return __tp_; }
89+
90+
private:
91+
_TimeZonePtr __zone_;
92+
sys_time<duration> __tp_;
93+
};
94+
4695
} // namespace chrono
4796

4897
# endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_TIME_ZONE_DATABASE) && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
4998
// && !defined(_LIBCPP_HAS_NO_LOCALIZATION)
5099

51100
_LIBCPP_END_NAMESPACE_STD
52101

102+
_LIBCPP_POP_MACROS
103+
53104
#endif // !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_TZDB)
54105

55106
#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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
#include <concepts>
25+
26+
int main(int, char**) {
27+
std::chrono::zoned_time<std::chrono::seconds> zt{std::chrono::sys_seconds{std::chrono::seconds{42}}};
28+
assert(zt.get_time_zone() == std::chrono::locate_zone("UTC"));
29+
assert(zt.get_sys_time() == std::chrono::sys_seconds{std::chrono::seconds{42}});
30+
31+
std::chrono::zoned_time<std::chrono::seconds> copy;
32+
copy = zt;
33+
std::same_as<std::chrono::zoned_time<std::chrono::seconds>&> decltype(auto) result = (copy = zt);
34+
assert(&result == &copy);
35+
assert(copy.get_time_zone() == std::chrono::locate_zone("UTC"));
36+
assert(copy.get_sys_time() == std::chrono::sys_seconds{std::chrono::seconds{42}});
37+
38+
return 0;
39+
}
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: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
#ifndef TEST_STD_TIME_TIME_ZONE_TIME_ZONE_ZONEDTIME_TEST_OFFSET_TIME_ZONE_H
11+
#define TEST_STD_TIME_TIME_ZONE_TIME_ZONE_ZONEDTIME_TEST_OFFSET_TIME_ZONE_H
12+
13+
#include <cassert>
14+
#include <charconv>
15+
#include <chrono>
16+
#include <string_view>
17+
18+
enum class offset_time_zone_flags {
19+
none = 0,
20+
has_default_zone = 1,
21+
has_locate_zone = 2,
22+
both = has_default_zone | has_locate_zone
23+
};
24+
25+
// The enforcement of the flags is done in the zoned_traits
26+
template <offset_time_zone_flags flags = offset_time_zone_flags::both>
27+
class offset_time_zone {
28+
public:
29+
offset_time_zone() : offset_{std::chrono::seconds{0}} {}
30+
explicit offset_time_zone(std::string_view name) {
31+
int count;
32+
const char* begin = name.data();
33+
const char* end = begin + name.size();
34+
std::from_chars_result result = std::from_chars(begin, end, count);
35+
assert(result == std::from_chars_result(end, std::errc{}));
36+
37+
offset_ = std::chrono::seconds(count);
38+
}
39+
40+
std::chrono::seconds offset() const { return offset_; }
41+
42+
private:
43+
std::chrono::seconds offset_;
44+
};
45+
46+
template <>
47+
struct std::chrono::zoned_traits<offset_time_zone<offset_time_zone_flags::has_default_zone>> {
48+
using type = offset_time_zone<offset_time_zone_flags::has_default_zone>;
49+
50+
static type default_zone() { return {}; }
51+
};
52+
53+
template <>
54+
struct std::chrono::zoned_traits<offset_time_zone<offset_time_zone_flags::has_locate_zone>> {
55+
using type = offset_time_zone<offset_time_zone_flags::has_locate_zone>;
56+
57+
static type locate_zone(std::string_view name) { return type{name}; }
58+
};
59+
60+
template <>
61+
struct std::chrono::zoned_traits<offset_time_zone<offset_time_zone_flags::both>> {
62+
using type = offset_time_zone<offset_time_zone_flags::both>;
63+
64+
static type default_zone() { return {}; }
65+
static type locate_zone(std::string_view name) { return type{name}; }
66+
};
67+
68+
#endif // TEST_STD_TIME_TIME_ZONE_TIME_ZONE_ZONEDTIME_TEST_OFFSET_TIME_ZONE_H
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 constraints 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+
}

0 commit comments

Comments
 (0)