Index: runtime/vm/os_win.cc |
diff --git a/runtime/vm/os_win.cc b/runtime/vm/os_win.cc |
index 2acef13fe304b92e735082d851fb144e2524bd51..91dacd723e7beaed5911ffbd778344568d46a16b 100644 |
--- a/runtime/vm/os_win.cc |
+++ b/runtime/vm/os_win.cc |
@@ -7,9 +7,12 @@ |
#include <time.h> |
#include "platform/assert.h" |
+#include "vm/thread.h" |
namespace dart { |
+static Mutex timezone_mutex; |
+ |
bool OS::BreakDownSecondsSinceEpoch(time_t seconds_since_epoch, |
bool in_utc, |
BrokenDownDate* result) { |
@@ -18,7 +21,10 @@ bool OS::BreakDownSecondsSinceEpoch(time_t seconds_since_epoch, |
if (in_utc) { |
error_code = gmtime_s(&tm_result, &seconds_since_epoch); |
} else { |
- // TODO(floitsch): we should be able to call tzset only once during |
+ MutexLocker ml(&timezone_mutex); |
+ // TODO(floitsch): we should not need the tzset. According to the man-page |
+ // the function already acts as if it invoked tzset. In any case we |
+ // shouldn't need to invoke it every time, but only once during |
// initialization. |
tzset(); // Make sure the libc knows about the local zone. |
error_code = localtime_s(&tm_result, &seconds_since_epoch); |
@@ -35,6 +41,8 @@ bool OS::BreakDownSecondsSinceEpoch(time_t seconds_since_epoch, |
bool OS::BrokenDownToSecondsSinceEpoch( |
const BrokenDownDate& broken_down, bool in_utc, time_t* result) { |
+ // Changing the timezone environment is not thread-safe. |
+ MutexLocker ml(&timezone_mutex); |
struct tm tm_broken_down; |
// mktime takes the years since 1900. |
tm_broken_down.tm_year = broken_down.year; |
@@ -45,21 +53,29 @@ bool OS::BrokenDownToSecondsSinceEpoch( |
tm_broken_down.tm_sec = broken_down.seconds; |
// Set wday to an impossible day, so that we can catch bad input. |
tm_broken_down.tm_wday = -1; |
- // Make sure the libc knows about the local zone. |
- // In case of 'in_utc' this call is mainly for multi-threading issues. If |
- // another thread uses a time-function it will set the timezone. The timezone |
- // adjustement below would then not work anymore. |
- // TODO(floitsch): we should be able to call tzset only once during |
- // initialization. |
- tzset(); |
if (in_utc) { |
+ char* saved_timezone = getenv("TZ"); |
+ // Set to UTC. |
+ setenv("TZ", "", 1); |
+ tzset(); |
// Disable daylight saving in utc mode. |
tm_broken_down.tm_isdst = 0; |
- // mktime assumes that the given date is local time zone. |
*result = mktime(&tm_broken_down); |
- // Remove the timezone. |
- *result -= timezone; |
+ // Restore old value of the timezone. |
+ if (saved_timezone) { |
+ setenv("TZ", saved_timezone, 1); |
+ } else { |
+ unsetenv("TZ"); |
+ } |
+ tzset(); |
} else { |
+ // Make sure the libc knows about the local zone. |
+ // In case of 'in_utc' this call is mainly for multi-threading issues. If |
+ // another thread uses a time-function it will set the timezone. The |
+ // timezone adjustement below would then not work anymore. |
+ // TODO(floitsch): we should be able to call tzset only once during |
+ // initialization. |
+ tzset(); |
// Let libc figure out if daylight saving is active. |
tm_broken_down.tm_isdst = -1; |
*result = mktime(&tm_broken_down); |