Debian Codegraph @

libcxx/test/std/time/time.zone/time.zone.zonedtime/test_offset_time_zone.h

raw
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 <format>
17 #include <string_view>
18 #include <type_traits>
19
20 enum class offset_time_zone_flags {
21 none = 0,
22 has_default_zone = 1,
23 has_locate_zone = 2,
24 both = has_default_zone | has_locate_zone
25 };
26
27 // The enforcement of the flags is done in the zoned_traits
28 template <offset_time_zone_flags flags = offset_time_zone_flags::both>
29 class offset_time_zone {
30 public:
31 offset_time_zone() : offset_{std::chrono::seconds{0}} {}
32 explicit offset_time_zone(std::string_view name) {
33 int count;
34 const char* begin = name.data();
35 const char* end = begin + name.size();
36 std::from_chars_result result = std::from_chars(begin, end, count);
37 assert(result == std::from_chars_result(end, std::errc{}));
38
39 offset_ = std::chrono::seconds(count);
40 }
41
42 std::chrono::seconds offset() const { return offset_; }
43
44 offset_time_zone* operator->() { return this; }
45
46 const offset_time_zone* operator->() const { return this; }
47
48 template <class Duration>
49 std::chrono::sys_time<std::common_type_t<Duration, std::chrono::seconds>>
50 to_sys(const std::chrono::local_time<Duration>& local) const {
51 return std::chrono::sys_time<std::common_type_t<Duration, std::chrono::seconds>>{
52 local.time_since_epoch() + offset_};
53 }
54
55 template <class Duration>
56 std::chrono::local_time<std::common_type_t<Duration, std::chrono::seconds>>
57 to_local(const std::chrono::sys_time<Duration>& sys) const {
58 return std::chrono::local_time<std::common_type_t<Duration, std::chrono::seconds>>{
59 sys.time_since_epoch() - offset_};
60 }
61
62 template <class Duration>
63 std::chrono::sys_info get_info(const std::chrono::sys_time<Duration>&) const {
64 return {std::chrono::sys_seconds::min(),
65 std::chrono::sys_seconds::max(),
66 offset_,
67 std::chrono::minutes{0},
68 std::format("{:+03d}s", offset_.count())};
69 }
70
71 private:
72 std::chrono::seconds offset_;
73 };
74
75 template <>
76 struct std::chrono::zoned_traits<offset_time_zone<offset_time_zone_flags::has_default_zone>> {
77 using type = offset_time_zone<offset_time_zone_flags::has_default_zone>;
78
79 static type default_zone() { return {}; }
80 };
81
82 template <>
83 struct std::chrono::zoned_traits<offset_time_zone<offset_time_zone_flags::has_locate_zone>> {
84 using type = offset_time_zone<offset_time_zone_flags::has_locate_zone>;
85
86 static type locate_zone(std::string_view name) { return type{name}; }
87 };
88
89 template <>
90 struct std::chrono::zoned_traits<offset_time_zone<offset_time_zone_flags::both>> {
91 using type = offset_time_zone<offset_time_zone_flags::both>;
92
93 static type default_zone() { return {}; }
94 static type locate_zone(std::string_view name) { return type{name}; }
95 };
96
97 #endif // TEST_STD_TIME_TIME_ZONE_TIME_ZONE_ZONEDTIME_TEST_OFFSET_TIME_ZONE_H