xref: /freebsd/contrib/llvm-project/libcxx/include/__chrono/convert_to_tm.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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/hh_mm_ss.h>
19 #include <__chrono/local_info.h>
20 #include <__chrono/month.h>
21 #include <__chrono/month_weekday.h>
22 #include <__chrono/monthday.h>
23 #include <__chrono/statically_widen.h>
24 #include <__chrono/sys_info.h>
25 #include <__chrono/system_clock.h>
26 #include <__chrono/time_point.h>
27 #include <__chrono/weekday.h>
28 #include <__chrono/year.h>
29 #include <__chrono/year_month.h>
30 #include <__chrono/year_month_day.h>
31 #include <__chrono/year_month_weekday.h>
32 #include <__chrono/zoned_time.h>
33 #include <__concepts/same_as.h>
34 #include <__config>
35 #include <__format/format_error.h>
36 #include <__memory/addressof.h>
37 #include <__type_traits/is_convertible.h>
38 #include <__type_traits/is_specialization.h>
39 #include <cstdint>
40 #include <ctime>
41 #include <limits>
42 
43 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
44 #  pragma GCC system_header
45 #endif
46 
47 _LIBCPP_PUSH_MACROS
48 #include <__undef_macros>
49 
50 _LIBCPP_BEGIN_NAMESPACE_STD
51 
52 #if _LIBCPP_STD_VER >= 20
53 
54 // Conerts a chrono date and weekday to a given _Tm type.
55 //
56 // This is an implementation detail for the function
57 //   template <class _Tm, class _ChronoT>
58 //   _Tm __convert_to_tm(const _ChronoT& __value)
59 //
60 // This manually converts the two values to the proper type. It is possible to
61 // convert from sys_days to time_t and then to _Tm. But this leads to the Y2K
62 // bug when time_t is a 32-bit signed integer. Chrono considers years beyond
63 // the year 2038 valid, so instead do the transformation manually.
64 template <class _Tm, class _Date>
65   requires(same_as<_Date, chrono::year_month_day> || same_as<_Date, chrono::year_month_day_last>)
__convert_to_tm(const _Date & __date,chrono::weekday __weekday)66 _LIBCPP_HIDE_FROM_ABI _Tm __convert_to_tm(const _Date& __date, chrono::weekday __weekday) {
67   _Tm __result = {};
68 #  ifdef __GLIBC__
69   __result.tm_zone = "UTC";
70 #  endif
71   __result.tm_year = static_cast<int>(__date.year()) - 1900;
72   __result.tm_mon  = static_cast<unsigned>(__date.month()) - 1;
73   __result.tm_mday = static_cast<unsigned>(__date.day());
74   __result.tm_wday = static_cast<unsigned>(__weekday.c_encoding());
75   __result.tm_yday =
76       (static_cast<chrono::sys_days>(__date) -
77        static_cast<chrono::sys_days>(chrono::year_month_day{__date.year(), chrono::January, chrono::day{1}}))
78           .count();
79 
80   return __result;
81 }
82 
83 template <class _Tm, class _Duration>
__convert_to_tm(const chrono::sys_time<_Duration> __tp)84 _LIBCPP_HIDE_FROM_ABI _Tm __convert_to_tm(const chrono::sys_time<_Duration> __tp) {
85   chrono::sys_days __days = chrono::floor<chrono::days>(__tp);
86   chrono::year_month_day __ymd{__days};
87 
88   _Tm __result = std::__convert_to_tm<_Tm>(chrono::year_month_day{__ymd}, chrono::weekday{__days});
89 
90   uint64_t __sec =
91       chrono::duration_cast<chrono::seconds>(__tp - chrono::time_point_cast<chrono::seconds>(__days)).count();
92   __sec %= 24 * 3600;
93   __result.tm_hour = __sec / 3600;
94   __sec %= 3600;
95   __result.tm_min = __sec / 60;
96   __result.tm_sec = __sec % 60;
97 
98   return __result;
99 }
100 
101 // Convert a chrono (calendar) time point, or dururation to the given _Tm type,
102 // which must have the same properties as std::tm.
103 template <class _Tm, class _ChronoT>
__convert_to_tm(const _ChronoT & __value)104 _LIBCPP_HIDE_FROM_ABI _Tm __convert_to_tm(const _ChronoT& __value) {
105   _Tm __result = {};
106 #  ifdef __GLIBC__
107   __result.tm_zone = "UTC";
108 #  endif
109 
110   if constexpr (__is_time_point<_ChronoT>) {
111     if constexpr (same_as<typename _ChronoT::clock, chrono::system_clock>)
112       return std::__convert_to_tm<_Tm>(__value);
113     else if constexpr (same_as<typename _ChronoT::clock, chrono::file_clock>)
114       return std::__convert_to_tm<_Tm>(_ChronoT::clock::to_sys(__value));
115     else if constexpr (same_as<typename _ChronoT::clock, chrono::local_t>)
116       return std::__convert_to_tm<_Tm>(chrono::sys_time<typename _ChronoT::duration>{__value.time_since_epoch()});
117     else
118       static_assert(sizeof(_ChronoT) == 0, "TODO: Add the missing clock specialization");
119   } else if constexpr (chrono::__is_duration<_ChronoT>::value) {
120     // [time.format]/6
121     //   ...  However, if a flag refers to a "time of day" (e.g. %H, %I, %p,
122     //   etc.), then a specialization of duration is interpreted as the time of
123     //   day elapsed since midnight.
124 
125     // Not all values can be converted to hours, it may run into ratio
126     // conversion errors. In that case the conversion to seconds works.
127     if constexpr (is_convertible_v<_ChronoT, chrono::hours>) {
128       auto __hour      = chrono::floor<chrono::hours>(__value);
129       auto __sec       = chrono::duration_cast<chrono::seconds>(__value - __hour);
130       __result.tm_hour = __hour.count() % 24;
131       __result.tm_min  = __sec.count() / 60;
132       __result.tm_sec  = __sec.count() % 60;
133     } else {
134       uint64_t __sec = chrono::duration_cast<chrono::seconds>(__value).count();
135       __sec %= 24 * 3600;
136       __result.tm_hour = __sec / 3600;
137       __sec %= 3600;
138       __result.tm_min = __sec / 60;
139       __result.tm_sec = __sec % 60;
140     }
141   } else if constexpr (same_as<_ChronoT, chrono::day>)
142     __result.tm_mday = static_cast<unsigned>(__value);
143   else if constexpr (same_as<_ChronoT, chrono::month>)
144     __result.tm_mon = static_cast<unsigned>(__value) - 1;
145   else if constexpr (same_as<_ChronoT, chrono::year>)
146     __result.tm_year = static_cast<int>(__value) - 1900;
147   else if constexpr (same_as<_ChronoT, chrono::weekday>)
148     __result.tm_wday = __value.c_encoding();
149   else if constexpr (same_as<_ChronoT, chrono::weekday_indexed> || same_as<_ChronoT, chrono::weekday_last>)
150     __result.tm_wday = __value.weekday().c_encoding();
151   else if constexpr (same_as<_ChronoT, chrono::month_day>) {
152     __result.tm_mday = static_cast<unsigned>(__value.day());
153     __result.tm_mon  = static_cast<unsigned>(__value.month()) - 1;
154   } else if constexpr (same_as<_ChronoT, chrono::month_day_last>) {
155     __result.tm_mon = static_cast<unsigned>(__value.month()) - 1;
156   } else if constexpr (same_as<_ChronoT, chrono::month_weekday> || same_as<_ChronoT, chrono::month_weekday_last>) {
157     __result.tm_wday = __value.weekday_indexed().weekday().c_encoding();
158     __result.tm_mon  = static_cast<unsigned>(__value.month()) - 1;
159   } else if constexpr (same_as<_ChronoT, chrono::year_month>) {
160     __result.tm_year = static_cast<int>(__value.year()) - 1900;
161     __result.tm_mon  = static_cast<unsigned>(__value.month()) - 1;
162   } else if constexpr (same_as<_ChronoT, chrono::year_month_day> || same_as<_ChronoT, chrono::year_month_day_last>) {
163     return std::__convert_to_tm<_Tm>(
164         chrono::year_month_day{__value}, chrono::weekday{static_cast<chrono::sys_days>(__value)});
165   } else if constexpr (same_as<_ChronoT, chrono::year_month_weekday> ||
166                        same_as<_ChronoT, chrono::year_month_weekday_last>) {
167     return std::__convert_to_tm<_Tm>(chrono::year_month_day{static_cast<chrono::sys_days>(__value)}, __value.weekday());
168   } else if constexpr (__is_hh_mm_ss<_ChronoT>) {
169     __result.tm_sec = __value.seconds().count();
170     __result.tm_min = __value.minutes().count();
171     // In libc++ hours is stored as a long. The type in std::tm is an int. So
172     // the overflow can only occur when hour uses more bits than an int
173     // provides.
174     if constexpr (sizeof(std::chrono::hours::rep) > sizeof(__result.tm_hour))
175       if (__value.hours().count() > std::numeric_limits<decltype(__result.tm_hour)>::max())
176         std::__throw_format_error("Formatting hh_mm_ss, encountered an hour overflow");
177     __result.tm_hour = __value.hours().count();
178 #  if !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_TZDB)
179   } else if constexpr (same_as<_ChronoT, chrono::sys_info>) {
180     // Has no time information.
181   } else if constexpr (same_as<_ChronoT, chrono::local_info>) {
182     // Has no time information.
183 #    if !defined(_LIBCPP_HAS_NO_TIME_ZONE_DATABASE) && !defined(_LIBCPP_HAS_NO_FILESYSTEM) &&                          \
184         !defined(_LIBCPP_HAS_NO_LOCALIZATION)
185   } else if constexpr (__is_specialization_v<_ChronoT, chrono::zoned_time>) {
186     return std::__convert_to_tm<_Tm>(
187         chrono::sys_time<typename _ChronoT::duration>{__value.get_local_time().time_since_epoch()});
188 #    endif
189 #  endif // !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_TZDB)
190   } else
191     static_assert(sizeof(_ChronoT) == 0, "Add the missing type specialization");
192 
193   return __result;
194 }
195 
196 #endif // if _LIBCPP_STD_VER >= 20
197 
198 _LIBCPP_END_NAMESPACE_STD
199 
200 _LIBCPP_POP_MACROS
201 
202 #endif // _LIBCPP___CHRONO_CONVERT_TO_TM_H
203