convert_to_tm.h 7.9 KB

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