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