Back to home page

EIC code displayed by LXR

 
 

    


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

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