Debian Codegraph @

libcxx/test/std/time/time.zone/time.zone.zonedtime/time.zone.zonedtime.ctor/sys_time.pass.cpp

raw
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 sys_time<Duration>& st);
21
22 #include <chrono>
23 #include <concepts>
24 #include <type_traits>
25
26 #include "../test_offset_time_zone.h"
27
28 static void test_construction() {
29 static_assert(std::constructible_from<std::chrono::zoned_time<std::chrono::seconds>, std::chrono::sys_seconds>);
30 std::chrono::zoned_time<std::chrono::seconds> zt{std::chrono::sys_seconds{std::chrono::seconds{42}}};
31 assert(zt.get_time_zone() == std::chrono::locate_zone("UTC"));
32 assert(zt.get_sys_time() == std::chrono::sys_seconds{std::chrono::seconds{42}});
33 }
34
35 static void test_conversion() {
36 static_assert(std::convertible_to<std::chrono::sys_seconds, std::chrono::zoned_time<std::chrono::seconds>>);
37 std::chrono::zoned_time<std::chrono::seconds> zt = std::chrono::sys_seconds{std::chrono::seconds{42}};
38 assert(zt.get_time_zone() == std::chrono::locate_zone("UTC"));
39 assert(zt.get_sys_time() == std::chrono::sys_seconds{std::chrono::seconds{42}});
40 }
41
42 static void test_duration_conversion() {
43 {
44 using duration = std::chrono::nanoseconds;
45 using time_point = std::chrono::sys_time<duration>;
46 std::chrono::zoned_time<duration> zt{time_point{duration{42}}};
47 assert(zt.get_sys_time() == time_point{duration{42}});
48 }
49 {
50 using duration = std::chrono::microseconds;
51 using time_point = std::chrono::sys_time<duration>;
52 std::chrono::zoned_time<duration> zt{time_point{duration{42}}};
53 assert(zt.get_sys_time() == time_point{duration{42}});
54 }
55 {
56 using duration = std::chrono::milliseconds;
57 using time_point = std::chrono::sys_time<duration>;
58 std::chrono::zoned_time<duration> zt{time_point{duration{42}}};
59 assert(zt.get_sys_time() == time_point{duration{42}});
60 }
61 {
62 using duration = std::chrono::seconds;
63 using time_point = std::chrono::sys_time<duration>;
64 std::chrono::zoned_time<duration> zt{time_point{duration{42}}};
65 assert(zt.get_sys_time() == time_point{duration{42}});
66 }
67 {
68 using duration = std::chrono::days;
69 using time_point = std::chrono::sys_time<duration>;
70 std::chrono::zoned_time<duration> zt{time_point{duration{42}}};
71 assert(zt.get_sys_time() == std::chrono::sys_seconds{std::chrono::days{42}});
72 }
73 {
74 using duration = std::chrono::weeks;
75 using time_point = std::chrono::sys_time<duration>;
76 std::chrono::zoned_time<duration> zt{time_point{duration{42}}};
77 assert(zt.get_sys_time() == std::chrono::sys_seconds{std::chrono::weeks{42}});
78 }
79 {
80 using duration = std::chrono::months;
81 using time_point = std::chrono::sys_time<duration>;
82 std::chrono::zoned_time<duration> zt{time_point{duration{42}}};
83 assert(zt.get_sys_time() == std::chrono::sys_seconds{std::chrono::months{42}});
84 }
85 {
86 using duration = std::chrono::years;
87 using time_point = std::chrono::sys_time<duration>;
88 std::chrono::zoned_time<duration> zt{time_point{duration{42}}};
89 assert(zt.get_sys_time() == std::chrono::sys_seconds{std::chrono::years{42}});
90 }
91 }
92
93 static void test_duration_constraints() {
94 static_assert(!std::constructible_from<
95 std::chrono::zoned_time<std::chrono::seconds, offset_time_zone<offset_time_zone_flags::none>>,
96 std::chrono::sys_seconds>);
97
98 {
99 using type = offset_time_zone<offset_time_zone_flags::has_default_zone>;
100 static_assert(
101 std::constructible_from<std::chrono::zoned_time<std::chrono::seconds, type>, std::chrono::sys_seconds>);
102
103 std::chrono::zoned_time<std::chrono::seconds, type> zt = std::chrono::sys_seconds{std::chrono::seconds{42}};
104
105 assert(zt.get_time_zone().offset() == std::chrono::seconds{0});
106 assert(zt.get_sys_time() == std::chrono::sys_seconds{std::chrono::seconds{42}});
107 }
108
109 static_assert(
110 !std::constructible_from<
111 std::chrono::zoned_time<std::chrono::seconds, offset_time_zone<offset_time_zone_flags::has_locate_zone>>,
112 std::chrono::sys_seconds>);
113
114 {
115 using type = offset_time_zone<offset_time_zone_flags::both>;
116 static_assert(
117 std::constructible_from<std::chrono::zoned_time<std::chrono::seconds, type>, std::chrono::sys_seconds>);
118
119 std::chrono::zoned_time<std::chrono::seconds, type> zt = std::chrono::sys_seconds{std::chrono::seconds{42}};
120
121 assert(zt.get_time_zone().offset() == std::chrono::seconds{0});
122 assert(zt.get_sys_time() == std::chrono::sys_seconds{std::chrono::seconds{42}});
123 }
124 }
125
126 // Verify:
127 // - the results of the constructed object,
128 // - conversion construction is possible,
129 // - Duration is converted to zoned_time<>::duration, and
130 // - whether the constructor's constraints are satisfied.
131 int main(int, char**) {
132 test_construction();
133 test_conversion();
134 test_duration_conversion();
135 test_duration_constraints();
136
137 return 0;
138 }