Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(429)

Unified Diff: runtime/vm/os_win.cc

Issue 9969098: Set the TZ env to get the UTC-ms since epoch. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Protect localtime_r too (thread-safe). Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« runtime/vm/os_linux.cc ('K') | « runtime/vm/os_macos.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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);
« runtime/vm/os_linux.cc ('K') | « runtime/vm/os_macos.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698