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. |
21 bool OS::LocalTime(int64_t seconds_since_epoch, tm* tm_result) { | 22 bool OS::LocalTime(int64_t seconds_since_epoch, tm* tm_result) { |
22 time_t seconds = static_cast<time_t>(seconds_since_epoch); | 23 time_t seconds = static_cast<time_t>(seconds_since_epoch); |
23 if (seconds != seconds_since_epoch) return false; | 24 if (seconds != seconds_since_epoch) return false; |
| 25 // localtime_s implicitly sets _timezone, _daylight and _tzname. |
24 errno_t error_code = localtime_s(tm_result, &seconds); | 26 errno_t error_code = localtime_s(tm_result, &seconds); |
25 return error_code == 0; | 27 return error_code == 0; |
26 } | 28 } |
27 | 29 |
28 | 30 |
29 bool OS::MkGmTime(tm* tm, int64_t* seconds_result) { | 31 bool OS::MkGmTime(tm* tm, int64_t* seconds_result) { |
30 // Disable daylight saving. | 32 // Disable daylight saving. |
31 tm->tm_isdst = 0; | 33 tm->tm_isdst = 0; |
32 // Set wday to an impossible day, so that we can catch bad input. | 34 // Set wday to an impossible day, so that we can catch bad input. |
33 tm->tm_wday = -1; | 35 tm->tm_wday = -1; |
(...skipping 13 matching lines...) Expand all Loading... |
47 tm->tm_wday = -1; | 49 tm->tm_wday = -1; |
48 time_t seconds = mktime(tm); | 50 time_t seconds = mktime(tm); |
49 if ((seconds == -1) && (tm->tm_wday == -1)) { | 51 if ((seconds == -1) && (tm->tm_wday == -1)) { |
50 return false; | 52 return false; |
51 } | 53 } |
52 *seconds_result = seconds; | 54 *seconds_result = seconds; |
53 return true; | 55 return true; |
54 } | 56 } |
55 | 57 |
56 | 58 |
| 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 = *offset_result - daylight_bias; |
| 101 } |
| 102 return true; |
| 103 } |
| 104 |
| 105 |
57 int64_t OS::GetCurrentTimeMillis() { | 106 int64_t OS::GetCurrentTimeMillis() { |
58 return GetCurrentTimeMicros() / 1000; | 107 return GetCurrentTimeMicros() / 1000; |
59 } | 108 } |
60 | 109 |
61 | 110 |
62 int64_t OS::GetCurrentTimeMicros() { | 111 int64_t OS::GetCurrentTimeMicros() { |
63 static const int64_t kTimeEpoc = 116444736000000000LL; | 112 static const int64_t kTimeEpoc = 116444736000000000LL; |
64 static const int64_t kTimeScaler = 10; // 100 ns to us. | 113 static const int64_t kTimeScaler = 10; // 100 ns to us. |
65 | 114 |
66 // Although win32 uses 64-bit integers for representing timestamps, | 115 // Although win32 uses 64-bit integers for representing timestamps, |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 void OS::Abort() { | 245 void OS::Abort() { |
197 abort(); | 246 abort(); |
198 } | 247 } |
199 | 248 |
200 | 249 |
201 void OS::Exit(int code) { | 250 void OS::Exit(int code) { |
202 exit(code); | 251 exit(code); |
203 } | 252 } |
204 | 253 |
205 } // namespace dart | 254 } // namespace dart |
OLD | NEW |