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 <mach/mach.h> | 9 #include <mach/mach.h> |
10 #include <mach/clock.h> | 10 #include <mach/clock.h> |
11 #include <mach/mach_time.h> | 11 #include <mach/mach_time.h> |
12 #include <sys/time.h> | 12 #include <sys/time.h> |
13 #include <sys/resource.h> | 13 #include <sys/resource.h> |
14 #include <unistd.h> | 14 #include <unistd.h> |
15 | 15 |
16 #include "platform/utils.h" | 16 #include "platform/utils.h" |
17 #include "vm/isolate.h" | 17 #include "vm/isolate.h" |
18 #include "vm/thread.h" | |
Ivan Posva
2012/04/11 12:01:24
ditto
floitsch
2012/04/11 12:10:21
Done.
| |
18 | 19 |
19 namespace dart { | 20 namespace dart { |
20 | 21 |
21 bool OS::BreakDownSecondsSinceEpoch(time_t seconds_since_epoch, | 22 bool OS::BreakDownSecondsSinceEpoch(time_t seconds_since_epoch, |
22 bool in_utc, | 23 bool in_utc, |
23 BrokenDownDate* result) { | 24 BrokenDownDate* result) { |
24 struct tm tm_result; | 25 struct tm tm_result; |
25 struct tm* error_code; | 26 struct tm* error_code; |
26 if (in_utc) { | 27 if (in_utc) { |
27 error_code = gmtime_r(&seconds_since_epoch, &tm_result); | 28 error_code = gmtime_r(&seconds_since_epoch, &tm_result); |
28 } else { | 29 } else { |
29 // TODO(floitsch): we should be able to call tzset only once during | |
30 // initialization. | |
31 tzset(); // Make sure the libc knows about the local zone. | |
32 error_code = localtime_r(&seconds_since_epoch, &tm_result); | 30 error_code = localtime_r(&seconds_since_epoch, &tm_result); |
33 } | 31 } |
34 result->year = tm_result.tm_year; | 32 result->year = tm_result.tm_year; |
35 result->month= tm_result.tm_mon; | 33 result->month= tm_result.tm_mon; |
36 result->day = tm_result.tm_mday; | 34 result->day = tm_result.tm_mday; |
37 result->hours = tm_result.tm_hour; | 35 result->hours = tm_result.tm_hour; |
38 result->minutes = tm_result.tm_min; | 36 result->minutes = tm_result.tm_min; |
39 result->seconds = tm_result.tm_sec; | 37 result->seconds = tm_result.tm_sec; |
40 return error_code != NULL; | 38 return error_code != NULL; |
41 } | 39 } |
42 | 40 |
43 | 41 |
42 // TODO(floitsch): cache utc offset. | |
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 | |
44 bool OS::BrokenDownToSecondsSinceEpoch( | 50 bool OS::BrokenDownToSecondsSinceEpoch( |
45 const BrokenDownDate& broken_down, bool in_utc, time_t* result) { | 51 const BrokenDownDate& broken_down, bool in_utc, time_t* result) { |
46 struct tm tm_broken_down; | 52 struct tm tm_broken_down; |
47 // mktime takes the years since 1900. | 53 // mktime takes the years since 1900. |
48 tm_broken_down.tm_year = broken_down.year; | 54 tm_broken_down.tm_year = broken_down.year; |
49 tm_broken_down.tm_mon = broken_down.month; | 55 tm_broken_down.tm_mon = broken_down.month; |
50 tm_broken_down.tm_mday = broken_down.day; | 56 tm_broken_down.tm_mday = broken_down.day; |
51 tm_broken_down.tm_hour = broken_down.hours; | 57 tm_broken_down.tm_hour = broken_down.hours; |
52 tm_broken_down.tm_min = broken_down.minutes; | 58 tm_broken_down.tm_min = broken_down.minutes; |
53 tm_broken_down.tm_sec = broken_down.seconds; | 59 tm_broken_down.tm_sec = broken_down.seconds; |
54 // 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. |
55 tm_broken_down.tm_wday = -1; | 61 tm_broken_down.tm_wday = -1; |
56 // Make sure the libc knows about the local zone. | 62 // Make sure the libc knows about the local zone. |
57 // In case of 'in_utc' this call is mainly for multi-threading issues. If | |
58 // another thread uses a time-function it will set the timezone. The timezone | |
59 // adjustement below would then not work anymore. | |
60 // 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 |
61 // initialization. | 64 // initialization. |
62 tzset(); | 65 tzset(); |
63 if (in_utc) { | 66 // Let libc figure out if daylight saving is active. |
64 // Disable daylight saving in utc mode. | 67 tm_broken_down.tm_isdst = -1; |
65 tm_broken_down.tm_isdst = 0; | 68 *result = mktime(&tm_broken_down); |
66 // mktime assumes that the given date is local time zone. | |
67 *result = mktime(&tm_broken_down); | |
68 // Remove the timezone. | |
69 *result -= timezone; | |
70 } else { | |
71 // Let libc figure out if daylight saving is active. | |
72 tm_broken_down.tm_isdst = -1; | |
73 *result = mktime(&tm_broken_down); | |
74 } | |
75 if ((*result == -1) && (tm_broken_down.tm_wday == -1)) { | 69 if ((*result == -1) && (tm_broken_down.tm_wday == -1)) { |
76 return false; | 70 return false; |
77 } | 71 } |
72 if (in_utc) { | |
73 *result += UtcOffsetInSeconds(*result); | |
74 } | |
78 return true; | 75 return true; |
79 } | 76 } |
80 | 77 |
81 | 78 |
82 int64_t OS::GetCurrentTimeMillis() { | 79 int64_t OS::GetCurrentTimeMillis() { |
83 return GetCurrentTimeMicros() / 1000; | 80 return GetCurrentTimeMicros() / 1000; |
84 } | 81 } |
85 | 82 |
86 | 83 |
87 int64_t OS::GetCurrentTimeMicros() { | 84 int64_t OS::GetCurrentTimeMicros() { |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
193 void OS::Abort() { | 190 void OS::Abort() { |
194 abort(); | 191 abort(); |
195 } | 192 } |
196 | 193 |
197 | 194 |
198 void OS::Exit(int code) { | 195 void OS::Exit(int code) { |
199 exit(code); | 196 exit(code); |
200 } | 197 } |
201 | 198 |
202 } // namespace dart | 199 } // namespace dart |
OLD | NEW |