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 <time.h> | 7 #include <time.h> |
8 | 8 |
9 #include "platform/assert.h" | 9 #include "platform/assert.h" |
10 | 10 |
11 namespace dart { | 11 namespace dart { |
12 | 12 |
13 bool OS::GmTime(int64_t seconds_since_epoch, tm* tm_result) { | 13 bool OS::GmTime(int64_t seconds_since_epoch, tm* tm_result) { |
14 time_t seconds = static_cast<time_t>(seconds_since_epoch); | 14 time_t seconds = static_cast<time_t>(seconds_since_epoch); |
15 if (seconds != seconds_since_epoch) return false; | 15 if (seconds != seconds_since_epoch) return false; |
16 errno_t error_code = gmtime_s(tm_result, &seconds); | 16 errno_t error_code = gmtime_s(tm_result, &seconds); |
17 return error_code == 0; | 17 return error_code == 0; |
18 } | 18 } |
19 | 19 |
20 | 20 |
21 // As a side-effect sets the globals _timezone, _daylight and _tzname. | |
22 bool OS::LocalTime(int64_t seconds_since_epoch, tm* tm_result) { | 21 bool OS::LocalTime(int64_t seconds_since_epoch, tm* tm_result) { |
23 time_t seconds = static_cast<time_t>(seconds_since_epoch); | 22 time_t seconds = static_cast<time_t>(seconds_since_epoch); |
24 if (seconds != seconds_since_epoch) return false; | 23 if (seconds != seconds_since_epoch) return false; |
25 // localtime_s implicitly sets _timezone, _daylight and _tzname. | |
26 errno_t error_code = localtime_s(tm_result, &seconds); | 24 errno_t error_code = localtime_s(tm_result, &seconds); |
27 return error_code == 0; | 25 return error_code == 0; |
28 } | 26 } |
29 | 27 |
30 | 28 |
31 bool OS::MkGmTime(tm* tm, int64_t* seconds_result) { | 29 bool OS::MkGmTime(tm* tm, int64_t* seconds_result) { |
32 // Disable daylight saving. | 30 // Disable daylight saving. |
33 tm->tm_isdst = 0; | 31 tm->tm_isdst = 0; |
34 // Set wday to an impossible day, so that we can catch bad input. | 32 // Set wday to an impossible day, so that we can catch bad input. |
35 tm->tm_wday = -1; | 33 tm->tm_wday = -1; |
(...skipping 13 matching lines...) Expand all Loading... |
49 tm->tm_wday = -1; | 47 tm->tm_wday = -1; |
50 time_t seconds = mktime(tm); | 48 time_t seconds = mktime(tm); |
51 if ((seconds == -1) && (tm->tm_wday == -1)) { | 49 if ((seconds == -1) && (tm->tm_wday == -1)) { |
52 return false; | 50 return false; |
53 } | 51 } |
54 *seconds_result = seconds; | 52 *seconds_result = seconds; |
55 return true; | 53 return true; |
56 } | 54 } |
57 | 55 |
58 | 56 |
59 static int GetDaylightSavingBiasInSeconds() { | |
60 TIME_ZONE_INFORMATION zone_information; | |
61 memset(&zone_information, 0, sizeof(zone_information)); | |
62 if (GetTimeZoneInformation(&zone_information) == TIME_ZONE_ID_INVALID) { | |
63 // By default the daylight saving offset is an hour. | |
64 return -60 * 60; | |
65 } else { | |
66 return static_cast<int>(zone_information.DaylightBias * 60); | |
67 } | |
68 } | |
69 | |
70 bool OS::GetTimeZoneName(int64_t seconds_since_epoch, | |
71 const char** name_result) { | |
72 tm decomposed; | |
73 // LocalTime will set _tzname. | |
74 bool succeeded = LocalTime(seconds_since_epoch, &decomposed); | |
75 if (!succeeded) return false; | |
76 int inDaylightSavingsTime = decomposed.tm_isdst; | |
77 if (inDaylightSavingsTime != 0 && inDaylightSavingsTime != 1) { | |
78 return false; | |
79 } | |
80 *name_result = _tzname[inDaylightSavingsTime]; | |
81 return true; | |
82 } | |
83 | |
84 | |
85 bool OS::GetTimeZoneOffsetInSeconds(int64_t seconds_since_epoch, | |
86 int* offset_result) { | |
87 tm decomposed; | |
88 // LocalTime will set _timezone. | |
89 bool succeeded = LocalTime(seconds_since_epoch, &decomposed); | |
90 if (!succeeded) return false; | |
91 int inDaylightSavingsTime = decomposed.tm_isdst; | |
92 if (inDaylightSavingsTime != 0 && inDaylightSavingsTime != 1) { | |
93 return false; | |
94 } | |
95 // Dart and Windows disagree on the sign of the bias. | |
96 *offset_result = static_cast<int>(-_timezone); | |
97 if (inDaylightSavingsTime == 1) { | |
98 static int daylight_bias = GetDaylightSavingBiasInSeconds() | |
99 // Subtract because windows and Dart disagree on the sign. | |
100 *offset_result -= daylight_bias; | |
101 } | |
102 return true; | |
103 } | |
104 | |
105 | |
106 int64_t OS::GetCurrentTimeMillis() { | 57 int64_t OS::GetCurrentTimeMillis() { |
107 return GetCurrentTimeMicros() / 1000; | 58 return GetCurrentTimeMicros() / 1000; |
108 } | 59 } |
109 | 60 |
110 | 61 |
111 int64_t OS::GetCurrentTimeMicros() { | 62 int64_t OS::GetCurrentTimeMicros() { |
112 static const int64_t kTimeEpoc = 116444736000000000LL; | 63 static const int64_t kTimeEpoc = 116444736000000000LL; |
113 static const int64_t kTimeScaler = 10; // 100 ns to us. | 64 static const int64_t kTimeScaler = 10; // 100 ns to us. |
114 | 65 |
115 // Although win32 uses 64-bit integers for representing timestamps, | 66 // Although win32 uses 64-bit integers for representing timestamps, |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 void OS::Abort() { | 196 void OS::Abort() { |
246 abort(); | 197 abort(); |
247 } | 198 } |
248 | 199 |
249 | 200 |
250 void OS::Exit(int code) { | 201 void OS::Exit(int code) { |
251 exit(code); | 202 exit(code); |
252 } | 203 } |
253 | 204 |
254 } // namespace dart | 205 } // namespace dart |
OLD | NEW |