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