From fea1440854712435af2140a7776b968d3b820f18 Mon Sep 17 00:00:00 2001 From: Stephane Janel Date: Sat, 21 Dec 2024 19:09:23 +0100 Subject: [PATCH] Fix local time switch for StringToTime --- src/schema/include/timepoint-schema.hpp | 23 ++++++----------------- src/tech/src/timestring.cpp | 8 ++++++-- src/tech/test/timestring_test.cpp | 5 ++--- 3 files changed, 14 insertions(+), 22 deletions(-) diff --git a/src/schema/include/timepoint-schema.hpp b/src/schema/include/timepoint-schema.hpp index f7fcaf14..c0304954 100644 --- a/src/schema/include/timepoint-schema.hpp +++ b/src/schema/include/timepoint-schema.hpp @@ -15,6 +15,11 @@ namespace cct::schema { struct TimePoint { auto operator<=>(const TimePoint &) const noexcept = default; + // 2023 - 01 - 01 T 12 : 34 : 56 Z + static constexpr size_t strLen() { return 4U + 1U + 2U + 1U + 2U + 1U + 2U + 1U + 2U + 1U + 2U + 1U; } + + char *appendTo(char *buf) const { return std::ranges::copy(TimeToString(ts), buf).out; } + ::cct::TimePoint ts{}; }; @@ -50,23 +55,7 @@ template <> struct to { template static void op(auto &&value, Ctx &&, B &&b, IX &&ix) { - auto timeStr = ::cct::TimeToString(value.ts); - auto valueLen = timeStr.size(); - bool withQuotes = ::cct::details::JsonWithQuotes(b, ix); - int64_t additionalSize = (withQuotes ? 2L : 0L) + static_cast(ix) + static_cast(valueLen) - - static_cast(b.size()); - if (additionalSize > 0) { - b.append(additionalSize, ' '); - } - - if (withQuotes) { - b[ix++] = '"'; - } - std::ranges::copy(timeStr, b.data() + ix); - ix += valueLen; - if (withQuotes) { - b[ix++] = '"'; - } + ::cct::details::ToStrLikeJson(value, b, ix); } }; } // namespace glz::detail \ No newline at end of file diff --git a/src/tech/src/timestring.cpp b/src/tech/src/timestring.cpp index 22725a3a..d0cdfc94 100644 --- a/src/tech/src/timestring.cpp +++ b/src/tech/src/timestring.cpp @@ -51,8 +51,12 @@ TimePoint StringToTime(std::string_view timeStr, const char* format) { std::tm utc{}; std::istringstream ss{std::string(timeStr)}; ss >> std::get_time(&utc, format); - // TODO: fix issue of local time switch - return Clock::from_time_t(std::mktime(&utc)); + if (ss.fail()) { + throw exception("Failed to parse time string {}", timeStr); + } + // Convert timestamp to epoch time assuming UTC + std::time_t timet = timegm(&utc); + return Clock::from_time_t(timet); } Nonce Nonce_TimeSinceEpochInMs(Duration delay) { diff --git a/src/tech/test/timestring_test.cpp b/src/tech/test/timestring_test.cpp index fe3e9fcd..0d0376e8 100644 --- a/src/tech/test/timestring_test.cpp +++ b/src/tech/test/timestring_test.cpp @@ -58,8 +58,7 @@ TEST(TimeStringTest, TimeToString) { } TEST(TimeStringTest, FromToString) { - // TODO: below lines should be uncommented - // std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); - // EXPECT_STREQ(TimeToString(now).c_str(), TimeToString(StringToTime(TimeToString(now).c_str())).c_str()); + std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); + EXPECT_EQ(TimeToString(now), TimeToString(StringToTime(TimeToString(now).c_str()))); } } // namespace cct