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 #include "vm/thread.h" | |
Ivan Posva
2012/04/11 12:01:24
ditto
floitsch
2012/04/11 12:10:21
Done.
| |
10 | 11 |
11 namespace dart { | 12 namespace dart { |
12 | 13 |
13 bool OS::BreakDownSecondsSinceEpoch(time_t seconds_since_epoch, | 14 bool OS::BreakDownSecondsSinceEpoch(time_t seconds_since_epoch, |
14 bool in_utc, | 15 bool in_utc, |
15 BrokenDownDate* result) { | 16 BrokenDownDate* result) { |
16 struct tm tm_result; | 17 struct tm tm_result; |
17 errno_t error_code; | 18 errno_t error_code; |
18 if (in_utc) { | 19 if (in_utc) { |
19 error_code = gmtime_s(&tm_result, &seconds_since_epoch); | 20 error_code = gmtime_s(&tm_result, &seconds_since_epoch); |
20 } else { | 21 } 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); | 22 error_code = localtime_s(&tm_result, &seconds_since_epoch); |
25 } | 23 } |
26 result->year = tm_result.tm_year; | 24 result->year = tm_result.tm_year; |
27 result->month= tm_result.tm_mon; | 25 result->month= tm_result.tm_mon; |
28 result->day = tm_result.tm_mday; | 26 result->day = tm_result.tm_mday; |
29 result->hours = tm_result.tm_hour; | 27 result->hours = tm_result.tm_hour; |
30 result->minutes = tm_result.tm_min; | 28 result->minutes = tm_result.tm_min; |
31 result->seconds = tm_result.tm_sec; | 29 result->seconds = tm_result.tm_sec; |
32 return error_code == 0; | 30 return error_code == 0; |
33 } | 31 } |
34 | 32 |
35 | 33 |
36 bool OS::BrokenDownToSecondsSinceEpoch( | 34 bool OS::BrokenDownToSecondsSinceEpoch( |
37 const BrokenDownDate& broken_down, bool in_utc, time_t* result) { | 35 const BrokenDownDate& broken_down, bool in_utc, time_t* result) { |
38 struct tm tm_broken_down; | 36 struct tm tm_broken_down; |
39 // mktime takes the years since 1900. | 37 // mktime takes the years since 1900. |
40 tm_broken_down.tm_year = broken_down.year; | 38 tm_broken_down.tm_year = broken_down.year; |
41 tm_broken_down.tm_mon = broken_down.month; | 39 tm_broken_down.tm_mon = broken_down.month; |
42 tm_broken_down.tm_mday = broken_down.day; | 40 tm_broken_down.tm_mday = broken_down.day; |
43 tm_broken_down.tm_hour = broken_down.hours; | 41 tm_broken_down.tm_hour = broken_down.hours; |
44 tm_broken_down.tm_min = broken_down.minutes; | 42 tm_broken_down.tm_min = broken_down.minutes; |
45 tm_broken_down.tm_sec = broken_down.seconds; | 43 tm_broken_down.tm_sec = broken_down.seconds; |
46 // Set wday to an impossible day, so that we can catch bad input. | 44 // Set wday to an impossible day, so that we can catch bad input. |
47 tm_broken_down.tm_wday = -1; | 45 tm_broken_down.tm_wday = -1; |
48 // Make sure the libc knows about the local zone. | 46 // Make sure the libc knows about the local zone. |
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 | 47 // TODO(floitsch): we should be able to call tzset only once during |
53 // initialization. | 48 // initialization. |
54 tzset(); | 49 tzset(); |
55 if (in_utc) { | 50 if (in_utc) { |
56 // Disable daylight saving in utc mode. | 51 // Disable daylight saving in utc mode. |
57 tm_broken_down.tm_isdst = 0; | 52 tm_broken_down.tm_isdst = 0; |
58 // mktime assumes that the given date is local time zone. | 53 *result = _mkgmtime(&tm_broken_down); |
59 *result = mktime(&tm_broken_down); | |
60 // Remove the timezone. | |
61 *result -= timezone; | |
62 } else { | 54 } else { |
63 // Let libc figure out if daylight saving is active. | 55 // Let libc figure out if daylight saving is active. |
64 tm_broken_down.tm_isdst = -1; | 56 tm_broken_down.tm_isdst = -1; |
65 *result = mktime(&tm_broken_down); | 57 *result = mktime(&tm_broken_down); |
66 } | 58 } |
67 if ((*result == -1) && (tm_broken_down.tm_wday == -1)) { | 59 if ((*result == -1 || *result == 1) && (tm_broken_down.tm_wday == -1)) { |
68 return false; | 60 return false; |
69 } | 61 } |
70 return true; | 62 return true; |
71 } | 63 } |
72 | 64 |
73 | 65 |
74 int64_t OS::GetCurrentTimeMillis() { | 66 int64_t OS::GetCurrentTimeMillis() { |
75 return GetCurrentTimeMicros() / 1000; | 67 return GetCurrentTimeMicros() / 1000; |
76 } | 68 } |
77 | 69 |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
200 void OS::Abort() { | 192 void OS::Abort() { |
201 abort(); | 193 abort(); |
202 } | 194 } |
203 | 195 |
204 | 196 |
205 void OS::Exit(int code) { | 197 void OS::Exit(int code) { |
206 exit(code); | 198 exit(code); |
207 } | 199 } |
208 | 200 |
209 } // namespace dart | 201 } // namespace dart |
OLD | NEW |