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 _LIBCPP___CHRONO_CONVERT_TO_TM_H
11#define _LIBCPP___CHRONO_CONVERT_TO_TM_H
12
13#include <__chrono/calendar.h>
14#include <__chrono/concepts.h>
15#include <__chrono/day.h>
16#include <__chrono/duration.h>
17#include <__chrono/file_clock.h>
18#include <__chrono/gps_clock.h>
19#include <__chrono/hh_mm_ss.h>
20#include <__chrono/local_info.h>
21#include <__chrono/month.h>
22#include <__chrono/month_weekday.h>
23#include <__chrono/monthday.h>
24#include <__chrono/statically_widen.h>
25#include <__chrono/sys_info.h>
26#include <__chrono/system_clock.h>
27#include <__chrono/tai_clock.h>
28#include <__chrono/time_point.h>
29#include <__chrono/utc_clock.h>
30#include <__chrono/weekday.h>
31#include <__chrono/year.h>
32#include <__chrono/year_month.h>
33#include <__chrono/year_month_day.h>
34#include <__chrono/year_month_weekday.h>
35#include <__chrono/zoned_time.h>
36#include <__concepts/same_as.h>
37#include <__config>
38#include <__format/format_error.h>
39#include <__memory/addressof.h>
40#include <__type_traits/common_type.h>
41#include <__type_traits/is_convertible.h>
42#include <__type_traits/is_specialization.h>
43#include <cstdint>
44#include <ctime>
45#include <limits>
46
47#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
48# pragma GCC system_header
49#endif
50
51_LIBCPP_PUSH_MACROS
52#include <__undef_macros>
53
54_LIBCPP_BEGIN_NAMESPACE_STD
55
56#if _LIBCPP_STD_VER >= 20
57
58// Conerts a chrono date and weekday to a given _Tm type.
59//
60// This is an implementation detail for the function
61// template <class _Tm, class _ChronoT>
62// _Tm __convert_to_tm(const _ChronoT& __value)
63//
64// This manually converts the two values to the proper type. It is possible to
65// convert from sys_days to time_t and then to _Tm. But this leads to the Y2K
66// bug when time_t is a 32-bit signed integer. Chrono considers years beyond
67// the year 2038 valid, so instead do the transformation manually.
68template <class _Tm, class _Date>
69 requires(same_as<_Date, chrono::year_month_day> || same_as<_Date, chrono::year_month_day_last>)
70_LIBCPP_HIDE_FROM_ABI _Tm __convert_to_tm(const _Date& __date, chrono::weekday __weekday) {
71 _Tm __result = {};
72# ifdef __GLIBC__
73 __result.tm_zone = "UTC";
74# endif
75 __result.tm_year = static_cast<int>(__date.year()) - 1900;
76 __result.tm_mon = static_cast<unsigned>(__date.month()) - 1;
77 __result.tm_mday = static_cast<unsigned>(__date.day());
78 __result.tm_wday = static_cast<unsigned>(__weekday.c_encoding());
79 __result.tm_yday =
80 (static_cast<chrono::sys_days>(__date) -
81 static_cast<chrono::sys_days>(chrono::year_month_day{__date.year(), chrono::January, chrono::day{1}}))
82 .count();
83
84 return __result;
85}
86
87template <class _Tm, class _Duration>
88_LIBCPP_HIDE_FROM_ABI _Tm __convert_to_tm(const chrono::sys_time<_Duration> __tp) {
89 chrono::sys_days __days = chrono::floor<chrono::days>(__tp);
90 chrono::year_month_day __ymd{__days};
91
92 _Tm __result = std::__convert_to_tm<_Tm>(chrono::year_month_day{__ymd}, chrono::weekday{__days});
93
94 uint64_t __sec =
95 chrono::duration_cast<chrono::seconds>(__tp - chrono::time_point_cast<chrono::seconds>(t: __days)).count();
96 __sec %= 24 * 3600;
97 __result.tm_hour = __sec / 3600;
98 __sec %= 3600;
99 __result.tm_min = __sec / 60;
100 __result.tm_sec = __sec % 60;
101
102 return __result;
103}
104
105# if _LIBCPP_HAS_TIME_ZONE_DATABASE && _LIBCPP_HAS_FILESYSTEM && _LIBCPP_HAS_LOCALIZATION
106# if _LIBCPP_HAS_EXPERIMENTAL_TZDB
107
108template <class _Tm, class _Duration>
109_LIBCPP_HIDE_FROM_ABI _Tm __convert_to_tm(chrono::utc_time<_Duration> __tp) {
110 _Tm __result = std::__convert_to_tm<_Tm>(chrono::utc_clock::to_sys(__tp));
111
112 if (chrono::get_leap_second_info(__tp).is_leap_second)
113 ++__result.tm_sec;
114
115 return __result;
116}
117
118template <class _Tm, class _Duration>
119_LIBCPP_HIDE_FROM_ABI _Tm __convert_to_tm(chrono::tai_time<_Duration> __tp) {
120 using _Rp = common_type_t<_Duration, chrono::seconds>;
121 // The time between the TAI epoch (1958-01-01) and UNIX epoch (1970-01-01).
122 // This avoids leap second conversion when going from TAI to UTC.
123 // (It also avoids issues when the date is before the UTC epoch.)
124 constexpr chrono::seconds __offset{4383 * 24 * 60 * 60};
125 return std::__convert_to_tm<_Tm>(chrono::sys_time<_Rp>{__tp.time_since_epoch() - __offset});
126}
127
128template <class _Tm, class _Duration>
129_LIBCPP_HIDE_FROM_ABI _Tm __convert_to_tm(chrono::gps_time<_Duration> __tp) {
130 using _Rp = common_type_t<_Duration, chrono::seconds>;
131 // The time between the GPS epoch (1980-01-06) and UNIX epoch (1970-01-01).
132 constexpr chrono::seconds __offset{3657 * 24 * 60 * 60};
133 return std::__convert_to_tm<_Tm>(chrono::sys_time<_Rp>{__tp.time_since_epoch() + __offset});
134}
135
136# endif // _LIBCPP_HAS_EXPERIMENTAL_TZDB
137# endif // _LIBCPP_HAS_TIME_ZONE_DATABASE && _LIBCPP_HAS_FILESYSTEM && _LIBCPP_HAS_LOCALIZATION
138
139// Convert a chrono (calendar) time point, or dururation to the given _Tm type,
140// which must have the same properties as std::tm.
141template <class _Tm, class _ChronoT>
142_LIBCPP_HIDE_FROM_ABI _Tm __convert_to_tm(const _ChronoT& __value) {
143 _Tm __result = {};
144# ifdef __GLIBC__
145 __result.tm_zone = "UTC";
146# endif
147
148 if constexpr (__is_time_point<_ChronoT>) {
149 if constexpr (same_as<typename _ChronoT::clock, chrono::file_clock>)
150 return std::__convert_to_tm<_Tm>(_ChronoT::clock::to_sys(__value));
151 else if constexpr (same_as<typename _ChronoT::clock, chrono::local_t>)
152 return std::__convert_to_tm<_Tm>(chrono::sys_time<typename _ChronoT::duration>{__value.time_since_epoch()});
153 else {
154 // Note that some clocks have specializations __convert_to_tm for their
155 // time_point. These don't need to be added here. They do not trigger
156 // this assert.
157 static_assert(sizeof(_ChronoT) == 0, "TODO: Add the missing clock specialization");
158 }
159 } else if constexpr (chrono::__is_duration_v<_ChronoT>) {
160 // [time.format]/6
161 // ... However, if a flag refers to a "time of day" (e.g. %H, %I, %p,
162 // etc.), then a specialization of duration is interpreted as the time of
163 // day elapsed since midnight.
164
165 // Not all values can be converted to hours, it may run into ratio
166 // conversion errors. In that case the conversion to seconds works.
167 if constexpr (is_convertible_v<_ChronoT, chrono::hours>) {
168 auto __hour = chrono::floor<chrono::hours>(__value);
169 auto __sec = chrono::duration_cast<chrono::seconds>(__value - __hour);
170 __result.tm_hour = __hour.count() % 24;
171 __result.tm_min = __sec.count() / 60;
172 __result.tm_sec = __sec.count() % 60;
173 } else {
174 uint64_t __sec = chrono::duration_cast<chrono::seconds>(__value).count();
175 __sec %= 24 * 3600;
176 __result.tm_hour = __sec / 3600;
177 __sec %= 3600;
178 __result.tm_min = __sec / 60;
179 __result.tm_sec = __sec % 60;
180 }
181 } else if constexpr (same_as<_ChronoT, chrono::day>)
182 __result.tm_mday = static_cast<unsigned>(__value);
183 else if constexpr (same_as<_ChronoT, chrono::month>)
184 __result.tm_mon = static_cast<unsigned>(__value) - 1;
185 else if constexpr (same_as<_ChronoT, chrono::year>)
186 __result.tm_year = static_cast<int>(__value) - 1900;
187 else if constexpr (same_as<_ChronoT, chrono::weekday>)
188 __result.tm_wday = __value.c_encoding();
189 else if constexpr (same_as<_ChronoT, chrono::weekday_indexed> || same_as<_ChronoT, chrono::weekday_last>)
190 __result.tm_wday = __value.weekday().c_encoding();
191 else if constexpr (same_as<_ChronoT, chrono::month_day>) {
192 __result.tm_mday = static_cast<unsigned>(__value.day());
193 __result.tm_mon = static_cast<unsigned>(__value.month()) - 1;
194 } else if constexpr (same_as<_ChronoT, chrono::month_day_last>) {
195 __result.tm_mon = static_cast<unsigned>(__value.month()) - 1;
196 } else if constexpr (same_as<_ChronoT, chrono::month_weekday> || same_as<_ChronoT, chrono::month_weekday_last>) {
197 __result.tm_wday = __value.weekday_indexed().weekday().c_encoding();
198 __result.tm_mon = static_cast<unsigned>(__value.month()) - 1;
199 } else if constexpr (same_as<_ChronoT, chrono::year_month>) {
200 __result.tm_year = static_cast<int>(__value.year()) - 1900;
201 __result.tm_mon = static_cast<unsigned>(__value.month()) - 1;
202 } else if constexpr (same_as<_ChronoT, chrono::year_month_day> || same_as<_ChronoT, chrono::year_month_day_last>) {
203 return std::__convert_to_tm<_Tm>(
204 chrono::year_month_day{__value}, chrono::weekday{static_cast<chrono::sys_days>(__value)});
205 } else if constexpr (same_as<_ChronoT, chrono::year_month_weekday> ||
206 same_as<_ChronoT, chrono::year_month_weekday_last>) {
207 return std::__convert_to_tm<_Tm>(chrono::year_month_day{static_cast<chrono::sys_days>(__value)}, __value.weekday());
208 } else if constexpr (__is_hh_mm_ss<_ChronoT>) {
209 __result.tm_sec = __value.seconds().count();
210 __result.tm_min = __value.minutes().count();
211 // In libc++ hours is stored as a long. The type in std::tm is an int. So
212 // the overflow can only occur when hour uses more bits than an int
213 // provides.
214 if constexpr (sizeof(std::chrono::hours::rep) > sizeof(__result.tm_hour))
215 if (__value.hours().count() > std::numeric_limits<decltype(__result.tm_hour)>::max())
216 std::__throw_format_error(s: "Formatting hh_mm_ss, encountered an hour overflow");
217 __result.tm_hour = __value.hours().count();
218# if _LIBCPP_HAS_EXPERIMENTAL_TZDB
219 } else if constexpr (same_as<_ChronoT, chrono::sys_info>) {
220 // Has no time information.
221 } else if constexpr (same_as<_ChronoT, chrono::local_info>) {
222 // Has no time information.
223# if _LIBCPP_HAS_TIME_ZONE_DATABASE && _LIBCPP_HAS_FILESYSTEM && _LIBCPP_HAS_LOCALIZATION
224 } else if constexpr (__is_specialization_v<_ChronoT, chrono::zoned_time>) {
225 return std::__convert_to_tm<_Tm>(
226 chrono::sys_time<typename _ChronoT::duration>{__value.get_local_time().time_since_epoch()});
227# endif
228# endif // _LIBCPP_HAS_EXPERIMENTAL_TZDB
229 } else
230 static_assert(sizeof(_ChronoT) == 0, "Add the missing type specialization");
231
232 return __result;
233}
234
235#endif // if _LIBCPP_STD_VER >= 20
236
237_LIBCPP_END_NAMESPACE_STD
238
239_LIBCPP_POP_MACROS
240
241#endif // _LIBCPP___CHRONO_CONVERT_TO_TM_H
242