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