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 <mach/mach.h> | 9 #include <mach/mach.h> |
10 #include <mach/clock.h> | 10 #include <mach/clock.h> |
11 #include <mach/mach_time.h> | 11 #include <mach/mach_time.h> |
12 #include <sys/time.h> | 12 #include <sys/time.h> |
13 #include <sys/resource.h> | 13 #include <sys/resource.h> |
14 #include <unistd.h> | 14 #include <unistd.h> |
15 | 15 |
16 #include "platform/utils.h" | 16 #include "platform/utils.h" |
17 #include "vm/isolate.h" | 17 #include "vm/isolate.h" |
18 | 18 |
19 namespace dart { | 19 namespace dart { |
20 | 20 |
21 bool OS::GmTime(int64_t seconds_since_epoch, tm* tm_result) { | 21 bool OS::BreakDownSecondsSinceEpoch(time_t seconds_since_epoch, |
22 time_t seconds = static_cast<time_t>(seconds_since_epoch); | 22 bool in_utc, |
23 if (seconds != seconds_since_epoch) return false; | 23 BrokenDownDate* result) { |
24 struct tm* error_code = gmtime_r(&seconds, tm_result); | 24 struct tm tm_result; |
| 25 struct tm* error_code; |
| 26 if (in_utc) { |
| 27 error_code = gmtime_r(&seconds_since_epoch, &tm_result); |
| 28 } else { |
| 29 // TODO(floitsch): we should be able to call tzset only once during |
| 30 // initialization. |
| 31 tzset(); // Make sure the libc knows about the local zone. |
| 32 error_code = localtime_r(&seconds_since_epoch, &tm_result); |
| 33 } |
| 34 result->year = tm_result.tm_year; |
| 35 result->month= tm_result.tm_mon; |
| 36 result->day = tm_result.tm_mday; |
| 37 result->hours = tm_result.tm_hour; |
| 38 result->minutes = tm_result.tm_min; |
| 39 result->seconds = tm_result.tm_sec; |
25 return error_code != NULL; | 40 return error_code != NULL; |
26 } | 41 } |
27 | 42 |
28 | 43 |
29 bool OS::LocalTime(int64_t seconds_since_epoch, tm* tm_result) { | 44 bool OS::BrokenDownToSecondsSinceEpoch( |
30 time_t seconds = static_cast<time_t>(seconds_since_epoch); | 45 const BrokenDownDate& broken_down, bool in_utc, time_t* result) { |
31 if (seconds != seconds_since_epoch) return false; | 46 struct tm tm_broken_down; |
32 struct tm* error_code = localtime_r(&seconds, tm_result); | 47 // mktime takes the years since 1900. |
33 return error_code != NULL; | 48 tm_broken_down.tm_year = broken_down.year; |
34 } | 49 tm_broken_down.tm_mon = broken_down.month; |
35 | 50 tm_broken_down.tm_mday = broken_down.day; |
36 | 51 tm_broken_down.tm_hour = broken_down.hours; |
37 bool OS::MkGmTime(tm* tm, int64_t* seconds_result) { | 52 tm_broken_down.tm_min = broken_down.minutes; |
| 53 tm_broken_down.tm_sec = broken_down.seconds; |
38 // Set wday to an impossible day, so that we can catch bad input. | 54 // Set wday to an impossible day, so that we can catch bad input. |
39 tm->tm_wday = -1; | 55 tm_broken_down.tm_wday = -1; |
40 time_t seconds = timegm(tm); | 56 // Make sure the libc knows about the local zone. |
41 if ((seconds == -1) && (tm->tm_wday == -1)) { | 57 // In case of 'in_utc' this call is mainly for multi-threading issues. If |
| 58 // another thread uses a time-function it will set the timezone. The timezone |
| 59 // adjustement below would then not work anymore. |
| 60 // TODO(floitsch): we should be able to call tzset only once during |
| 61 // initialization. |
| 62 tzset(); |
| 63 if (in_utc) { |
| 64 // Disable daylight saving in utc mode. |
| 65 tm_broken_down.tm_isdst = 0; |
| 66 // mktime assumes that the given date is local time zone. |
| 67 *result = mktime(&tm_broken_down); |
| 68 // Remove the timezone. |
| 69 *result -= timezone; |
| 70 } else { |
| 71 // Let libc figure out if daylight saving is active. |
| 72 tm_broken_down.tm_isdst = -1; |
| 73 *result = mktime(&tm_broken_down); |
| 74 } |
| 75 if ((*result == -1) && (tm_broken_down.tm_wday == -1)) { |
42 return false; | 76 return false; |
43 } | 77 } |
44 *seconds_result = seconds; | |
45 return true; | 78 return true; |
46 } | 79 } |
47 | 80 |
48 | |
49 bool OS::MkTime(tm* tm, int64_t* seconds_result) { | |
50 // Let the libc figure out if daylight saving is active. | |
51 tm->tm_isdst = -1; | |
52 // Set wday to an impossible day, so that we can catch bad input. | |
53 tm->tm_wday = -1; | |
54 time_t seconds = mktime(tm); | |
55 if ((seconds == -1) && (tm->tm_wday == -1)) { | |
56 return false; | |
57 } | |
58 *seconds_result = seconds; | |
59 return true; | |
60 } | |
61 | |
62 | 81 |
63 int64_t OS::GetCurrentTimeMillis() { | 82 int64_t OS::GetCurrentTimeMillis() { |
64 return GetCurrentTimeMicros() / 1000; | 83 return GetCurrentTimeMicros() / 1000; |
65 } | 84 } |
66 | 85 |
67 | 86 |
68 int64_t OS::GetCurrentTimeMicros() { | 87 int64_t OS::GetCurrentTimeMicros() { |
69 // gettimeofday has microsecond resolution. | 88 // gettimeofday has microsecond resolution. |
70 struct timeval tv; | 89 struct timeval tv; |
71 if (gettimeofday(&tv, NULL) < 0) { | 90 if (gettimeofday(&tv, NULL) < 0) { |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 void OS::Abort() { | 193 void OS::Abort() { |
175 abort(); | 194 abort(); |
176 } | 195 } |
177 | 196 |
178 | 197 |
179 void OS::Exit(int code) { | 198 void OS::Exit(int code) { |
180 exit(code); | 199 exit(code); |
181 } | 200 } |
182 | 201 |
183 } // namespace dart | 202 } // namespace dart |
OLD | NEW |