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