From 129ce4bb154b8350ed32e1557dcf18dc7bcf11ba Mon Sep 17 00:00:00 2001 From: Steph Prince <40640337+stephprince@users.noreply.github.com> Date: Wed, 24 Apr 2024 15:24:49 -0700 Subject: [PATCH] update datetime to include tz offset --- src/Utils.hpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Utils.hpp b/src/Utils.hpp index e32531e0..0362190f 100644 --- a/src/Utils.hpp +++ b/src/Utils.hpp @@ -30,20 +30,25 @@ inline std::string getCurrentTime() // Get current time auto currentTime = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); - - // Convert to tm struct to extract date and time components std::tm utcTime = *std::gmtime(¤tTime); - // Format the date and time in ISO 8601 format with the UTC offset - std::ostringstream oss; - oss << std::put_time(&utcTime, "%FT%T"); - // Get the timezone offset auto localTime = std::localtime(¤tTime); auto utcOffset = localTime->tm_hour - utcTime.tm_hour; auto utcOffsetMinutes = (utcOffset < 0 ? -1 : 1) * (localTime->tm_min - utcTime.tm_min); + // Adjust the UTC time to the local time + utcTime.tm_hour += utcOffset; + if (utcTime.tm_hour < 0) { + utcTime.tm_hour += 24; + utcTime.tm_mday -= 1; + } + + // Format the date and time in ISO 8601 format with the UTC offset + std::ostringstream oss; + oss << std::put_time(&utcTime, "%FT%T"); + // Add the timezone offset to the date and time string oss << (utcOffset < 0 ? "-" : "+") << std::setw(2) << std::setfill('0') << std::abs(utcOffset) << ":" << std::setw(2) << std::setfill('0')