Index: runtime/vm/os_macos.cc |
diff --git a/runtime/vm/os_macos.cc b/runtime/vm/os_macos.cc |
index e6107650d00cec279b45ca5bdf231d49aa18e632..f2d882c87ffbc4358accae1e22ca1c3c83d00a44 100644 |
--- a/runtime/vm/os_macos.cc |
+++ b/runtime/vm/os_macos.cc |
@@ -15,9 +15,12 @@ |
#include "platform/utils.h" |
#include "vm/isolate.h" |
+#include "vm/thread.h" |
namespace dart { |
+static Mutex timezone_mutex; |
+ |
bool OS::BreakDownSecondsSinceEpoch(time_t seconds_since_epoch, |
bool in_utc, |
BrokenDownDate* result) { |
@@ -26,7 +29,10 @@ bool OS::BreakDownSecondsSinceEpoch(time_t seconds_since_epoch, |
if (in_utc) { |
error_code = gmtime_r(&seconds_since_epoch, &tm_result); |
} 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_r(&seconds_since_epoch, &tm_result); |
@@ -43,6 +49,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; |
@@ -53,21 +61,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); |