123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- #ifndef BOOST_MYSQL_IMPL_INTERNAL_DT_TO_STRING_HPP
- #define BOOST_MYSQL_IMPL_INTERNAL_DT_TO_STRING_HPP
- #include <boost/mysql/time.hpp>
- #include <boost/assert.hpp>
- #include <boost/charconv/to_chars.hpp>
- #include <boost/core/span.hpp>
- #include <cstddef>
- #include <cstdint>
- #include <cstdlib>
- #include <system_error>
- #include <type_traits>
- #if BOOST_GCC >= 70000
- #pragma GCC diagnostic push
- #pragma GCC diagnostic ignored "-Wstringop-overflow"
- #endif
- namespace boost {
- namespace mysql {
- namespace detail {
- template <class IntType>
- inline char* call_to_chars(char* begin, char* end, IntType value) noexcept
- {
- auto r = charconv::to_chars(begin, end, value);
- BOOST_ASSERT(r.ec == std::errc());
- return r.ptr;
- }
- template <class UnsignedInt>
- inline char* write_pad2(char* begin, char* end, UnsignedInt value) noexcept
- {
- static_assert(std::is_unsigned<UnsignedInt>::value, "");
- if (value < 10u)
- *begin++ = '0';
- return call_to_chars(begin, end, value);
- }
- inline char* write_pad4(char* begin, char* end, unsigned long value) noexcept
- {
- for (auto l : {1000u, 100u, 10u})
- {
- if (value < l)
- *begin++ = '0';
- }
- return call_to_chars(begin, end, value);
- }
- inline char* write_pad6(char* begin, char* end, unsigned long value) noexcept
- {
- for (auto l : {100000u, 10000u, 1000u, 100u, 10u})
- {
- if (value < l)
- *begin++ = '0';
- }
- return call_to_chars(begin, end, value);
- }
- inline std::size_t date_to_string(
- std::uint16_t year,
- std::uint8_t month,
- std::uint8_t day,
- span<char, 32> output
- ) noexcept
- {
-
-
- char* it = output.data();
- char* end = it + output.size();
-
- it = write_pad4(it, end, year);
-
- *it++ = '-';
- it = write_pad2(it, end, static_cast<unsigned long>(month));
-
- *it++ = '-';
- it = write_pad2(it, end, static_cast<unsigned long>(day));
-
- return it - output.data();
- }
- inline std::size_t datetime_to_string(
- std::uint16_t year,
- std::uint8_t month,
- std::uint8_t day,
- std::uint8_t hour,
- std::uint8_t minute,
- std::uint8_t second,
- std::uint32_t microsecond,
- span<char, 64> output
- ) noexcept
- {
-
-
- char* it = output.data();
- char* end = it + output.size();
-
- it += date_to_string(year, month, day, span<char, 32>(it, 32));
-
- *it++ = ' ';
- it = write_pad2(it, end, static_cast<unsigned long>(hour));
-
- *it++ = ':';
- it = write_pad2(it, end, static_cast<unsigned long>(minute));
-
- *it++ = ':';
- it = write_pad2(it, end, static_cast<unsigned long>(second));
-
- *it++ = '.';
- it = write_pad6(it, end, microsecond);
-
- return it - output.data();
- }
- inline std::size_t time_to_string(::boost::mysql::time value, span<char, 64> output) noexcept
- {
-
-
-
- constexpr auto min_val = (::boost::mysql::time::min)();
- using unsigned_t = typename std::make_unsigned<typename ::boost::mysql::time::rep>::type;
- auto total_count = value == min_val ? static_cast<unsigned_t>(min_val.count())
- : static_cast<unsigned_t>(std::abs(value.count()));
- auto num_micros = total_count % 1000000u;
- total_count /= 1000000u;
- auto num_secs = total_count % 60u;
- total_count /= 60u;
- auto num_mins = total_count % 60u;
- total_count /= 60u;
- auto num_hours = total_count;
-
- char* it = output.data();
- char* end = it + output.size();
-
- if (value.count() < 0)
- *it++ = '-';
-
- it = write_pad2(it, end, num_hours);
-
- *it++ = ':';
- it = write_pad2(it, end, static_cast<unsigned long>(num_mins));
-
- *it++ = ':';
- it = write_pad2(it, end, static_cast<unsigned long>(num_secs));
-
- *it++ = '.';
- it = write_pad6(it, end, static_cast<unsigned long>(num_micros));
-
- return it - output.data();
- }
- }
- }
- }
- #if BOOST_GCC >= 110000
- #pragma GCC diagnostic pop
- #endif
- #endif
|