1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 // UNSUPPORTED: no-filesystem, no-localization, no-tzdb
12 // XFAIL: libcpp-has-no-experimental-tzdb
13 // XFAIL: availability-tzdb-missing
17 // template<class Duration, class TimeZonePtr = const time_zone*>
20 // zoned_time(const sys_time<Duration>& st);
25 #include <type_traits>
27 #include "../test_offset_time_zone.h"
29 static void test_construction() {
30 static_assert(std::constructible_from
<std::chrono::zoned_time
<std::chrono::seconds
>, std::chrono::sys_seconds
>);
31 std::chrono::zoned_time
<std::chrono::seconds
> zt
{std::chrono::sys_seconds
{std::chrono::seconds
{42}}};
32 assert(zt
.get_time_zone() == std::chrono::locate_zone("UTC"));
33 assert(zt
.get_sys_time() == std::chrono::sys_seconds
{std::chrono::seconds
{42}});
36 static void test_conversion() {
37 static_assert(std::convertible_to
<std::chrono::sys_seconds
, std::chrono::zoned_time
<std::chrono::seconds
>>);
38 std::chrono::zoned_time
<std::chrono::seconds
> zt
= std::chrono::sys_seconds
{std::chrono::seconds
{42}};
39 assert(zt
.get_time_zone() == std::chrono::locate_zone("UTC"));
40 assert(zt
.get_sys_time() == std::chrono::sys_seconds
{std::chrono::seconds
{42}});
43 static void test_duration_conversion() {
45 using duration
= std::chrono::nanoseconds
;
46 using time_point
= std::chrono::sys_time
<duration
>;
47 std::chrono::zoned_time
<duration
> zt
{time_point
{duration
{42}}};
48 assert(zt
.get_sys_time() == time_point
{duration
{42}});
51 using duration
= std::chrono::microseconds
;
52 using time_point
= std::chrono::sys_time
<duration
>;
53 std::chrono::zoned_time
<duration
> zt
{time_point
{duration
{42}}};
54 assert(zt
.get_sys_time() == time_point
{duration
{42}});
57 using duration
= std::chrono::milliseconds
;
58 using time_point
= std::chrono::sys_time
<duration
>;
59 std::chrono::zoned_time
<duration
> zt
{time_point
{duration
{42}}};
60 assert(zt
.get_sys_time() == time_point
{duration
{42}});
63 using duration
= std::chrono::seconds
;
64 using time_point
= std::chrono::sys_time
<duration
>;
65 std::chrono::zoned_time
<duration
> zt
{time_point
{duration
{42}}};
66 assert(zt
.get_sys_time() == time_point
{duration
{42}});
69 using duration
= std::chrono::days
;
70 using time_point
= std::chrono::sys_time
<duration
>;
71 std::chrono::zoned_time
<duration
> zt
{time_point
{duration
{42}}};
72 assert(zt
.get_sys_time() == std::chrono::sys_seconds
{std::chrono::days
{42}});
75 using duration
= std::chrono::weeks
;
76 using time_point
= std::chrono::sys_time
<duration
>;
77 std::chrono::zoned_time
<duration
> zt
{time_point
{duration
{42}}};
78 assert(zt
.get_sys_time() == std::chrono::sys_seconds
{std::chrono::weeks
{42}});
81 using duration
= std::chrono::months
;
82 using time_point
= std::chrono::sys_time
<duration
>;
83 std::chrono::zoned_time
<duration
> zt
{time_point
{duration
{42}}};
84 assert(zt
.get_sys_time() == std::chrono::sys_seconds
{std::chrono::months
{42}});
87 using duration
= std::chrono::years
;
88 using time_point
= std::chrono::sys_time
<duration
>;
89 std::chrono::zoned_time
<duration
> zt
{time_point
{duration
{42}}};
90 assert(zt
.get_sys_time() == std::chrono::sys_seconds
{std::chrono::years
{42}});
94 static void test_duration_constraints() {
95 static_assert(!std::constructible_from
<
96 std::chrono::zoned_time
<std::chrono::seconds
, offset_time_zone
<offset_time_zone_flags::none
>>,
97 std::chrono::sys_seconds
>);
100 using type
= offset_time_zone
<offset_time_zone_flags::has_default_zone
>;
102 std::constructible_from
<std::chrono::zoned_time
<std::chrono::seconds
, type
>, std::chrono::sys_seconds
>);
104 std::chrono::zoned_time
<std::chrono::seconds
, type
> zt
= std::chrono::sys_seconds
{std::chrono::seconds
{42}};
106 assert(zt
.get_time_zone().offset() == std::chrono::seconds
{0});
107 assert(zt
.get_sys_time() == std::chrono::sys_seconds
{std::chrono::seconds
{42}});
111 !std::constructible_from
<
112 std::chrono::zoned_time
<std::chrono::seconds
, offset_time_zone
<offset_time_zone_flags::has_locate_zone
>>,
113 std::chrono::sys_seconds
>);
116 using type
= offset_time_zone
<offset_time_zone_flags::both
>;
118 std::constructible_from
<std::chrono::zoned_time
<std::chrono::seconds
, type
>, std::chrono::sys_seconds
>);
120 std::chrono::zoned_time
<std::chrono::seconds
, type
> zt
= std::chrono::sys_seconds
{std::chrono::seconds
{42}};
122 assert(zt
.get_time_zone().offset() == std::chrono::seconds
{0});
123 assert(zt
.get_sys_time() == std::chrono::sys_seconds
{std::chrono::seconds
{42}});
128 // - the results of the constructed object,
129 // - conversion construction is possible,
130 // - Duration is converted to zoned_time<>::duration, and
131 // - whether the constructor's constraints are satisfied.
132 int main(int, char**) {
135 test_duration_conversion();
136 test_duration_constraints();