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