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

Side by Side 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: Remove spurious comment and unify win-code with other platforms. 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
« no previous file with comments | « no previous file | runtime/vm/os_macos.cc » ('j') | runtime/vm/os_macos.cc » ('J')
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 <errno.h> 7 #include <errno.h>
8 #include <limits.h> 8 #include <limits.h>
9 #include <time.h> 9 #include <time.h>
10 #include <sys/resource.h> 10 #include <sys/resource.h>
11 #include <sys/time.h> 11 #include <sys/time.h>
12 #include <sys/types.h> 12 #include <sys/types.h>
13 #include <unistd.h> 13 #include <unistd.h>
14 14
15 #include "platform/utils.h" 15 #include "platform/utils.h"
16 #include "vm/isolate.h" 16 #include "vm/isolate.h"
17 #include "vm/thread.h"
Ivan Posva 2012/04/11 12:01:24 Please only include what you need.
floitsch 2012/04/11 12:10:21 Done.
17 18
18 namespace dart { 19 namespace dart {
19 20
20 bool OS::BreakDownSecondsSinceEpoch(time_t seconds_since_epoch, 21 bool OS::BreakDownSecondsSinceEpoch(time_t seconds_since_epoch,
21 bool in_utc, 22 bool in_utc,
22 BrokenDownDate* result) { 23 BrokenDownDate* result) {
23 struct tm tm_result; 24 struct tm tm_result;
24 struct tm* error_code; 25 struct tm* error_code;
25 if (in_utc) { 26 if (in_utc) {
26 error_code = gmtime_r(&seconds_since_epoch, &tm_result); 27 error_code = gmtime_r(&seconds_since_epoch, &tm_result);
27 } else { 28 } else {
28 // TODO(floitsch): we should be able to call tzset only once during
29 // initialization.
30 tzset(); // Make sure the libc knows about the local zone. 29 tzset(); // Make sure the libc knows about the local zone.
31 error_code = localtime_r(&seconds_since_epoch, &tm_result); 30 error_code = localtime_r(&seconds_since_epoch, &tm_result);
32 } 31 }
33 result->year = tm_result.tm_year; 32 result->year = tm_result.tm_year;
34 result->month= tm_result.tm_mon; 33 result->month= tm_result.tm_mon;
35 result->day = tm_result.tm_mday; 34 result->day = tm_result.tm_mday;
36 result->hours = tm_result.tm_hour; 35 result->hours = tm_result.tm_hour;
37 result->minutes = tm_result.tm_min; 36 result->minutes = tm_result.tm_min;
38 result->seconds = tm_result.tm_sec; 37 result->seconds = tm_result.tm_sec;
39 return error_code != NULL; 38 return error_code != NULL;
40 } 39 }
41 40
42 41
42 // TODO(floitsch): cache utc offset.
Ivan Posva 2012/04/11 12:01:24 If you cache how long are you willing to cache the
floitsch 2012/04/11 12:10:21 I would do the same as V8. Changed comment to incl
43 static int UtcOffsetInSeconds(time_t seconds_since_epoch) {
44 struct tm t;
45 struct tm* error_code = localtime_r(&seconds_since_epoch, &t);
46 ASSERT(error_code != NULL);
47 return t.tm_gmtoff;
48 }
49
50
43 bool OS::BrokenDownToSecondsSinceEpoch( 51 bool OS::BrokenDownToSecondsSinceEpoch(
44 const BrokenDownDate& broken_down, bool in_utc, time_t* result) { 52 const BrokenDownDate& broken_down, bool in_utc, time_t* result) {
45 struct tm tm_broken_down; 53 struct tm tm_broken_down;
46 // mktime takes the years since 1900. 54 // mktime takes the years since 1900.
47 tm_broken_down.tm_year = broken_down.year; 55 tm_broken_down.tm_year = broken_down.year;
48 tm_broken_down.tm_mon = broken_down.month; 56 tm_broken_down.tm_mon = broken_down.month;
49 tm_broken_down.tm_mday = broken_down.day; 57 tm_broken_down.tm_mday = broken_down.day;
50 tm_broken_down.tm_hour = broken_down.hours; 58 tm_broken_down.tm_hour = broken_down.hours;
51 tm_broken_down.tm_min = broken_down.minutes; 59 tm_broken_down.tm_min = broken_down.minutes;
52 tm_broken_down.tm_sec = broken_down.seconds; 60 tm_broken_down.tm_sec = broken_down.seconds;
53 // Set wday to an impossible day, so that we can catch bad input. 61 // Set wday to an impossible day, so that we can catch bad input.
54 tm_broken_down.tm_wday = -1; 62 tm_broken_down.tm_wday = -1;
55 // Make sure the libc knows about the local zone. 63 // Make sure the libc knows about the local zone.
56 // In case of 'in_utc' this call is mainly for multi-threading issues. If
57 // another thread uses a time-function it will set the timezone. The timezone
58 // adjustement below would then not work anymore.
59 // TODO(floitsch): we should be able to call tzset only once during 64 // TODO(floitsch): we should be able to call tzset only once during
60 // initialization. 65 // initialization.
61 tzset(); 66 tzset();
62 if (in_utc) { 67 // Let libc figure out if daylight saving is active.
63 // Disable daylight saving in utc mode. 68 tm_broken_down.tm_isdst = -1;
64 tm_broken_down.tm_isdst = 0; 69 *result = mktime(&tm_broken_down);
65 // mktime assumes that the given date is local time zone.
66 *result = mktime(&tm_broken_down);
67 // Remove the timezone.
68 *result -= timezone;
69 } else {
70 // Let libc figure out if daylight saving is active.
71 tm_broken_down.tm_isdst = -1;
72 *result = mktime(&tm_broken_down);
73 }
74 if ((*result == -1) && (tm_broken_down.tm_wday == -1)) { 70 if ((*result == -1) && (tm_broken_down.tm_wday == -1)) {
75 return false; 71 return false;
76 } 72 }
73 if (in_utc) {
74 *result += UtcOffsetInSeconds(*result);
75 }
77 return true; 76 return true;
78 } 77 }
79 78
80 79
81 int64_t OS::GetCurrentTimeMillis() { 80 int64_t OS::GetCurrentTimeMillis() {
82 return GetCurrentTimeMicros() / 1000; 81 return GetCurrentTimeMicros() / 1000;
83 } 82 }
84 83
85 84
86 int64_t OS::GetCurrentTimeMicros() { 85 int64_t OS::GetCurrentTimeMicros() {
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 void OS::Abort() { 219 void OS::Abort() {
221 abort(); 220 abort();
222 } 221 }
223 222
224 223
225 void OS::Exit(int code) { 224 void OS::Exit(int code) {
226 exit(code); 225 exit(code);
227 } 226 }
228 227
229 } // namespace dart 228 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | runtime/vm/os_macos.cc » ('j') | runtime/vm/os_macos.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698