| 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 <cassert> |
| 24 | #include <concepts> |
| 25 | #include <type_traits> |
| 26 | |
| 27 | #include "../test_offset_time_zone.h" |
| 28 | |
| 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}}); |
| 34 | } |
| 35 | |
| 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}}); |
| 41 | } |
| 42 | |
| 43 | static void test_duration_conversion() { |
| 44 | { |
| 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}}); |
| 49 | } |
| 50 | { |
| 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}}); |
| 55 | } |
| 56 | { |
| 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}}); |
| 61 | } |
| 62 | { |
| 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}}); |
| 67 | } |
| 68 | { |
| 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}}); |
| 73 | } |
| 74 | { |
| 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}}); |
| 79 | } |
| 80 | { |
| 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}}); |
| 85 | } |
| 86 | { |
| 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}}); |
| 91 | } |
| 92 | } |
| 93 | |
| 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>); |
| 98 | |
| 99 | { |
| 100 | using type = offset_time_zone<offset_time_zone_flags::has_default_zone>; |
| 101 | static_assert( |
| 102 | std::constructible_from<std::chrono::zoned_time<std::chrono::seconds, type>, std::chrono::sys_seconds>); |
| 103 | |
| 104 | std::chrono::zoned_time<std::chrono::seconds, type> zt = std::chrono::sys_seconds{std::chrono::seconds{42}}; |
| 105 | |
| 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}}); |
| 108 | } |
| 109 | |
| 110 | static_assert( |
| 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>); |
| 114 | |
| 115 | { |
| 116 | using type = offset_time_zone<offset_time_zone_flags::both>; |
| 117 | static_assert( |
| 118 | std::constructible_from<std::chrono::zoned_time<std::chrono::seconds, type>, std::chrono::sys_seconds>); |
| 119 | |
| 120 | std::chrono::zoned_time<std::chrono::seconds, type> zt = std::chrono::sys_seconds{std::chrono::seconds{42}}; |
| 121 | |
| 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}}); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // Verify: |
| 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**) { |
| 133 | test_construction(); |
| 134 | test_conversion(); |
| 135 | test_duration_conversion(); |
| 136 | test_duration_constraints(); |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |