Skip to content

Commit 046a41e

Browse files
authored
fix DST setting properly in UtcTime (#133)
Fixes #132 Signed-off-by: Bala.FA <bala@minio.io>
1 parent dc82f4d commit 046a41e

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

include/miniocpp/utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ class UtcTime {
120120

121121
static std::tm auxLocaltime(const std::time_t& time);
122122
std::tm getBrokenDownTime() const { return auxLocaltime(secs_); }
123+
static std::time_t toUtcSeconds(const std::time_t time);
123124

124125
public:
125126
UtcTime() = default;

src/utils.cc

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,20 +333,29 @@ std::tm UtcTime::auxLocaltime(const std::time_t& time) {
333333
return result;
334334
}
335335

336+
std::time_t UtcTime::toUtcSeconds(const std::time_t secs_local) {
337+
std::tm result{};
338+
#ifdef _WIN32
339+
gmtime_s(&result, &secs_local);
340+
#else
341+
gmtime_r(&secs_local, &result);
342+
#endif
343+
result.tm_isdst = -1;
344+
return std::mktime(&result);
345+
}
346+
336347
UtcTime UtcTime::Now() {
337348
auto usec_now = std::chrono::system_clock::now().time_since_epoch() /
338349
std::chrono::microseconds(1);
339-
auto secs_local = static_cast<time_t>(usec_now / 1000000);
340-
auto secs_utc = std::mktime(std::gmtime(&secs_local));
341-
return UtcTime(secs_utc, static_cast<long>(usec_now % 1000000));
350+
return UtcTime(toUtcSeconds(static_cast<time_t>(usec_now / 1000000)),
351+
static_cast<long>(usec_now % 1000000));
342352
}
343353

344354
void UtcTime::ToLocalTime(std::tm& time) {
345355
auto usec_now = std::chrono::system_clock::now().time_since_epoch() /
346356
std::chrono::microseconds(1);
347357
auto secs_local = static_cast<time_t>(usec_now / 1000000);
348-
auto secs_utc = std::mktime(std::gmtime(&secs_local));
349-
auto secs = secs_ + (secs_local - secs_utc);
358+
auto secs = secs_ + (secs_local - toUtcSeconds(secs_local));
350359
time = auxLocaltime(secs);
351360
}
352361

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

418427
unsigned long ul = 0;
428+
#ifdef _WIN32
429+
static_cast<void>(sscanf_s(rv, ".%lu", &ul));
430+
#else
419431
static_cast<void>(sscanf(rv, ".%lu", &ul));
432+
#endif
420433
long usecs = (long)ul;
421434

422435
return UtcTime(secs, usecs);

0 commit comments

Comments
 (0)