convert_to_tm.h 5.3 KB

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