dt_to_string.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // Copyright (c) 2019-2024 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BOOST_MYSQL_IMPL_INTERNAL_DT_TO_STRING_HPP
  8. #define BOOST_MYSQL_IMPL_INTERNAL_DT_TO_STRING_HPP
  9. #include <boost/mysql/time.hpp>
  10. #include <boost/assert.hpp>
  11. #include <boost/charconv/to_chars.hpp>
  12. #include <boost/core/span.hpp>
  13. #include <cstddef>
  14. #include <cstdint>
  15. #include <cstdlib>
  16. #include <system_error>
  17. #include <type_traits>
  18. // gcc-11+ issues spurious warnings about to_chars
  19. #if BOOST_GCC >= 70000
  20. #pragma GCC diagnostic push
  21. #pragma GCC diagnostic ignored "-Wstringop-overflow"
  22. #endif
  23. namespace boost {
  24. namespace mysql {
  25. namespace detail {
  26. // Helpers
  27. template <class IntType>
  28. inline char* call_to_chars(char* begin, char* end, IntType value) noexcept
  29. {
  30. auto r = charconv::to_chars(begin, end, value);
  31. BOOST_ASSERT(r.ec == std::errc());
  32. return r.ptr;
  33. }
  34. template <class UnsignedInt>
  35. inline char* write_pad2(char* begin, char* end, UnsignedInt value) noexcept
  36. {
  37. static_assert(std::is_unsigned<UnsignedInt>::value, "");
  38. if (value < 10u)
  39. *begin++ = '0';
  40. return call_to_chars(begin, end, value);
  41. }
  42. inline char* write_pad4(char* begin, char* end, unsigned long value) noexcept
  43. {
  44. for (auto l : {1000u, 100u, 10u})
  45. {
  46. if (value < l)
  47. *begin++ = '0';
  48. }
  49. return call_to_chars(begin, end, value);
  50. }
  51. inline char* write_pad6(char* begin, char* end, unsigned long value) noexcept
  52. {
  53. for (auto l : {100000u, 10000u, 1000u, 100u, 10u})
  54. {
  55. if (value < l)
  56. *begin++ = '0';
  57. }
  58. return call_to_chars(begin, end, value);
  59. }
  60. inline std::size_t date_to_string(
  61. std::uint16_t year,
  62. std::uint8_t month,
  63. std::uint8_t day,
  64. span<char, 32> output
  65. ) noexcept
  66. {
  67. // Worst-case output is 14 chars, extra space just in case
  68. // Iterators
  69. char* it = output.data();
  70. char* end = it + output.size();
  71. // Year
  72. it = write_pad4(it, end, year);
  73. // Month
  74. *it++ = '-';
  75. it = write_pad2(it, end, static_cast<unsigned long>(month));
  76. // Day
  77. *it++ = '-';
  78. it = write_pad2(it, end, static_cast<unsigned long>(day));
  79. // Done
  80. return it - output.data();
  81. }
  82. inline std::size_t datetime_to_string(
  83. std::uint16_t year,
  84. std::uint8_t month,
  85. std::uint8_t day,
  86. std::uint8_t hour,
  87. std::uint8_t minute,
  88. std::uint8_t second,
  89. std::uint32_t microsecond,
  90. span<char, 64> output
  91. ) noexcept
  92. {
  93. // Worst-case output is 37 chars, extra space just in case
  94. // Iterators
  95. char* it = output.data();
  96. char* end = it + output.size();
  97. // Date
  98. it += date_to_string(year, month, day, span<char, 32>(it, 32));
  99. // Hour
  100. *it++ = ' ';
  101. it = write_pad2(it, end, static_cast<unsigned long>(hour));
  102. // Minutes
  103. *it++ = ':';
  104. it = write_pad2(it, end, static_cast<unsigned long>(minute));
  105. // Seconds
  106. *it++ = ':';
  107. it = write_pad2(it, end, static_cast<unsigned long>(second));
  108. // Microseconds
  109. *it++ = '.';
  110. it = write_pad6(it, end, microsecond);
  111. // Done
  112. return it - output.data();
  113. }
  114. inline std::size_t time_to_string(::boost::mysql::time value, span<char, 64> output) noexcept
  115. {
  116. // Worst-case output is 34 chars, extra space just in case
  117. // Values. Note that std::abs(time::min()) invokes UB because of
  118. // signed integer overflow
  119. constexpr auto min_val = (::boost::mysql::time::min)();
  120. using unsigned_t = typename std::make_unsigned<typename ::boost::mysql::time::rep>::type;
  121. auto total_count = value == min_val ? static_cast<unsigned_t>(min_val.count())
  122. : static_cast<unsigned_t>(std::abs(value.count()));
  123. auto num_micros = total_count % 1000000u;
  124. total_count /= 1000000u;
  125. auto num_secs = total_count % 60u;
  126. total_count /= 60u;
  127. auto num_mins = total_count % 60u;
  128. total_count /= 60u;
  129. auto num_hours = total_count;
  130. // Iterators
  131. char* it = output.data();
  132. char* end = it + output.size();
  133. // Sign
  134. if (value.count() < 0)
  135. *it++ = '-';
  136. // Hours
  137. it = write_pad2(it, end, num_hours); // type is unspecified
  138. // Minutes
  139. *it++ = ':';
  140. it = write_pad2(it, end, static_cast<unsigned long>(num_mins));
  141. // Seconds
  142. *it++ = ':';
  143. it = write_pad2(it, end, static_cast<unsigned long>(num_secs));
  144. // Microseconds
  145. *it++ = '.';
  146. it = write_pad6(it, end, static_cast<unsigned long>(num_micros));
  147. // Done
  148. return it - output.data();
  149. }
  150. } // namespace detail
  151. } // namespace mysql
  152. } // namespace boost
  153. #if BOOST_GCC >= 110000
  154. #pragma GCC diagnostic pop
  155. #endif
  156. #endif