mirror of
https://github.com/minio/minio-cpp.git
synced 2025-08-06 13:02:41 +03:00
fix DST setting properly in UtcTime (#133)
Fixes #132 Signed-off-by: Bala.FA <bala@minio.io>
This commit is contained in:
@@ -120,6 +120,7 @@ class UtcTime {
|
|||||||
|
|
||||||
static std::tm auxLocaltime(const std::time_t& time);
|
static std::tm auxLocaltime(const std::time_t& time);
|
||||||
std::tm getBrokenDownTime() const { return auxLocaltime(secs_); }
|
std::tm getBrokenDownTime() const { return auxLocaltime(secs_); }
|
||||||
|
static std::time_t toUtcSeconds(const std::time_t time);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
UtcTime() = default;
|
UtcTime() = default;
|
||||||
|
23
src/utils.cc
23
src/utils.cc
@@ -333,20 +333,29 @@ std::tm UtcTime::auxLocaltime(const std::time_t& time) {
|
|||||||
return result;
|
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() {
|
UtcTime UtcTime::Now() {
|
||||||
auto usec_now = std::chrono::system_clock::now().time_since_epoch() /
|
auto usec_now = std::chrono::system_clock::now().time_since_epoch() /
|
||||||
std::chrono::microseconds(1);
|
std::chrono::microseconds(1);
|
||||||
auto secs_local = static_cast<time_t>(usec_now / 1000000);
|
return UtcTime(toUtcSeconds(static_cast<time_t>(usec_now / 1000000)),
|
||||||
auto secs_utc = std::mktime(std::gmtime(&secs_local));
|
static_cast<long>(usec_now % 1000000));
|
||||||
return UtcTime(secs_utc, static_cast<long>(usec_now % 1000000));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void UtcTime::ToLocalTime(std::tm& time) {
|
void UtcTime::ToLocalTime(std::tm& time) {
|
||||||
auto usec_now = std::chrono::system_clock::now().time_since_epoch() /
|
auto usec_now = std::chrono::system_clock::now().time_since_epoch() /
|
||||||
std::chrono::microseconds(1);
|
std::chrono::microseconds(1);
|
||||||
auto secs_local = static_cast<time_t>(usec_now / 1000000);
|
auto secs_local = static_cast<time_t>(usec_now / 1000000);
|
||||||
auto secs_utc = std::mktime(std::gmtime(&secs_local));
|
auto secs = secs_ + (secs_local - toUtcSeconds(secs_local));
|
||||||
auto secs = secs_ + (secs_local - secs_utc);
|
|
||||||
time = auxLocaltime(secs);
|
time = auxLocaltime(secs);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -416,7 +425,11 @@ UtcTime UtcTime::FromISO8601UTC(const char* value) {
|
|||||||
std::time_t secs = std::mktime(&t);
|
std::time_t secs = std::mktime(&t);
|
||||||
|
|
||||||
unsigned long ul = 0;
|
unsigned long ul = 0;
|
||||||
|
#ifdef _WIN32
|
||||||
|
static_cast<void>(sscanf_s(rv, ".%lu", &ul));
|
||||||
|
#else
|
||||||
static_cast<void>(sscanf(rv, ".%lu", &ul));
|
static_cast<void>(sscanf(rv, ".%lu", &ul));
|
||||||
|
#endif
|
||||||
long usecs = (long)ul;
|
long usecs = (long)ul;
|
||||||
|
|
||||||
return UtcTime(secs, usecs);
|
return UtcTime(secs, usecs);
|
||||||
|
Reference in New Issue
Block a user