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