Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:14

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___CHRONO_CONVERT_TO_TM_H
0011 #define _LIBCPP___CHRONO_CONVERT_TO_TM_H
0012 
0013 #include <__chrono/calendar.h>
0014 #include <__chrono/concepts.h>
0015 #include <__chrono/day.h>
0016 #include <__chrono/duration.h>
0017 #include <__chrono/file_clock.h>
0018 #include <__chrono/hh_mm_ss.h>
0019 #include <__chrono/local_info.h>
0020 #include <__chrono/month.h>
0021 #include <__chrono/month_weekday.h>
0022 #include <__chrono/monthday.h>
0023 #include <__chrono/statically_widen.h>
0024 #include <__chrono/sys_info.h>
0025 #include <__chrono/system_clock.h>
0026 #include <__chrono/time_point.h>
0027 #include <__chrono/utc_clock.h>
0028 #include <__chrono/weekday.h>
0029 #include <__chrono/year.h>
0030 #include <__chrono/year_month.h>
0031 #include <__chrono/year_month_day.h>
0032 #include <__chrono/year_month_weekday.h>
0033 #include <__chrono/zoned_time.h>
0034 #include <__concepts/same_as.h>
0035 #include <__config>
0036 #include <__format/format_error.h>
0037 #include <__memory/addressof.h>
0038 #include <__type_traits/is_convertible.h>
0039 #include <__type_traits/is_specialization.h>
0040 #include <cstdint>
0041 #include <ctime>
0042 #include <limits>
0043 
0044 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0045 #  pragma GCC system_header
0046 #endif
0047 
0048 _LIBCPP_PUSH_MACROS
0049 #include <__undef_macros>
0050 
0051 _LIBCPP_BEGIN_NAMESPACE_STD
0052 
0053 #if _LIBCPP_STD_VER >= 20
0054 
0055 // Conerts a chrono date and weekday to a given _Tm type.
0056 //
0057 // This is an implementation detail for the function
0058 //   template <class _Tm, class _ChronoT>
0059 //   _Tm __convert_to_tm(const _ChronoT& __value)
0060 //
0061 // This manually converts the two values to the proper type. It is possible to
0062 // convert from sys_days to time_t and then to _Tm. But this leads to the Y2K
0063 // bug when time_t is a 32-bit signed integer. Chrono considers years beyond
0064 // the year 2038 valid, so instead do the transformation manually.
0065 template <class _Tm, class _Date>
0066   requires(same_as<_Date, chrono::year_month_day> || same_as<_Date, chrono::year_month_day_last>)
0067 _LIBCPP_HIDE_FROM_ABI _Tm __convert_to_tm(const _Date& __date, chrono::weekday __weekday) {
0068   _Tm __result = {};
0069 #  ifdef __GLIBC__
0070   __result.tm_zone = "UTC";
0071 #  endif
0072   __result.tm_year = static_cast<int>(__date.year()) - 1900;
0073   __result.tm_mon  = static_cast<unsigned>(__date.month()) - 1;
0074   __result.tm_mday = static_cast<unsigned>(__date.day());
0075   __result.tm_wday = static_cast<unsigned>(__weekday.c_encoding());
0076   __result.tm_yday =
0077       (static_cast<chrono::sys_days>(__date) -
0078        static_cast<chrono::sys_days>(chrono::year_month_day{__date.year(), chrono::January, chrono::day{1}}))
0079           .count();
0080 
0081   return __result;
0082 }
0083 
0084 template <class _Tm, class _Duration>
0085 _LIBCPP_HIDE_FROM_ABI _Tm __convert_to_tm(const chrono::sys_time<_Duration> __tp) {
0086   chrono::sys_days __days = chrono::floor<chrono::days>(__tp);
0087   chrono::year_month_day __ymd{__days};
0088 
0089   _Tm __result = std::__convert_to_tm<_Tm>(chrono::year_month_day{__ymd}, chrono::weekday{__days});
0090 
0091   uint64_t __sec =
0092       chrono::duration_cast<chrono::seconds>(__tp - chrono::time_point_cast<chrono::seconds>(__days)).count();
0093   __sec %= 24 * 3600;
0094   __result.tm_hour = __sec / 3600;
0095   __sec %= 3600;
0096   __result.tm_min = __sec / 60;
0097   __result.tm_sec = __sec % 60;
0098 
0099   return __result;
0100 }
0101 
0102 #  if _LIBCPP_HAS_TIME_ZONE_DATABASE && _LIBCPP_HAS_FILESYSTEM && _LIBCPP_HAS_LOCALIZATION
0103 #    if _LIBCPP_HAS_EXPERIMENTAL_TZDB
0104 
0105 template <class _Tm, class _Duration>
0106 _LIBCPP_HIDE_FROM_ABI _Tm __convert_to_tm(chrono::utc_time<_Duration> __tp) {
0107   _Tm __result = std::__convert_to_tm<_Tm>(chrono::utc_clock::to_sys(__tp));
0108 
0109   if (chrono::get_leap_second_info(__tp).is_leap_second)
0110     ++__result.tm_sec;
0111 
0112   return __result;
0113 }
0114 
0115 #    endif // _LIBCPP_HAS_EXPERIMENTAL_TZDB
0116 #  endif   // _LIBCPP_HAS_TIME_ZONE_DATABASE && _LIBCPP_HAS_FILESYSTEM && _LIBCPP_HAS_LOCALIZATION
0117 
0118 // Convert a chrono (calendar) time point, or dururation to the given _Tm type,
0119 // which must have the same properties as std::tm.
0120 template <class _Tm, class _ChronoT>
0121 _LIBCPP_HIDE_FROM_ABI _Tm __convert_to_tm(const _ChronoT& __value) {
0122   _Tm __result = {};
0123 #  ifdef __GLIBC__
0124   __result.tm_zone = "UTC";
0125 #  endif
0126 
0127   if constexpr (__is_time_point<_ChronoT>) {
0128     if constexpr (same_as<typename _ChronoT::clock, chrono::system_clock>)
0129       return std::__convert_to_tm<_Tm>(__value);
0130 #  if _LIBCPP_HAS_TIME_ZONE_DATABASE && _LIBCPP_HAS_FILESYSTEM && _LIBCPP_HAS_LOCALIZATION
0131 #    if _LIBCPP_HAS_EXPERIMENTAL_TZDB
0132     else if constexpr (same_as<typename _ChronoT::clock, chrono::utc_clock>)
0133       return std::__convert_to_tm<_Tm>(__value);
0134 #    endif // _LIBCPP_HAS_EXPERIMENTAL_TZDB
0135 #  endif   // _LIBCPP_HAS_TIME_ZONE_DATABASE && _LIBCPP_HAS_FILESYSTEM && _LIBCPP_HAS_LOCALIZATION
0136     else if constexpr (same_as<typename _ChronoT::clock, chrono::file_clock>)
0137       return std::__convert_to_tm<_Tm>(_ChronoT::clock::to_sys(__value));
0138     else if constexpr (same_as<typename _ChronoT::clock, chrono::local_t>)
0139       return std::__convert_to_tm<_Tm>(chrono::sys_time<typename _ChronoT::duration>{__value.time_since_epoch()});
0140     else
0141       static_assert(sizeof(_ChronoT) == 0, "TODO: Add the missing clock specialization");
0142   } else if constexpr (chrono::__is_duration_v<_ChronoT>) {
0143     // [time.format]/6
0144     //   ...  However, if a flag refers to a "time of day" (e.g. %H, %I, %p,
0145     //   etc.), then a specialization of duration is interpreted as the time of
0146     //   day elapsed since midnight.
0147 
0148     // Not all values can be converted to hours, it may run into ratio
0149     // conversion errors. In that case the conversion to seconds works.
0150     if constexpr (is_convertible_v<_ChronoT, chrono::hours>) {
0151       auto __hour      = chrono::floor<chrono::hours>(__value);
0152       auto __sec       = chrono::duration_cast<chrono::seconds>(__value - __hour);
0153       __result.tm_hour = __hour.count() % 24;
0154       __result.tm_min  = __sec.count() / 60;
0155       __result.tm_sec  = __sec.count() % 60;
0156     } else {
0157       uint64_t __sec = chrono::duration_cast<chrono::seconds>(__value).count();
0158       __sec %= 24 * 3600;
0159       __result.tm_hour = __sec / 3600;
0160       __sec %= 3600;
0161       __result.tm_min = __sec / 60;
0162       __result.tm_sec = __sec % 60;
0163     }
0164   } else if constexpr (same_as<_ChronoT, chrono::day>)
0165     __result.tm_mday = static_cast<unsigned>(__value);
0166   else if constexpr (same_as<_ChronoT, chrono::month>)
0167     __result.tm_mon = static_cast<unsigned>(__value) - 1;
0168   else if constexpr (same_as<_ChronoT, chrono::year>)
0169     __result.tm_year = static_cast<int>(__value) - 1900;
0170   else if constexpr (same_as<_ChronoT, chrono::weekday>)
0171     __result.tm_wday = __value.c_encoding();
0172   else if constexpr (same_as<_ChronoT, chrono::weekday_indexed> || same_as<_ChronoT, chrono::weekday_last>)
0173     __result.tm_wday = __value.weekday().c_encoding();
0174   else if constexpr (same_as<_ChronoT, chrono::month_day>) {
0175     __result.tm_mday = static_cast<unsigned>(__value.day());
0176     __result.tm_mon  = static_cast<unsigned>(__value.month()) - 1;
0177   } else if constexpr (same_as<_ChronoT, chrono::month_day_last>) {
0178     __result.tm_mon = static_cast<unsigned>(__value.month()) - 1;
0179   } else if constexpr (same_as<_ChronoT, chrono::month_weekday> || same_as<_ChronoT, chrono::month_weekday_last>) {
0180     __result.tm_wday = __value.weekday_indexed().weekday().c_encoding();
0181     __result.tm_mon  = static_cast<unsigned>(__value.month()) - 1;
0182   } else if constexpr (same_as<_ChronoT, chrono::year_month>) {
0183     __result.tm_year = static_cast<int>(__value.year()) - 1900;
0184     __result.tm_mon  = static_cast<unsigned>(__value.month()) - 1;
0185   } else if constexpr (same_as<_ChronoT, chrono::year_month_day> || same_as<_ChronoT, chrono::year_month_day_last>) {
0186     return std::__convert_to_tm<_Tm>(
0187         chrono::year_month_day{__value}, chrono::weekday{static_cast<chrono::sys_days>(__value)});
0188   } else if constexpr (same_as<_ChronoT, chrono::year_month_weekday> ||
0189                        same_as<_ChronoT, chrono::year_month_weekday_last>) {
0190     return std::__convert_to_tm<_Tm>(chrono::year_month_day{static_cast<chrono::sys_days>(__value)}, __value.weekday());
0191   } else if constexpr (__is_hh_mm_ss<_ChronoT>) {
0192     __result.tm_sec = __value.seconds().count();
0193     __result.tm_min = __value.minutes().count();
0194     // In libc++ hours is stored as a long. The type in std::tm is an int. So
0195     // the overflow can only occur when hour uses more bits than an int
0196     // provides.
0197     if constexpr (sizeof(std::chrono::hours::rep) > sizeof(__result.tm_hour))
0198       if (__value.hours().count() > std::numeric_limits<decltype(__result.tm_hour)>::max())
0199         std::__throw_format_error("Formatting hh_mm_ss, encountered an hour overflow");
0200     __result.tm_hour = __value.hours().count();
0201 #  if _LIBCPP_HAS_EXPERIMENTAL_TZDB
0202   } else if constexpr (same_as<_ChronoT, chrono::sys_info>) {
0203     // Has no time information.
0204   } else if constexpr (same_as<_ChronoT, chrono::local_info>) {
0205     // Has no time information.
0206 #    if _LIBCPP_HAS_TIME_ZONE_DATABASE && _LIBCPP_HAS_FILESYSTEM && _LIBCPP_HAS_LOCALIZATION
0207   } else if constexpr (__is_specialization_v<_ChronoT, chrono::zoned_time>) {
0208     return std::__convert_to_tm<_Tm>(
0209         chrono::sys_time<typename _ChronoT::duration>{__value.get_local_time().time_since_epoch()});
0210 #    endif
0211 #  endif // _LIBCPP_HAS_EXPERIMENTAL_TZDB
0212   } else
0213     static_assert(sizeof(_ChronoT) == 0, "Add the missing type specialization");
0214 
0215   return __result;
0216 }
0217 
0218 #endif // if _LIBCPP_STD_VER >= 20
0219 
0220 _LIBCPP_END_NAMESPACE_STD
0221 
0222 _LIBCPP_POP_MACROS
0223 
0224 #endif // _LIBCPP___CHRONO_CONVERT_TO_TM_H