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