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// For information see https://libcxx.llvm.org/DesignDocs/TimeZone.html
11
12#ifndef _LIBCPP___CHRONO_TIME_ZONE_H
13#define _LIBCPP___CHRONO_TIME_ZONE_H
14
15#include <version>
16// Enable the contents of the header only when libc++ was built with experimental features enabled.
17#if _LIBCPP_HAS_EXPERIMENTAL_TZDB
18
19# include <__chrono/calendar.h>
20# include <__chrono/duration.h>
21# include <__chrono/exception.h>
22# include <__chrono/local_info.h>
23# include <__chrono/sys_info.h>
24# include <__chrono/system_clock.h>
25# include <__compare/strong_order.h>
26# include <__config>
27# include <__memory/unique_ptr.h>
28# include <__type_traits/common_type.h>
29# include <string_view>
30
31# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
32# pragma GCC system_header
33# endif
34
35_LIBCPP_PUSH_MACROS
36# include <__undef_macros>
37
38# if _LIBCPP_STD_VER >= 20 && _LIBCPP_HAS_TIME_ZONE_DATABASE && _LIBCPP_HAS_FILESYSTEM && _LIBCPP_HAS_LOCALIZATION
39
40_LIBCPP_BEGIN_NAMESPACE_STD
41_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
42
43namespace chrono {
44
45enum class choose { earliest, latest };
46
47class _LIBCPP_AVAILABILITY_TZDB time_zone {
48 _LIBCPP_HIDE_FROM_ABI time_zone() = default;
49
50public:
51 class __impl; // public so it can be used by make_unique.
52
53 // The "constructor".
54 //
55 // The default constructor is private to avoid the constructor from being
56 // part of the ABI. Instead use an __ugly_named function as an ABI interface,
57 // since that gives us the ability to change it in the future.
58 [[nodiscard]] _LIBCPP_EXPORTED_FROM_ABI static time_zone __create(unique_ptr<__impl>&& __p);
59
60 _LIBCPP_EXPORTED_FROM_ABI ~time_zone();
61
62 _LIBCPP_HIDE_FROM_ABI time_zone(time_zone&&) = default;
63 _LIBCPP_HIDE_FROM_ABI time_zone& operator=(time_zone&&) = default;
64
65 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI string_view name() const noexcept { return __name(); }
66
67 template <class _Duration>
68 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI sys_info get_info(const sys_time<_Duration>& __time) const {
69 return __get_info(chrono::time_point_cast<seconds>(__time));
70 }
71
72 template <class _Duration>
73 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI local_info get_info(const local_time<_Duration>& __time) const {
74 return __get_info(chrono::time_point_cast<seconds>(__time));
75 }
76
77 // We don't apply nodiscard here since this function throws on many inputs,
78 // so it could be used as a validation.
79 template <class _Duration>
80 _LIBCPP_HIDE_FROM_ABI sys_time<common_type_t<_Duration, seconds>> to_sys(const local_time<_Duration>& __time) const {
81 local_info __info = get_info(__time);
82 switch (__info.result) {
83 case local_info::unique:
84 return sys_time<common_type_t<_Duration, seconds>>{__time.time_since_epoch() - __info.first.offset};
85
86 case local_info::nonexistent:
87 chrono::__throw_nonexistent_local_time(__time, __info);
88
89 case local_info::ambiguous:
90 chrono::__throw_ambiguous_local_time(__time, __info);
91 }
92
93 // TODO TZDB The Standard does not specify anything in these cases.
94 _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
95 __info.result != -1, "cannot convert the local time; it would be before the minimum system clock value");
96 _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
97 __info.result != -2, "cannot convert the local time; it would be after the maximum system clock value");
98
99 return {};
100 }
101
102 template <class _Duration>
103 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI sys_time<common_type_t<_Duration, seconds>>
104 to_sys(const local_time<_Duration>& __time, choose __z) const {
105 local_info __info = get_info(__time);
106 switch (__info.result) {
107 case local_info::unique: // first and second are the same
108 return sys_time<common_type_t<_Duration, seconds>>{__time.time_since_epoch() - __info.first.offset};
109
110 case local_info::nonexistent:
111 // first and second are the same
112 // All non-existing values are converted to the same time.
113 return sys_time<common_type_t<_Duration, seconds>>{__info.first.end};
114
115 case local_info::ambiguous:
116 switch (__z) {
117 case choose::earliest:
118 return sys_time<common_type_t<_Duration, seconds>>{__time.time_since_epoch() - __info.first.offset};
119
120 case choose::latest:
121 return sys_time<common_type_t<_Duration, seconds>>{__time.time_since_epoch() - __info.second.offset};
122
123 // Note a value out of bounds is not specified.
124 }
125 }
126
127 // TODO TZDB The standard does not specify anything in these cases.
128 _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
129 __info.result != -1, "cannot convert the local time; it would be before the minimum system clock value");
130 _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
131 __info.result != -2, "cannot convert the local time; it would be after the maximum system clock value");
132
133 return {};
134 }
135
136 template <class _Duration>
137 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI local_time<common_type_t<_Duration, seconds>>
138 to_local(const sys_time<_Duration>& __time) const {
139 using _Dp = common_type_t<_Duration, seconds>;
140
141 sys_info __info = get_info(__time);
142
143 _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
144 __info.offset >= chrono::seconds{0} || __time.time_since_epoch() >= _Dp::min() - __info.offset,
145 "cannot convert the system time; it would be before the minimum local clock value");
146
147 _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
148 __info.offset <= chrono::seconds{0} || __time.time_since_epoch() <= _Dp::max() - __info.offset,
149 "cannot convert the system time; it would be after the maximum local clock value");
150
151 return local_time<_Dp>{__time.time_since_epoch() + __info.offset};
152 }
153
154 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI const __impl& __implementation() const noexcept { return *__impl_; }
155
156private:
157 [[nodiscard]] _LIBCPP_EXPORTED_FROM_ABI string_view __name() const noexcept;
158
159 [[nodiscard]] _LIBCPP_AVAILABILITY_TZDB _LIBCPP_EXPORTED_FROM_ABI sys_info __get_info(sys_seconds __time) const;
160 [[nodiscard]] _LIBCPP_AVAILABILITY_TZDB _LIBCPP_EXPORTED_FROM_ABI local_info __get_info(local_seconds __time) const;
161
162 unique_ptr<__impl> __impl_;
163};
164
165[[nodiscard]] _LIBCPP_AVAILABILITY_TZDB _LIBCPP_HIDE_FROM_ABI inline bool
166operator==(const time_zone& __x, const time_zone& __y) noexcept {
167 return __x.name() == __y.name();
168}
169
170[[nodiscard]] _LIBCPP_AVAILABILITY_TZDB _LIBCPP_HIDE_FROM_ABI inline strong_ordering
171operator<=>(const time_zone& __x, const time_zone& __y) noexcept {
172 return __x.name() <=> __y.name();
173}
174
175} // namespace chrono
176
177_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
178_LIBCPP_END_NAMESPACE_STD
179
180# endif // _LIBCPP_STD_VER >= 20 && _LIBCPP_HAS_TIME_ZONE_DATABASE && _LIBCPP_HAS_FILESYSTEM &&
181 // _LIBCPP_HAS_LOCALIZATION
182
183_LIBCPP_POP_MACROS
184
185#endif // _LIBCPP_HAS_EXPERIMENTAL_TZDB
186
187#endif // _LIBCPP___CHRONO_TIME_ZONE_H
188