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