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

Side by Side 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, 8 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 unified diff | Download patch | Annotate | Revision Log
« runtime/vm/os_linux.cc ('K') | « runtime/vm/os_macos.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "vm/os.h" 5 #include "vm/os.h"
6 6
7 #include <time.h> 7 #include <time.h>
8 8
9 #include "platform/assert.h" 9 #include "platform/assert.h"
10 #include "vm/thread.h"
10 11
11 namespace dart { 12 namespace dart {
12 13
14 static Mutex timezone_mutex;
15
13 bool OS::BreakDownSecondsSinceEpoch(time_t seconds_since_epoch, 16 bool OS::BreakDownSecondsSinceEpoch(time_t seconds_since_epoch,
14 bool in_utc, 17 bool in_utc,
15 BrokenDownDate* result) { 18 BrokenDownDate* result) {
16 struct tm tm_result; 19 struct tm tm_result;
17 errno_t error_code; 20 errno_t error_code;
18 if (in_utc) { 21 if (in_utc) {
19 error_code = gmtime_s(&tm_result, &seconds_since_epoch); 22 error_code = gmtime_s(&tm_result, &seconds_since_epoch);
20 } else { 23 } else {
21 // TODO(floitsch): we should be able to call tzset only once during 24 MutexLocker ml(&timezone_mutex);
25 // TODO(floitsch): we should not need the tzset. According to the man-page
26 // the function already acts as if it invoked tzset. In any case we
27 // shouldn't need to invoke it every time, but only once during
22 // initialization. 28 // initialization.
23 tzset(); // Make sure the libc knows about the local zone. 29 tzset(); // Make sure the libc knows about the local zone.
24 error_code = localtime_s(&tm_result, &seconds_since_epoch); 30 error_code = localtime_s(&tm_result, &seconds_since_epoch);
25 } 31 }
26 result->year = tm_result.tm_year; 32 result->year = tm_result.tm_year;
27 result->month= tm_result.tm_mon; 33 result->month= tm_result.tm_mon;
28 result->day = tm_result.tm_mday; 34 result->day = tm_result.tm_mday;
29 result->hours = tm_result.tm_hour; 35 result->hours = tm_result.tm_hour;
30 result->minutes = tm_result.tm_min; 36 result->minutes = tm_result.tm_min;
31 result->seconds = tm_result.tm_sec; 37 result->seconds = tm_result.tm_sec;
32 return error_code == 0; 38 return error_code == 0;
33 } 39 }
34 40
35 41
36 bool OS::BrokenDownToSecondsSinceEpoch( 42 bool OS::BrokenDownToSecondsSinceEpoch(
37 const BrokenDownDate& broken_down, bool in_utc, time_t* result) { 43 const BrokenDownDate& broken_down, bool in_utc, time_t* result) {
44 // Changing the timezone environment is not thread-safe.
45 MutexLocker ml(&timezone_mutex);
38 struct tm tm_broken_down; 46 struct tm tm_broken_down;
39 // mktime takes the years since 1900. 47 // mktime takes the years since 1900.
40 tm_broken_down.tm_year = broken_down.year; 48 tm_broken_down.tm_year = broken_down.year;
41 tm_broken_down.tm_mon = broken_down.month; 49 tm_broken_down.tm_mon = broken_down.month;
42 tm_broken_down.tm_mday = broken_down.day; 50 tm_broken_down.tm_mday = broken_down.day;
43 tm_broken_down.tm_hour = broken_down.hours; 51 tm_broken_down.tm_hour = broken_down.hours;
44 tm_broken_down.tm_min = broken_down.minutes; 52 tm_broken_down.tm_min = broken_down.minutes;
45 tm_broken_down.tm_sec = broken_down.seconds; 53 tm_broken_down.tm_sec = broken_down.seconds;
46 // Set wday to an impossible day, so that we can catch bad input. 54 // Set wday to an impossible day, so that we can catch bad input.
47 tm_broken_down.tm_wday = -1; 55 tm_broken_down.tm_wday = -1;
48 // Make sure the libc knows about the local zone.
49 // In case of 'in_utc' this call is mainly for multi-threading issues. If
50 // another thread uses a time-function it will set the timezone. The timezone
51 // adjustement below would then not work anymore.
52 // TODO(floitsch): we should be able to call tzset only once during
53 // initialization.
54 tzset();
55 if (in_utc) { 56 if (in_utc) {
57 char* saved_timezone = getenv("TZ");
58 // Set to UTC.
59 setenv("TZ", "", 1);
60 tzset();
56 // Disable daylight saving in utc mode. 61 // Disable daylight saving in utc mode.
57 tm_broken_down.tm_isdst = 0; 62 tm_broken_down.tm_isdst = 0;
58 // mktime assumes that the given date is local time zone.
59 *result = mktime(&tm_broken_down); 63 *result = mktime(&tm_broken_down);
60 // Remove the timezone. 64 // Restore old value of the timezone.
61 *result -= timezone; 65 if (saved_timezone) {
66 setenv("TZ", saved_timezone, 1);
67 } else {
68 unsetenv("TZ");
69 }
70 tzset();
62 } else { 71 } else {
72 // Make sure the libc knows about the local zone.
73 // In case of 'in_utc' this call is mainly for multi-threading issues. If
74 // another thread uses a time-function it will set the timezone. The
75 // timezone adjustement below would then not work anymore.
76 // TODO(floitsch): we should be able to call tzset only once during
77 // initialization.
78 tzset();
63 // Let libc figure out if daylight saving is active. 79 // Let libc figure out if daylight saving is active.
64 tm_broken_down.tm_isdst = -1; 80 tm_broken_down.tm_isdst = -1;
65 *result = mktime(&tm_broken_down); 81 *result = mktime(&tm_broken_down);
66 } 82 }
67 if ((*result == -1) && (tm_broken_down.tm_wday == -1)) { 83 if ((*result == -1) && (tm_broken_down.tm_wday == -1)) {
68 return false; 84 return false;
69 } 85 }
70 return true; 86 return true;
71 } 87 }
72 88
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 void OS::Abort() { 216 void OS::Abort() {
201 abort(); 217 abort();
202 } 218 }
203 219
204 220
205 void OS::Exit(int code) { 221 void OS::Exit(int code) {
206 exit(code); 222 exit(code);
207 } 223 }
208 224
209 } // namespace dart 225 } // namespace dart
OLDNEW
« 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