Skip to content

Commit

Permalink
update datetime to include tz offset
Browse files Browse the repository at this point in the history
  • Loading branch information
stephprince committed Apr 24, 2024
1 parent 98b1aff commit 129ce4b
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(&currentTime);

// 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(&currentTime);
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')
Expand Down

0 comments on commit 129ce4b

Please sign in to comment.