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