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/day.h> 14 #include <__chrono/duration.h> 15 #include <__chrono/hh_mm_ss.h> 16 #include <__chrono/month.h> 17 #include <__chrono/month_weekday.h> 18 #include <__chrono/monthday.h> 19 #include <__chrono/statically_widen.h> 20 #include <__chrono/system_clock.h> 21 #include <__chrono/time_point.h> 22 #include <__chrono/weekday.h> 23 #include <__chrono/year.h> 24 #include <__chrono/year_month.h> 25 #include <__chrono/year_month_day.h> 26 #include <__chrono/year_month_weekday.h> 27 #include <__concepts/same_as.h> 28 #include <__config> 29 #include <__memory/addressof.h> 30 #include <cstdint> 31 #include <ctime> 32 33 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 34 # pragma GCC system_header 35 #endif 36 37 _LIBCPP_BEGIN_NAMESPACE_STD 38 39 #if _LIBCPP_STD_VER > 17 40 41 // Conerts a chrono date and weekday to a given _Tm type. 42 // 43 // This is an implementation detail for the function 44 // template <class _Tm, class _ChronoT> 45 // _Tm __convert_to_tm(const _ChronoT& __value) 46 // 47 // This manually converts the two values to the proper type. It is possible to 48 // convert from sys_days to time_t and then to _Tm. But this leads to the Y2K 49 // bug when time_t is a 32-bit signed integer. Chrono considers years beyond 50 // the year 2038 valid, so instead do the transformation manually. 51 template <class _Tm, class _Date> 52 requires(same_as<_Date, chrono::year_month_day> || same_as<_Date, chrono::year_month_day_last>) 53 _LIBCPP_HIDE_FROM_ABI _Tm __convert_to_tm(const _Date& __date, chrono::weekday __weekday) { 54 _Tm __result = {}; 55 # ifdef __GLIBC__ 56 __result.tm_zone = "UTC"; 57 # endif 58 __result.tm_year = static_cast<int>(__date.year()) - 1900; 59 __result.tm_mon = static_cast<unsigned>(__date.month()) - 1; 60 __result.tm_mday = static_cast<unsigned>(__date.day()); 61 __result.tm_wday = static_cast<unsigned>(__weekday.c_encoding()); 62 __result.tm_yday = 63 (static_cast<chrono::sys_days>(__date) - 64 static_cast<chrono::sys_days>(chrono::year_month_day{__date.year(), chrono::January, chrono::day{1}})) 65 .count(); 66 67 return __result; 68 } 69 70 // Convert a chrono (calendar) time point, or dururation to the given _Tm type, 71 // which must have the same properties as std::tm. 72 template <class _Tm, class _ChronoT> 73 _LIBCPP_HIDE_FROM_ABI _Tm __convert_to_tm(const _ChronoT& __value) { 74 _Tm __result = {}; 75 # ifdef __GLIBC__ 76 __result.tm_zone = "UTC"; 77 # endif 78 79 if constexpr (chrono::__is_duration<_ChronoT>::value) { 80 // [time.format]/6 81 // ... However, if a flag refers to a "time of day" (e.g. %H, %I, %p, 82 // etc.), then a specialization of duration is interpreted as the time of 83 // day elapsed since midnight. 84 uint64_t __sec = chrono::duration_cast<chrono::seconds>(__value).count(); 85 __sec %= 24 * 3600; 86 __result.tm_hour = __sec / 3600; 87 __sec %= 3600; 88 __result.tm_min = __sec / 60; 89 __result.tm_sec = __sec % 60; 90 } else if constexpr (same_as<_ChronoT, chrono::day>) 91 __result.tm_mday = static_cast<unsigned>(__value); 92 else if constexpr (same_as<_ChronoT, chrono::month>) 93 __result.tm_mon = static_cast<unsigned>(__value) - 1; 94 else if constexpr (same_as<_ChronoT, chrono::year>) 95 __result.tm_year = static_cast<int>(__value) - 1900; 96 else if constexpr (same_as<_ChronoT, chrono::weekday>) 97 __result.tm_wday = __value.c_encoding(); 98 else if constexpr (same_as<_ChronoT, chrono::weekday_indexed> || same_as<_ChronoT, chrono::weekday_last>) 99 __result.tm_wday = __value.weekday().c_encoding(); 100 else if constexpr (same_as<_ChronoT, chrono::month_day>) { 101 __result.tm_mday = static_cast<unsigned>(__value.day()); 102 __result.tm_mon = static_cast<unsigned>(__value.month()) - 1; 103 } else if constexpr (same_as<_ChronoT, chrono::month_day_last>) { 104 __result.tm_mon = static_cast<unsigned>(__value.month()) - 1; 105 } else if constexpr (same_as<_ChronoT, chrono::month_weekday> || same_as<_ChronoT, chrono::month_weekday_last>) { 106 __result.tm_wday = __value.weekday_indexed().weekday().c_encoding(); 107 __result.tm_mon = static_cast<unsigned>(__value.month()) - 1; 108 } else if constexpr (same_as<_ChronoT, chrono::year_month>) { 109 __result.tm_year = static_cast<int>(__value.year()) - 1900; 110 __result.tm_mon = static_cast<unsigned>(__value.month()) - 1; 111 } else if constexpr (same_as<_ChronoT, chrono::year_month_day> || same_as<_ChronoT, chrono::year_month_day_last>) { 112 return std::__convert_to_tm<_Tm>( 113 chrono::year_month_day{__value}, chrono::weekday{static_cast<chrono::sys_days>(__value)}); 114 } else if constexpr (same_as<_ChronoT, chrono::year_month_weekday> || 115 same_as<_ChronoT, chrono::year_month_weekday_last>) { 116 return std::__convert_to_tm<_Tm>(chrono::year_month_day{static_cast<chrono::sys_days>(__value)}, __value.weekday()); 117 } else 118 static_assert(sizeof(_ChronoT) == 0, "Add the missing type specialization"); 119 120 return __result; 121 } 122 123 #endif //if _LIBCPP_STD_VER > 17 124 125 _LIBCPP_END_NAMESPACE_STD 126 127 #endif // _LIBCPP___CHRONO_CONVERT_TO_TM_H 128