From cc336ecb5e36a7547cecab305692a93271e0e5b8 Mon Sep 17 00:00:00 2001 From: Andreas Hansson Date: Wed, 12 Nov 2014 09:05:20 -0500 Subject: [PATCH] base: Revert 9277177eccff and use getenv/setenv for UTC time This patch reverts changeset 9277177eccff which does not do what it was intended to do. In essence, we go back to implementing mkutctime much like the non-standard timegm extension. --- src/base/time.cc | 29 +++++++++++++++++++++++++++-- src/dev/sparc/dtod.cc | 3 +-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/base/time.cc b/src/base/time.cc index c265c1c47..a0ae9bb82 100644 --- a/src/base/time.cc +++ b/src/base/time.cc @@ -150,7 +150,32 @@ sleep(const Time &time) time_t mkutctime(struct tm *time) { - time_t local = mktime(time); - return mktime(gmtime(&local)); + // get the current timezone + char *tz = getenv("TZ"); + + // copy the string as the pointer gets invalidated when updating + // the environment + if (tz) { + tz = strdup(tz); + if (!tz) { + fatal("Failed to reserve memory for UTC time conversion\n"); + } + } + + // change to UTC and get the time + setenv("TZ", "", 1); + tzset(); + time_t ret = mktime(time); + + // restore the timezone again + if (tz) { + setenv("TZ", tz, 1); + free(tz); + } else { + unsetenv("TZ"); + } + tzset(); + + return ret; } diff --git a/src/dev/sparc/dtod.cc b/src/dev/sparc/dtod.cc index 0d57b1f83..94a32bed4 100644 --- a/src/dev/sparc/dtod.cc +++ b/src/dev/sparc/dtod.cc @@ -53,8 +53,7 @@ DumbTOD::DumbTOD(const Params *p) : BasicPioDevice(p, 0x08) { struct tm tm = p->time; - time_t local = mktime(&tm); - todTime = mktime(gmtime(&local)); + todTime = mkutctime(&tm); DPRINTFN("Real-time clock set to %s\n", asctime(&tm)); DPRINTFN("Real-time clock set to %d\n", todTime); -- 2.30.2