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