| 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. |
| 68 | template <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 | |
| 87 | template <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 | |
| 108 | template <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 | |
| 118 | template <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 | |
| 128 | template <class _Tm, class _Duration> |
| 129 | _LIBCPP_HIDE_FROM_ABI _Tm __convert_to_tm(chrono::gps_time<_Duration> __tp) { |
| 130 | return std::__convert_to_tm<_Tm>(chrono::utc_clock::to_sys(chrono::gps_clock::to_utc(__tp))); |
| 131 | } |
| 132 | |
| 133 | # endif // _LIBCPP_HAS_EXPERIMENTAL_TZDB |
| 134 | # endif // _LIBCPP_HAS_TIME_ZONE_DATABASE && _LIBCPP_HAS_FILESYSTEM && _LIBCPP_HAS_LOCALIZATION |
| 135 | |
| 136 | // Convert a chrono (calendar) time point, or dururation to the given _Tm type, |
| 137 | // which must have the same properties as std::tm. |
| 138 | template <class _Tm, class _ChronoT> |
| 139 | _LIBCPP_HIDE_FROM_ABI _Tm __convert_to_tm(const _ChronoT& __value) { |
| 140 | _Tm __result = {}; |
| 141 | # ifdef __GLIBC__ |
| 142 | __result.tm_zone = "UTC" ; |
| 143 | # endif |
| 144 | |
| 145 | if constexpr (__is_time_point<_ChronoT>) { |
| 146 | if constexpr (same_as<typename _ChronoT::clock, chrono::file_clock>) |
| 147 | return std::__convert_to_tm<_Tm>(_ChronoT::clock::to_sys(__value)); |
| 148 | else if constexpr (same_as<typename _ChronoT::clock, chrono::local_t>) |
| 149 | return std::__convert_to_tm<_Tm>(chrono::sys_time<typename _ChronoT::duration>{__value.time_since_epoch()}); |
| 150 | else { |
| 151 | // Note that some clocks have specializations __convert_to_tm for their |
| 152 | // time_point. These don't need to be added here. They do not trigger |
| 153 | // this assert. |
| 154 | static_assert(sizeof(_ChronoT) == 0, "TODO: Add the missing clock specialization" ); |
| 155 | } |
| 156 | } else if constexpr (chrono::__is_duration_v<_ChronoT>) { |
| 157 | // [time.format]/6 |
| 158 | // ... However, if a flag refers to a "time of day" (e.g. %H, %I, %p, |
| 159 | // etc.), then a specialization of duration is interpreted as the time of |
| 160 | // day elapsed since midnight. |
| 161 | |
| 162 | // Not all values can be converted to hours, it may run into ratio |
| 163 | // conversion errors. In that case the conversion to seconds works. |
| 164 | if constexpr (is_convertible_v<_ChronoT, chrono::hours>) { |
| 165 | auto __hour = chrono::floor<chrono::hours>(__value); |
| 166 | auto __sec = chrono::duration_cast<chrono::seconds>(__value - __hour); |
| 167 | __result.tm_hour = __hour.count() % 24; |
| 168 | __result.tm_min = __sec.count() / 60; |
| 169 | __result.tm_sec = __sec.count() % 60; |
| 170 | } else { |
| 171 | uint64_t __sec = chrono::duration_cast<chrono::seconds>(__value).count(); |
| 172 | __sec %= 24 * 3600; |
| 173 | __result.tm_hour = __sec / 3600; |
| 174 | __sec %= 3600; |
| 175 | __result.tm_min = __sec / 60; |
| 176 | __result.tm_sec = __sec % 60; |
| 177 | } |
| 178 | } else if constexpr (same_as<_ChronoT, chrono::day>) |
| 179 | __result.tm_mday = static_cast<unsigned>(__value); |
| 180 | else if constexpr (same_as<_ChronoT, chrono::month>) |
| 181 | __result.tm_mon = static_cast<unsigned>(__value) - 1; |
| 182 | else if constexpr (same_as<_ChronoT, chrono::year>) |
| 183 | __result.tm_year = static_cast<int>(__value) - 1900; |
| 184 | else if constexpr (same_as<_ChronoT, chrono::weekday>) |
| 185 | __result.tm_wday = __value.c_encoding(); |
| 186 | else if constexpr (same_as<_ChronoT, chrono::weekday_indexed> || same_as<_ChronoT, chrono::weekday_last>) |
| 187 | __result.tm_wday = __value.weekday().c_encoding(); |
| 188 | else if constexpr (same_as<_ChronoT, chrono::month_day>) { |
| 189 | __result.tm_mday = static_cast<unsigned>(__value.day()); |
| 190 | __result.tm_mon = static_cast<unsigned>(__value.month()) - 1; |
| 191 | } else if constexpr (same_as<_ChronoT, chrono::month_day_last>) { |
| 192 | __result.tm_mon = static_cast<unsigned>(__value.month()) - 1; |
| 193 | } else if constexpr (same_as<_ChronoT, chrono::month_weekday> || same_as<_ChronoT, chrono::month_weekday_last>) { |
| 194 | __result.tm_wday = __value.weekday_indexed().weekday().c_encoding(); |
| 195 | __result.tm_mon = static_cast<unsigned>(__value.month()) - 1; |
| 196 | } else if constexpr (same_as<_ChronoT, chrono::year_month>) { |
| 197 | __result.tm_year = static_cast<int>(__value.year()) - 1900; |
| 198 | __result.tm_mon = static_cast<unsigned>(__value.month()) - 1; |
| 199 | } else if constexpr (same_as<_ChronoT, chrono::year_month_day> || same_as<_ChronoT, chrono::year_month_day_last>) { |
| 200 | return std::__convert_to_tm<_Tm>( |
| 201 | chrono::year_month_day{__value}, chrono::weekday{static_cast<chrono::sys_days>(__value)}); |
| 202 | } else if constexpr (same_as<_ChronoT, chrono::year_month_weekday> || |
| 203 | same_as<_ChronoT, chrono::year_month_weekday_last>) { |
| 204 | return std::__convert_to_tm<_Tm>(chrono::year_month_day{static_cast<chrono::sys_days>(__value)}, __value.weekday()); |
| 205 | } else if constexpr (__is_hh_mm_ss<_ChronoT>) { |
| 206 | __result.tm_sec = __value.seconds().count(); |
| 207 | __result.tm_min = __value.minutes().count(); |
| 208 | // In libc++ hours is stored as a long. The type in std::tm is an int. So |
| 209 | // the overflow can only occur when hour uses more bits than an int |
| 210 | // provides. |
| 211 | if constexpr (sizeof(std::chrono::hours::rep) > sizeof(__result.tm_hour)) |
| 212 | if (__value.hours().count() > std::numeric_limits<decltype(__result.tm_hour)>::max()) |
| 213 | std::__throw_format_error(s: "Formatting hh_mm_ss, encountered an hour overflow" ); |
| 214 | __result.tm_hour = __value.hours().count(); |
| 215 | # if _LIBCPP_HAS_EXPERIMENTAL_TZDB |
| 216 | } else if constexpr (same_as<_ChronoT, chrono::sys_info>) { |
| 217 | // Has no time information. |
| 218 | } else if constexpr (same_as<_ChronoT, chrono::local_info>) { |
| 219 | // Has no time information. |
| 220 | # if _LIBCPP_HAS_TIME_ZONE_DATABASE && _LIBCPP_HAS_FILESYSTEM && _LIBCPP_HAS_LOCALIZATION |
| 221 | } else if constexpr (__is_specialization_v<_ChronoT, chrono::zoned_time>) { |
| 222 | return std::__convert_to_tm<_Tm>( |
| 223 | chrono::sys_time<typename _ChronoT::duration>{__value.get_local_time().time_since_epoch()}); |
| 224 | # endif |
| 225 | # endif // _LIBCPP_HAS_EXPERIMENTAL_TZDB |
| 226 | } else |
| 227 | static_assert(sizeof(_ChronoT) == 0, "Add the missing type specialization" ); |
| 228 | |
| 229 | return __result; |
| 230 | } |
| 231 | |
| 232 | #endif // if _LIBCPP_STD_VER >= 20 |
| 233 | |
| 234 | _LIBCPP_END_NAMESPACE_STD |
| 235 | |
| 236 | _LIBCPP_POP_MACROS |
| 237 | |
| 238 | #endif // _LIBCPP___CHRONO_CONVERT_TO_TM_H |
| 239 | |