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