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::GmTime(int64_t seconds_since_epoch, tm* tm_result) { | 21 static bool LocalTime(int64_t seconds_since_epoch, tm* tm_result) { |
22 time_t seconds = static_cast<time_t>(seconds_since_epoch); | |
23 if (seconds != seconds_since_epoch) return false; | |
24 struct tm* error_code = gmtime_r(&seconds, tm_result); | |
25 return error_code != NULL; | |
26 } | |
27 | |
28 | |
29 bool OS::LocalTime(int64_t seconds_since_epoch, tm* tm_result) { | |
30 time_t seconds = static_cast<time_t>(seconds_since_epoch); | 22 time_t seconds = static_cast<time_t>(seconds_since_epoch); |
31 if (seconds != seconds_since_epoch) return false; | 23 if (seconds != seconds_since_epoch) return false; |
32 struct tm* error_code = localtime_r(&seconds, tm_result); | 24 struct tm* error_code = localtime_r(&seconds, tm_result); |
33 return error_code != NULL; | 25 return error_code != NULL; |
34 } | 26 } |
35 | 27 |
36 | 28 |
37 bool OS::MkGmTime(tm* tm, int64_t* seconds_result) { | 29 const char* OS::GetTimeZoneName(int64_t seconds_since_epoch) { |
38 // Set wday to an impossible day, so that we can catch bad input. | 30 tm decomposed; |
39 tm->tm_wday = -1; | 31 bool succeeded = LocalTime(seconds_since_epoch, &decomposed); |
40 time_t seconds = timegm(tm); | 32 ASSERT(succeeded); |
41 if ((seconds == -1) && (tm->tm_wday == -1)) { | 33 return decomposed.tm_zone; |
42 return false; | |
43 } | |
44 *seconds_result = seconds; | |
45 return true; | |
46 } | 34 } |
47 | 35 |
48 | 36 |
49 bool OS::MkTime(tm* tm, int64_t* seconds_result) { | 37 bool OS::GetTimeZoneOffsetInSeconds(int64_t seconds_since_epoch) { |
50 // Let the libc figure out if daylight saving is active. | 38 tm decomposed; |
51 tm->tm_isdst = -1; | 39 bool succeeded = LocalTime(seconds_since_epoch, &decomposed); |
52 // Set wday to an impossible day, so that we can catch bad input. | 40 ASSERT(succeeded); |
53 tm->tm_wday = -1; | 41 // Even if the offset was 24 hours it would still easily fit into 32 bits. |
54 time_t seconds = mktime(tm); | 42 return static_cast<int>(decomposed.tm_gmtoff); |
55 if ((seconds == -1) && (tm->tm_wday == -1)) { | |
56 return false; | |
57 } | |
58 *seconds_result = seconds; | |
59 return true; | |
60 } | 43 } |
61 | 44 |
62 | 45 |
63 bool OS::GetTimeZoneName(int64_t seconds_since_epoch, | 46 int OS::GetLocalTimeZoneAdjustmentInSeconds() { |
64 const char** name_result) { | 47 // TODO(floitsch): avoid excessive calls to tzset? |
65 tm decomposed; | 48 tzset(); |
66 bool succeeded = LocalTime(seconds_since_epoch, &decomposed); | 49 // Even if the offset was 24 hours it would still easily fit into 32 bits. |
67 if (!succeeded) return false; | 50 // Note that Unix and Dart disagree on the sign. |
68 *name_result = decomposed.tm_zone; | 51 return static_cast<int>(-timezone); |
69 return true; | |
70 } | 52 } |
71 | 53 |
72 | 54 |
73 bool OS::GetTimeZoneOffsetInSeconds(int64_t seconds_since_epoch, | |
74 int* offset_result) { | |
75 tm decomposed; | |
76 bool succeeded = LocalTime(seconds_since_epoch, &decomposed); | |
77 if (!succeeded) return false; | |
78 // Even if the offset was 24 hours it would still easily fit into 32 bits. | |
79 *offset_result = static_cast<int>(decomposed.tm_gmtoff); | |
80 return true; | |
81 } | |
82 | |
83 | |
84 int64_t OS::GetCurrentTimeMillis() { | 55 int64_t OS::GetCurrentTimeMillis() { |
85 return GetCurrentTimeMicros() / 1000; | 56 return GetCurrentTimeMicros() / 1000; |
86 } | 57 } |
87 | 58 |
88 | 59 |
89 int64_t OS::GetCurrentTimeMicros() { | 60 int64_t OS::GetCurrentTimeMicros() { |
90 // gettimeofday has microsecond resolution. | 61 // gettimeofday has microsecond resolution. |
91 struct timeval tv; | 62 struct timeval tv; |
92 if (gettimeofday(&tv, NULL) < 0) { | 63 if (gettimeofday(&tv, NULL) < 0) { |
93 UNREACHABLE(); | 64 UNREACHABLE(); |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 void OS::Abort() { | 166 void OS::Abort() { |
196 abort(); | 167 abort(); |
197 } | 168 } |
198 | 169 |
199 | 170 |
200 void OS::Exit(int code) { | 171 void OS::Exit(int code) { |
201 exit(code); | 172 exit(code); |
202 } | 173 } |
203 | 174 |
204 } // namespace dart | 175 } // namespace dart |
OLD | NEW |