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 | 18 |
19 namespace dart { | 19 namespace dart { |
20 | 20 |
21 bool OS::BreakDownSecondsSinceEpoch(time_t seconds_since_epoch, | 21 bool OS::BreakDownSecondsSinceEpoch(time_t seconds_since_epoch, |
22 bool in_utc, | 22 bool in_utc, |
23 BrokenDownDate* result) { | 23 BrokenDownDate* result) { |
24 struct tm tm_result; | 24 struct tm tm_result; |
25 struct tm* error_code; | 25 struct tm* error_code; |
26 if (in_utc) { | 26 if (in_utc) { |
27 error_code = gmtime_r(&seconds_since_epoch, &tm_result); | 27 error_code = gmtime_r(&seconds_since_epoch, &tm_result); |
28 } else { | 28 } 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. | |
cshapiro
2012/04/19 23:52:12
I am confused by this call being removed for Win32
floitsch
2012/04/20 16:09:57
removed.
| |
32 error_code = localtime_r(&seconds_since_epoch, &tm_result); | 29 error_code = localtime_r(&seconds_since_epoch, &tm_result); |
33 } | 30 } |
34 result->year = tm_result.tm_year; | 31 result->year = tm_result.tm_year; |
35 result->month= tm_result.tm_mon; | 32 result->month= tm_result.tm_mon; |
36 result->day = tm_result.tm_mday; | 33 result->day = tm_result.tm_mday; |
37 result->hours = tm_result.tm_hour; | 34 result->hours = tm_result.tm_hour; |
38 result->minutes = tm_result.tm_min; | 35 result->minutes = tm_result.tm_min; |
39 result->seconds = tm_result.tm_sec; | 36 result->seconds = tm_result.tm_sec; |
40 return error_code != NULL; | 37 return error_code != NULL; |
41 } | 38 } |
42 | 39 |
43 | 40 |
41 // TODO(floitsch): cache utc offset. | |
cshapiro
2012/04/19 23:52:12
This seems unnecessary. See below.
| |
42 static int UtcOffsetInSeconds(time_t seconds_since_epoch) { | |
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 | |
44 bool OS::BrokenDownToSecondsSinceEpoch( | 49 bool OS::BrokenDownToSecondsSinceEpoch( |
45 const BrokenDownDate& broken_down, bool in_utc, time_t* result) { | 50 const BrokenDownDate& broken_down, bool in_utc, time_t* result) { |
46 struct tm tm_broken_down; | 51 struct tm tm_broken_down; |
47 // mktime takes the years since 1900. | 52 // mktime takes the years since 1900. |
48 tm_broken_down.tm_year = broken_down.year; | 53 tm_broken_down.tm_year = broken_down.year; |
49 tm_broken_down.tm_mon = broken_down.month; | 54 tm_broken_down.tm_mon = broken_down.month; |
50 tm_broken_down.tm_mday = broken_down.day; | 55 tm_broken_down.tm_mday = broken_down.day; |
51 tm_broken_down.tm_hour = broken_down.hours; | 56 tm_broken_down.tm_hour = broken_down.hours; |
52 tm_broken_down.tm_min = broken_down.minutes; | 57 tm_broken_down.tm_min = broken_down.minutes; |
53 tm_broken_down.tm_sec = broken_down.seconds; | 58 tm_broken_down.tm_sec = broken_down.seconds; |
54 // Set wday to an impossible day, so that we can catch bad input. | 59 // Set wday to an impossible day, so that we can catch bad input. |
55 tm_broken_down.tm_wday = -1; | 60 tm_broken_down.tm_wday = -1; |
56 // Make sure the libc knows about the local zone. | 61 // 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 | 62 // TODO(floitsch): we should be able to call tzset only once during |
61 // initialization. | 63 // initialization. |
62 tzset(); | 64 tzset(); |
63 if (in_utc) { | 65 // Let libc figure out if daylight saving is active. |
64 // Disable daylight saving in utc mode. | 66 tm_broken_down.tm_isdst = -1; |
65 tm_broken_down.tm_isdst = 0; | 67 *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)) { | 68 if ((*result == -1) && (tm_broken_down.tm_wday == -1)) { |
76 return false; | 69 return false; |
77 } | 70 } |
71 if (in_utc) { | |
72 *result += UtcOffsetInSeconds(*result); | |
cshapiro
2012/04/19 23:52:12
use timegm instead of mktime and correcting with U
floitsch
2012/04/20 16:09:57
The manpage of timegm explicitly states that it is
cshapiro
2012/04/20 20:47:49
I see no mention of timegm being deprecated or tha
floitsch
2012/04/23 08:37:09
Ok. "deprecated" was too strong. But they explicit
| |
73 } | |
78 return true; | 74 return true; |
79 } | 75 } |
80 | 76 |
81 | 77 |
82 int64_t OS::GetCurrentTimeMillis() { | 78 int64_t OS::GetCurrentTimeMillis() { |
83 return GetCurrentTimeMicros() / 1000; | 79 return GetCurrentTimeMicros() / 1000; |
84 } | 80 } |
85 | 81 |
86 | 82 |
87 int64_t OS::GetCurrentTimeMicros() { | 83 int64_t OS::GetCurrentTimeMicros() { |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
193 void OS::Abort() { | 189 void OS::Abort() { |
194 abort(); | 190 abort(); |
195 } | 191 } |
196 | 192 |
197 | 193 |
198 void OS::Exit(int code) { | 194 void OS::Exit(int code) { |
199 exit(code); | 195 exit(code); |
200 } | 196 } |
201 | 197 |
202 } // namespace dart | 198 } // namespace dart |
OLD | NEW |