Skip to content

Commit

Permalink
fix DST setting properly in UtcTime
Browse files Browse the repository at this point in the history
Fixes minio#132

Signed-off-by: Bala.FA <[email protected]>
  • Loading branch information
balamurugana committed Apr 11, 2024
1 parent f5132e0 commit 3792bae
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions include/miniocpp/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class UtcTime {

static std::tm auxLocaltime(const std::time_t& time);
std::tm getBrokenDownTime() const { return auxLocaltime(secs_); }
static std::time_t toUtcSeconds(const std::time_t time);

public:
UtcTime() = default;
Expand Down
23 changes: 18 additions & 5 deletions src/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -333,20 +333,29 @@ std::tm UtcTime::auxLocaltime(const std::time_t& time) {
return result;
}

std::time_t UtcTime::toUtcSeconds(const std::time_t secs_local) {
std::tm result{};
#ifdef _WIN32
gmtime_s(&result, &secs_local);
#else
gmtime_r(&secs_local, &result);
#endif
result.tm_isdst = -1;
return std::mktime(&result);
}

UtcTime UtcTime::Now() {
auto usec_now = std::chrono::system_clock::now().time_since_epoch() /
std::chrono::microseconds(1);
auto secs_local = static_cast<time_t>(usec_now / 1000000);
auto secs_utc = std::mktime(std::gmtime(&secs_local));
return UtcTime(secs_utc, static_cast<long>(usec_now % 1000000));
return UtcTime(toUtcSeconds(static_cast<time_t>(usec_now / 1000000)),
static_cast<long>(usec_now % 1000000));
}

void UtcTime::ToLocalTime(std::tm& time) {
auto usec_now = std::chrono::system_clock::now().time_since_epoch() /
std::chrono::microseconds(1);
auto secs_local = static_cast<time_t>(usec_now / 1000000);
auto secs_utc = std::mktime(std::gmtime(&secs_local));
auto secs = secs_ + (secs_local - secs_utc);
auto secs = secs_ + (secs_local - toUtcSeconds(secs_local));
time = auxLocaltime(secs);
}

Expand Down Expand Up @@ -416,7 +425,11 @@ UtcTime UtcTime::FromISO8601UTC(const char* value) {
std::time_t secs = std::mktime(&t);

unsigned long ul = 0;
#ifdef _WIN32
static_cast<void>(sscanf_s(rv, ".%lu", &ul));
#else
static_cast<void>(sscanf(rv, ".%lu", &ul));
#endif
long usecs = (long)ul;

return UtcTime(secs, usecs);
Expand Down

0 comments on commit 3792bae

Please sign in to comment.