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) { |
| 14 time_t seconds = static_cast<time_t>(seconds_since_epoch); |
| 15 if (seconds != seconds_since_epoch) return false; |
| 16 errno_t error_code = gmtime_s(tm_result, &seconds); |
| 17 return error_code == 0; |
| 18 } |
| 19 |
| 20 |
13 // As a side-effect sets the globals _timezone, _daylight and _tzname. | 21 // As a side-effect sets the globals _timezone, _daylight and _tzname. |
14 static bool LocalTime(int64_t seconds_since_epoch, tm* tm_result) { | 22 bool OS::LocalTime(int64_t seconds_since_epoch, tm* tm_result) { |
15 time_t seconds = static_cast<time_t>(seconds_since_epoch); | 23 time_t seconds = static_cast<time_t>(seconds_since_epoch); |
16 if (seconds != seconds_since_epoch) return false; | 24 if (seconds != seconds_since_epoch) return false; |
17 // localtime_s implicitly sets _timezone, _daylight and _tzname. | 25 // localtime_s implicitly sets _timezone, _daylight and _tzname. |
18 errno_t error_code = localtime_s(tm_result, &seconds); | 26 errno_t error_code = localtime_s(tm_result, &seconds); |
19 return error_code == 0; | 27 return error_code == 0; |
20 } | 28 } |
21 | 29 |
22 | 30 |
| 31 bool OS::MkGmTime(tm* tm, int64_t* seconds_result) { |
| 32 // Disable daylight saving. |
| 33 tm->tm_isdst = 0; |
| 34 // Set wday to an impossible day, so that we can catch bad input. |
| 35 tm->tm_wday = -1; |
| 36 time_t seconds = _mkgmtime(tm); |
| 37 if ((seconds == -1) && (tm->tm_wday == -1)) { |
| 38 return false; |
| 39 } |
| 40 *seconds_result = seconds; |
| 41 return true; |
| 42 } |
| 43 |
| 44 |
| 45 bool OS::MkTime(tm* tm, int64_t* seconds_result) { |
| 46 // Let the libc figure out if daylight saving is active. |
| 47 tm->tm_isdst = -1; |
| 48 // Set wday to an impossible day, so that we can catch bad input. |
| 49 tm->tm_wday = -1; |
| 50 time_t seconds = mktime(tm); |
| 51 if ((seconds == -1) && (tm->tm_wday == -1)) { |
| 52 return false; |
| 53 } |
| 54 *seconds_result = seconds; |
| 55 return true; |
| 56 } |
| 57 |
| 58 |
23 static int GetDaylightSavingBiasInSeconds() { | 59 static int GetDaylightSavingBiasInSeconds() { |
24 TIME_ZONE_INFORMATION zone_information; | 60 TIME_ZONE_INFORMATION zone_information; |
25 memset(&zone_information, 0, sizeof(zone_information)); | 61 memset(&zone_information, 0, sizeof(zone_information)); |
26 if (GetTimeZoneInformation(&zone_information) == TIME_ZONE_ID_INVALID) { | 62 if (GetTimeZoneInformation(&zone_information) == TIME_ZONE_ID_INVALID) { |
27 // By default the daylight saving offset is an hour. | 63 // By default the daylight saving offset is an hour. |
28 return -60 * 60; | 64 return -60 * 60; |
29 } else { | 65 } else { |
30 return static_cast<int>(zone_information.DaylightBias * 60); | 66 return static_cast<int>(zone_information.DaylightBias * 60); |
31 } | 67 } |
32 } | 68 } |
33 | 69 |
34 | 70 bool OS::GetTimeZoneName(int64_t seconds_since_epoch, |
35 const char* OS::GetTimeZoneName(int64_t seconds_since_epoch) { | 71 const char** name_result) { |
36 tm decomposed; | 72 tm decomposed; |
37 // LocalTime will set _tzname. | 73 // LocalTime will set _tzname. |
38 bool succeeded = LocalTime(seconds_since_epoch, &decomposed); | 74 bool succeeded = LocalTime(seconds_since_epoch, &decomposed); |
39 ASSERT(succeeded); | 75 if (!succeeded) return false; |
40 int inDaylightSavingsTime = decomposed.tm_isdst; | 76 int inDaylightSavingsTime = decomposed.tm_isdst; |
41 ASSERT(inDaylightSavingsTime == 0 || inDaylightSavingsTime == 1); | 77 if (inDaylightSavingsTime != 0 && inDaylightSavingsTime != 1) { |
42 return _tzname[inDaylightSavingsTime]; | 78 return false; |
| 79 } |
| 80 *name_result = _tzname[inDaylightSavingsTime]; |
| 81 return true; |
43 } | 82 } |
44 | 83 |
45 | 84 |
46 int OS::GetTimeZoneOffsetInSeconds(int64_t seconds_since_epoch) { | 85 bool OS::GetTimeZoneOffsetInSeconds(int64_t seconds_since_epoch, |
| 86 int* offset_result) { |
47 tm decomposed; | 87 tm decomposed; |
48 // LocalTime will set _timezone. | 88 // LocalTime will set _timezone. |
49 bool succeeded = LocalTime(seconds_since_epoch, &decomposed); | 89 bool succeeded = LocalTime(seconds_since_epoch, &decomposed); |
50 ASSERT(succeeded); | 90 if (!succeeded) return false; |
51 int inDaylightSavingsTime = decomposed.tm_isdst; | 91 int inDaylightSavingsTime = decomposed.tm_isdst; |
52 ASSERT(inDaylightSavingsTime == 0 || inDaylightSavingsTime == 1); | 92 if (inDaylightSavingsTime != 0 && inDaylightSavingsTime != 1) { |
| 93 return false; |
| 94 } |
53 // Dart and Windows disagree on the sign of the bias. | 95 // Dart and Windows disagree on the sign of the bias. |
54 int offset = static_cast<int>(-_timezone); | 96 *offset_result = static_cast<int>(-_timezone); |
55 if (inDaylightSavingsTime == 1) { | 97 if (inDaylightSavingsTime == 1) { |
56 static int daylight_bias = GetDaylightSavingBiasInSeconds(); | 98 static int daylight_bias = GetDaylightSavingBiasInSeconds(); |
57 // Subtract because windows and Dart disagree on the sign. | 99 // Subtract because windows and Dart disagree on the sign. |
58 offset = offset - daylight_bias; | 100 *offset_result = *offset_result - daylight_bias; |
59 } | 101 } |
60 return offset; | 102 return true; |
61 } | 103 } |
62 | 104 |
63 | 105 |
64 int OS::GetLocalTimeZoneAdjustmentInSeconds() { | |
65 // TODO(floitsch): avoid excessive calls to _tzset? | |
66 _tzset(); | |
67 // Dart and Windows disagree on the sign of the bias. | |
68 return static_cast<int>(-_timezone); | |
69 } | |
70 | |
71 | |
72 int64_t OS::GetCurrentTimeMillis() { | 106 int64_t OS::GetCurrentTimeMillis() { |
73 return GetCurrentTimeMicros() / 1000; | 107 return GetCurrentTimeMicros() / 1000; |
74 } | 108 } |
75 | 109 |
76 | 110 |
77 int64_t OS::GetCurrentTimeMicros() { | 111 int64_t OS::GetCurrentTimeMicros() { |
78 static const int64_t kTimeEpoc = 116444736000000000LL; | 112 static const int64_t kTimeEpoc = 116444736000000000LL; |
79 static const int64_t kTimeScaler = 10; // 100 ns to us. | 113 static const int64_t kTimeScaler = 10; // 100 ns to us. |
80 | 114 |
81 // 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... |
211 void OS::Abort() { | 245 void OS::Abort() { |
212 abort(); | 246 abort(); |
213 } | 247 } |
214 | 248 |
215 | 249 |
216 void OS::Exit(int code) { | 250 void OS::Exit(int code) { |
217 exit(code); | 251 exit(code); |
218 } | 252 } |
219 | 253 |
220 } // namespace dart | 254 } // namespace dart |
OLD | NEW |