OLD | NEW |
---|---|
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 | 17 |
18 namespace dart { | 18 namespace dart { |
19 | 19 |
20 bool OS::BreakDownSecondsSinceEpoch(time_t seconds_since_epoch, | 20 bool OS::BreakDownSecondsSinceEpoch(time_t seconds_since_epoch, |
21 bool in_utc, | 21 bool in_utc, |
22 BrokenDownDate* result) { | 22 BrokenDownDate* result) { |
23 struct tm tm_result; | 23 struct tm tm_result; |
24 struct tm* error_code; | 24 struct tm* error_code; |
25 if (in_utc) { | 25 if (in_utc) { |
26 error_code = gmtime_r(&seconds_since_epoch, &tm_result); | 26 error_code = gmtime_r(&seconds_since_epoch, &tm_result); |
27 } else { | 27 } 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. | 28 tzset(); // Make sure the libc knows about the local zone. |
31 error_code = localtime_r(&seconds_since_epoch, &tm_result); | 29 error_code = localtime_r(&seconds_since_epoch, &tm_result); |
32 } | 30 } |
33 result->year = tm_result.tm_year; | 31 result->year = tm_result.tm_year; |
34 result->month= tm_result.tm_mon; | 32 result->month= tm_result.tm_mon; |
35 result->day = tm_result.tm_mday; | 33 result->day = tm_result.tm_mday; |
36 result->hours = tm_result.tm_hour; | 34 result->hours = tm_result.tm_hour; |
37 result->minutes = tm_result.tm_min; | 35 result->minutes = tm_result.tm_min; |
38 result->seconds = tm_result.tm_sec; | 36 result->seconds = tm_result.tm_sec; |
39 return error_code != NULL; | 37 return error_code != NULL; |
40 } | 38 } |
41 | 39 |
42 | 40 |
41 // TODO(floitsch): cache utc offset. Maybe look at V8's caching mechanism. | |
42 static int UtcOffsetInSeconds(time_t seconds_since_epoch) { | |
cshapiro
2012/04/19 23:52:12
This seems unnecessary, see below.
| |
43 struct tm t; | |
44 struct tm* error_code = localtime_r(&seconds_since_epoch, &t); | |
45 ASSERT(error_code != NULL); | |
46 return t.tm_gmtoff; | |
47 } | |
48 | |
49 | |
43 bool OS::BrokenDownToSecondsSinceEpoch( | 50 bool OS::BrokenDownToSecondsSinceEpoch( |
44 const BrokenDownDate& broken_down, bool in_utc, time_t* result) { | 51 const BrokenDownDate& broken_down, bool in_utc, time_t* result) { |
45 struct tm tm_broken_down; | 52 struct tm tm_broken_down; |
46 // mktime takes the years since 1900. | 53 // mktime takes the years since 1900. |
47 tm_broken_down.tm_year = broken_down.year; | 54 tm_broken_down.tm_year = broken_down.year; |
48 tm_broken_down.tm_mon = broken_down.month; | 55 tm_broken_down.tm_mon = broken_down.month; |
49 tm_broken_down.tm_mday = broken_down.day; | 56 tm_broken_down.tm_mday = broken_down.day; |
50 tm_broken_down.tm_hour = broken_down.hours; | 57 tm_broken_down.tm_hour = broken_down.hours; |
51 tm_broken_down.tm_min = broken_down.minutes; | 58 tm_broken_down.tm_min = broken_down.minutes; |
52 tm_broken_down.tm_sec = broken_down.seconds; | 59 tm_broken_down.tm_sec = broken_down.seconds; |
53 // Set wday to an impossible day, so that we can catch bad input. | 60 // Set wday to an impossible day, so that we can catch bad input. |
54 tm_broken_down.tm_wday = -1; | 61 tm_broken_down.tm_wday = -1; |
55 // Make sure the libc knows about the local zone. | 62 // 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 | 63 // TODO(floitsch): we should be able to call tzset only once during |
60 // initialization. | 64 // initialization. |
61 tzset(); | 65 tzset(); |
62 if (in_utc) { | 66 // Let libc figure out if daylight saving is active. |
63 // Disable daylight saving in utc mode. | 67 tm_broken_down.tm_isdst = -1; |
64 tm_broken_down.tm_isdst = 0; | 68 *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)) { | 69 if ((*result == -1) && (tm_broken_down.tm_wday == -1)) { |
75 return false; | 70 return false; |
76 } | 71 } |
72 if (in_utc) { | |
73 *result += UtcOffsetInSeconds(*result); | |
cshapiro
2012/04/19 23:52:12
See comment in os_macos.cc.
| |
74 } | |
77 return true; | 75 return true; |
78 } | 76 } |
79 | 77 |
80 | 78 |
81 int64_t OS::GetCurrentTimeMillis() { | 79 int64_t OS::GetCurrentTimeMillis() { |
82 return GetCurrentTimeMicros() / 1000; | 80 return GetCurrentTimeMicros() / 1000; |
83 } | 81 } |
84 | 82 |
85 | 83 |
86 int64_t OS::GetCurrentTimeMicros() { | 84 int64_t OS::GetCurrentTimeMicros() { |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
220 void OS::Abort() { | 218 void OS::Abort() { |
221 abort(); | 219 abort(); |
222 } | 220 } |
223 | 221 |
224 | 222 |
225 void OS::Exit(int code) { | 223 void OS::Exit(int code) { |
226 exit(code); | 224 exit(code); |
227 } | 225 } |
228 | 226 |
229 } // namespace dart | 227 } // namespace dart |
OLD | NEW |