2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
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
17 #include <string_view>
18 #include <system_error>
19 #include <type_traits>
21 enum class offset_time_zone_flags
{
25 both
= has_default_zone
| has_locate_zone
28 // The enforcement of the flags is done in the zoned_traits
29 template <offset_time_zone_flags flags
= offset_time_zone_flags::both
>
30 class offset_time_zone
{
32 offset_time_zone() : offset_
{std::chrono::seconds
{0}} {}
33 explicit offset_time_zone(std::string_view name
) {
35 const char* begin
= name
.data();
36 const char* end
= begin
+ name
.size();
37 std::from_chars_result result
= std::from_chars(begin
, end
, count
);
38 assert(result
== std::from_chars_result(end
, std::errc
{}));
40 offset_
= std::chrono::seconds(count
);
43 std::chrono::seconds
offset() const { return offset_
; }
45 offset_time_zone
* operator->() { return this; }
47 const offset_time_zone
* operator->() const { return this; }
49 template <class Duration
>
50 std::chrono::sys_time
<std::common_type_t
<Duration
, std::chrono::seconds
>>
51 to_sys(const std::chrono::local_time
<Duration
>& local
) const {
52 return std::chrono::sys_time
<std::common_type_t
<Duration
, std::chrono::seconds
>>{
53 local
.time_since_epoch() + offset_
};
56 template <class Duration
>
57 std::chrono::local_time
<std::common_type_t
<Duration
, std::chrono::seconds
>>
58 to_local(const std::chrono::sys_time
<Duration
>& sys
) const {
59 return std::chrono::local_time
<std::common_type_t
<Duration
, std::chrono::seconds
>>{
60 sys
.time_since_epoch() - offset_
};
63 template <class Duration
>
64 std::chrono::sys_info
get_info(const std::chrono::sys_time
<Duration
>&) const {
65 return {std::chrono::sys_seconds::min(),
66 std::chrono::sys_seconds::max(),
68 std::chrono::minutes
{0},
69 std::format("{:+03d}s", offset_
.count())};
73 std::chrono::seconds offset_
;
77 struct std::chrono::zoned_traits
<offset_time_zone
<offset_time_zone_flags::has_default_zone
>> {
78 using type
= offset_time_zone
<offset_time_zone_flags::has_default_zone
>;
80 static type
default_zone() { return {}; }
84 struct std::chrono::zoned_traits
<offset_time_zone
<offset_time_zone_flags::has_locate_zone
>> {
85 using type
= offset_time_zone
<offset_time_zone_flags::has_locate_zone
>;
87 static type
locate_zone(std::string_view name
) { return type
{name
}; }
91 struct std::chrono::zoned_traits
<offset_time_zone
<offset_time_zone_flags::both
>> {
92 using type
= offset_time_zone
<offset_time_zone_flags::both
>;
94 static type
default_zone() { return {}; }
95 static type
locate_zone(std::string_view name
) { return type
{name
}; }
98 #endif // TEST_STD_TIME_TIME_ZONE_TIME_ZONE_ZONEDTIME_TEST_OFFSET_TIME_ZONE_H