| 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::BreakDownSecondsSinceEpoch(time_t seconds_since_epoch, | 20 bool OS::GmTime(int64_t seconds_since_epoch, tm* tm_result) { |
| 21 bool in_utc, | 21 time_t seconds = static_cast<time_t>(seconds_since_epoch); |
| 22 BrokenDownDate* result) { | 22 if (seconds != seconds_since_epoch) return false; |
| 23 struct tm tm_result; | 23 struct tm* error_code = gmtime_r(&seconds, tm_result); |
| 24 struct tm* error_code; | |
| 25 if (in_utc) { | |
| 26 error_code = gmtime_r(&seconds_since_epoch, &tm_result); | |
| 27 } else { | |
| 28 // TODO(floitsch): we should be able to call tzset only once during | |
| 29 // initialization. | |
| 30 tzset(); // Make sure the libc knows about the local zone. | |
| 31 error_code = localtime_r(&seconds_since_epoch, &tm_result); | |
| 32 } | |
| 33 result->year = tm_result.tm_year; | |
| 34 result->month= tm_result.tm_mon; | |
| 35 result->day = tm_result.tm_mday; | |
| 36 result->hours = tm_result.tm_hour; | |
| 37 result->minutes = tm_result.tm_min; | |
| 38 result->seconds = tm_result.tm_sec; | |
| 39 return error_code != NULL; | 24 return error_code != NULL; |
| 40 } | 25 } |
| 41 | 26 |
| 42 | 27 |
| 43 bool OS::BrokenDownToSecondsSinceEpoch( | 28 bool OS::LocalTime(int64_t seconds_since_epoch, tm* tm_result) { |
| 44 const BrokenDownDate& broken_down, bool in_utc, time_t* result) { | 29 time_t seconds = static_cast<time_t>(seconds_since_epoch); |
| 45 struct tm tm_broken_down; | 30 if (seconds != seconds_since_epoch) return false; |
| 46 // mktime takes the years since 1900. | 31 struct tm* error_code = localtime_r(&seconds, tm_result); |
| 47 tm_broken_down.tm_year = broken_down.year; | 32 return error_code != NULL; |
| 48 tm_broken_down.tm_mon = broken_down.month; | 33 } |
| 49 tm_broken_down.tm_mday = broken_down.day; | 34 |
| 50 tm_broken_down.tm_hour = broken_down.hours; | 35 |
| 51 tm_broken_down.tm_min = broken_down.minutes; | 36 bool OS::MkGmTime(tm* tm, int64_t* seconds_result) { |
| 52 tm_broken_down.tm_sec = broken_down.seconds; | |
| 53 // Set wday to an impossible day, so that we can catch bad input. | 37 // Set wday to an impossible day, so that we can catch bad input. |
| 54 tm_broken_down.tm_wday = -1; | 38 tm->tm_wday = -1; |
| 55 // Make sure the libc knows about the local zone. | 39 time_t seconds = timegm(tm); |
| 56 // In case of 'in_utc' this call is mainly for multi-threading issues. If | 40 if ((seconds == -1) && (tm->tm_wday == -1)) { |
| 57 // another thread uses a time-function it will set the timezone. The timezone | |
| 58 // adjustement below would then not work anymore. | |
| 59 // TODO(floitsch): we should be able to call tzset only once during | |
| 60 // initialization. | |
| 61 tzset(); | |
| 62 if (in_utc) { | |
| 63 // Disable daylight saving in utc mode. | |
| 64 tm_broken_down.tm_isdst = 0; | |
| 65 // mktime assumes that the given date is local time zone. | |
| 66 *result = mktime(&tm_broken_down); | |
| 67 // Remove the timezone. | |
| 68 *result -= timezone; | |
| 69 } else { | |
| 70 // Let libc figure out if daylight saving is active. | |
| 71 tm_broken_down.tm_isdst = -1; | |
| 72 *result = mktime(&tm_broken_down); | |
| 73 } | |
| 74 if ((*result == -1) && (tm_broken_down.tm_wday == -1)) { | |
| 75 return false; | 41 return false; |
| 76 } | 42 } |
| 43 *seconds_result = seconds; |
| 77 return true; | 44 return true; |
| 78 } | 45 } |
| 79 | 46 |
| 47 |
| 48 bool OS::MkTime(tm* tm, int64_t* seconds_result) { |
| 49 // Let the libc figure out if daylight saving is active. |
| 50 tm->tm_isdst = -1; |
| 51 // Set wday to an impossible day, so that we can catch bad input. |
| 52 tm->tm_wday = -1; |
| 53 time_t seconds = mktime(tm); |
| 54 if ((seconds == -1) && (tm->tm_wday == -1)) { |
| 55 return false; |
| 56 } |
| 57 *seconds_result = seconds; |
| 58 return true; |
| 59 } |
| 60 |
| 80 | 61 |
| 81 int64_t OS::GetCurrentTimeMillis() { | 62 int64_t OS::GetCurrentTimeMillis() { |
| 82 return GetCurrentTimeMicros() / 1000; | 63 return GetCurrentTimeMicros() / 1000; |
| 83 } | 64 } |
| 84 | 65 |
| 85 | 66 |
| 86 int64_t OS::GetCurrentTimeMicros() { | 67 int64_t OS::GetCurrentTimeMicros() { |
| 87 // gettimeofday has microsecond resolution. | 68 // gettimeofday has microsecond resolution. |
| 88 struct timeval tv; | 69 struct timeval tv; |
| 89 if (gettimeofday(&tv, NULL) < 0) { | 70 if (gettimeofday(&tv, NULL) < 0) { |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 void OS::Abort() { | 201 void OS::Abort() { |
| 221 abort(); | 202 abort(); |
| 222 } | 203 } |
| 223 | 204 |
| 224 | 205 |
| 225 void OS::Exit(int code) { | 206 void OS::Exit(int code) { |
| 226 exit(code); | 207 exit(code); |
| 227 } | 208 } |
| 228 | 209 |
| 229 } // namespace dart | 210 } // namespace dart |
| OLD | NEW |