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

Unified Diff: runtime/vm/os_linux.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
« no previous file with comments | « no previous file | runtime/vm/os_macos.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/vm/os_linux.cc
diff --git a/runtime/vm/os_linux.cc b/runtime/vm/os_linux.cc
index 52bdaf7b538f92e3abf87cdd86acc0d4a8f692ca..e119648aee506b53b0f66923a1f890e8408da0f9 100644
--- a/runtime/vm/os_linux.cc
+++ b/runtime/vm/os_linux.cc
@@ -14,9 +14,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) {
@@ -25,7 +28,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);
@@ -42,6 +48,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);
Ivan Posva 2012/04/03 23:03:52 You might be protecting against other Dart VM base
floitsch 2012/04/04 08:27:43 I never thought I would be safe against the embedd
struct tm tm_broken_down;
// mktime takes the years since 1900.
tm_broken_down.tm_year = broken_down.year;
@@ -52,21 +60,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);
« no previous file with comments | « no previous file | runtime/vm/os_macos.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698