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 <time.h> | 5 #include <time.h> |
6 | 6 |
7 #include "vm/bootstrap_natives.h" | 7 #include "vm/bootstrap_natives.h" |
8 | 8 |
9 #include "vm/bigint_operations.h" | 9 #include "vm/bigint_operations.h" |
10 #include "vm/native_entry.h" | 10 #include "vm/native_entry.h" |
11 #include "vm/object.h" | 11 #include "vm/object.h" |
12 #include "vm/os.h" | 12 #include "vm/os.h" |
13 | 13 |
14 namespace dart { | 14 namespace dart { |
15 | 15 |
16 typedef struct BrokenDownDate { | 16 static int32_t kMaxAllowedSeconds = 2100000000; |
17 intptr_t year; | |
18 intptr_t month; // [1..12] | |
19 intptr_t day; // [1..31] | |
20 intptr_t hours; | |
21 intptr_t minutes; | |
22 intptr_t seconds; | |
23 } BrokenDownDate; | |
24 | |
25 | |
26 // Takes the seconds since epoch (midnight, January 1, 1970 UTC) and breaks it | |
27 // down into date and time. | |
28 // If 'dart_is_utc', then the broken down date and time are in the UTC timezone, | |
29 // otherwise the local timezone is used. | |
30 // The returned year is offset by 1900. The returned month is 0-based. | |
31 // Returns true if the conversion succeeds, false otherwise. | |
32 static bool BreakDownSecondsSinceEpoch(const Integer& dart_seconds, | |
33 const Bool& dart_is_utc, | |
34 BrokenDownDate* result) { | |
35 // Always fill the result to avoid unitialized use warnings. | |
36 result->year = 0; | |
37 result->month = 0; | |
38 result->day = 0; | |
39 result->hours = 0; | |
40 result->minutes = 0; | |
41 result->seconds = 0; | |
42 | |
43 bool is_utc = dart_is_utc.value(); | |
44 int64_t seconds = dart_seconds.AsInt64Value(); | |
45 | |
46 struct tm tm_result; | |
47 bool succeeded; | |
48 if (is_utc) { | |
49 succeeded = OS::GmTime(seconds, &tm_result); | |
50 } else { | |
51 succeeded = OS::LocalTime(seconds, &tm_result); | |
52 } | |
53 if (succeeded) { | |
54 result->year = tm_result.tm_year; | |
55 // C uses years since 1900, and not full years. | |
56 // Adding 1900 could overflow the intptr_t. | |
57 if (result->year > kIntptrMax - 1900) return false; | |
58 result->year += 1900; | |
59 // Dart has 1-based months (contrary to C's 0-based). | |
60 result->month= tm_result.tm_mon + 1; | |
61 result->day = tm_result.tm_mday; | |
62 result->hours = tm_result.tm_hour; | |
63 result->minutes = tm_result.tm_min; | |
64 result->seconds = tm_result.tm_sec; | |
65 } | |
66 return succeeded; | |
67 } | |
68 | |
69 | |
70 static bool BrokenDownToSecondsSinceEpoch(const BrokenDownDate& broken_down, | |
71 bool in_utc, | |
72 int64_t* result) { | |
73 // Always set the result to avoid unitialized use warnings. | |
74 *result = 0; | |
75 | |
76 struct tm tm_broken_down; | |
77 intptr_t year = broken_down.year; | |
78 // C works with years since 1900. | |
79 // Removing 1900 could underflow the intptr_t. | |
80 if (year < kIntptrMin + 1900) return false; | |
81 year -= 1900; | |
82 intptr_t month = broken_down.month; | |
83 // C works with 0-based months. | |
84 // Avoid underflows (even though they should not matter since the date would | |
85 // be invalid anyways. | |
86 if (month < 0) return false; | |
87 month--; | |
88 tm_broken_down.tm_year = static_cast<int>(year); | |
89 tm_broken_down.tm_mon = static_cast<int>(month); | |
90 tm_broken_down.tm_mday = static_cast<int>(broken_down.day); | |
91 tm_broken_down.tm_hour = static_cast<int>(broken_down.hours); | |
92 tm_broken_down.tm_min = static_cast<int>(broken_down.minutes); | |
93 tm_broken_down.tm_sec = static_cast<int>(broken_down.seconds); | |
94 // Verify that casting to int did not change the value. | |
95 if (tm_broken_down.tm_year != year | |
96 || tm_broken_down.tm_mon != month | |
97 || tm_broken_down.tm_mday != broken_down.day | |
98 || tm_broken_down.tm_hour != broken_down.hours | |
99 || tm_broken_down.tm_min != broken_down.minutes | |
100 || tm_broken_down.tm_sec != broken_down.seconds) { | |
101 return false; | |
102 } | |
103 if (in_utc) { | |
104 return OS::MkGmTime(&tm_broken_down, result); | |
105 } else { | |
106 return OS::MkTime(&tm_broken_down, result); | |
107 } | |
108 } | |
109 | |
110 | |
111 DEFINE_NATIVE_ENTRY(DateNatives_brokenDownToSecondsSinceEpoch, 7) { | |
112 GET_NATIVE_ARGUMENT(Integer, dart_years, arguments->At(0)); | |
113 GET_NATIVE_ARGUMENT(Smi, dart_month, arguments->At(1)); | |
114 GET_NATIVE_ARGUMENT(Smi, dart_day, arguments->At(2)); | |
115 GET_NATIVE_ARGUMENT(Smi, dart_hours, arguments->At(3)); | |
116 GET_NATIVE_ARGUMENT(Smi, dart_minutes, arguments->At(4)); | |
117 GET_NATIVE_ARGUMENT(Smi, dart_seconds, arguments->At(5)); | |
118 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(6)); | |
119 if (!dart_years.IsSmi()) { | |
120 UNIMPLEMENTED(); | |
121 } | |
122 Smi& smi_years = Smi::Handle(); | |
123 smi_years ^= dart_years.raw(); | |
124 BrokenDownDate broken_down; | |
125 broken_down.year = smi_years.Value(); | |
126 broken_down.month = dart_month.Value(); | |
127 broken_down.day = dart_day.Value(); | |
128 broken_down.hours = dart_hours.Value(); | |
129 broken_down.minutes = dart_minutes.Value(); | |
130 broken_down.seconds = dart_seconds.Value(); | |
131 int64_t value; | |
132 bool succeeded = BrokenDownToSecondsSinceEpoch(broken_down, | |
133 dart_is_utc.value(), | |
134 &value); | |
135 if (!succeeded) { | |
136 UNIMPLEMENTED(); | |
137 } | |
138 arguments->SetReturn(Integer::Handle(Integer::New(value))); | |
139 } | |
140 | |
141 | 17 |
142 DEFINE_NATIVE_ENTRY(DateNatives_timeZoneName, 1) { | 18 DEFINE_NATIVE_ENTRY(DateNatives_timeZoneName, 1) { |
143 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0)); | 19 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0)); |
144 int64_t seconds = dart_seconds.AsInt64Value(); | 20 int64_t seconds = dart_seconds.AsInt64Value(); |
145 const char* name; | 21 if (seconds < 0 || seconds > kMaxAllowedSeconds) { |
146 bool succeeded = OS::GetTimeZoneName(seconds, &name); | 22 GrowableArray<const Object*> args; |
147 if (!succeeded) { | 23 args.Add(&dart_seconds); |
148 UNIMPLEMENTED(); | 24 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); |
149 } | 25 } |
| 26 const char* name = OS::GetTimeZoneName(seconds); |
150 const String& dart_name = String::Handle(String::New(name)); | 27 const String& dart_name = String::Handle(String::New(name)); |
151 arguments->SetReturn(dart_name); | 28 arguments->SetReturn(dart_name); |
152 } | 29 } |
153 | 30 |
154 | 31 |
155 DEFINE_NATIVE_ENTRY(DateNatives_timeZoneOffsetInSeconds, 1) { | 32 DEFINE_NATIVE_ENTRY(DateNatives_timeZoneOffsetInSeconds, 1) { |
156 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0)); | 33 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0)); |
157 int64_t seconds = dart_seconds.AsInt64Value(); | 34 int64_t seconds = dart_seconds.AsInt64Value(); |
158 int offset; | 35 if (seconds < 0 || seconds > kMaxAllowedSeconds) { |
159 bool succeeded = OS::GetTimeZoneOffsetInSeconds(seconds, &offset); | 36 GrowableArray<const Object*> args; |
160 if (!succeeded) { | 37 args.Add(&dart_seconds); |
161 UNIMPLEMENTED(); | 38 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); |
162 } | 39 } |
| 40 int offset = OS::GetTimeZoneOffsetInSeconds(seconds); |
163 const Integer& dart_offset = Integer::Handle(Integer::New(offset)); | 41 const Integer& dart_offset = Integer::Handle(Integer::New(offset)); |
164 arguments->SetReturn(dart_offset); | 42 arguments->SetReturn(dart_offset); |
165 } | 43 } |
166 | 44 |
167 | 45 |
| 46 DEFINE_NATIVE_ENTRY(DateNatives_localTimeZoneAdjustmentInSeconds, 0) { |
| 47 int adjustment = OS::GetLocalTimeZoneAdjustmentInSeconds(); |
| 48 const Integer& dart_adjustment = Integer::Handle(Integer::New(adjustment)); |
| 49 arguments->SetReturn(dart_adjustment); |
| 50 } |
| 51 |
| 52 |
168 DEFINE_NATIVE_ENTRY(DateNatives_currentTimeMillis, 0) { | 53 DEFINE_NATIVE_ENTRY(DateNatives_currentTimeMillis, 0) { |
169 const Integer& time = Integer::Handle( | 54 const Integer& time = Integer::Handle( |
170 Integer::New(OS::GetCurrentTimeMillis())); | 55 Integer::New(OS::GetCurrentTimeMillis())); |
171 arguments->SetReturn(time); | 56 arguments->SetReturn(time); |
172 } | 57 } |
173 | 58 |
174 | |
175 DEFINE_NATIVE_ENTRY(DateNatives_getYear, 2) { | |
176 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0)); | |
177 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(1)); | |
178 BrokenDownDate broken_down; | |
179 bool succeeded = | |
180 BreakDownSecondsSinceEpoch(dart_seconds, dart_is_utc, &broken_down); | |
181 if (!succeeded) { | |
182 UNIMPLEMENTED(); | |
183 } | |
184 intptr_t year = broken_down.year; | |
185 arguments->SetReturn(Integer::Handle(Integer::New(year))); | |
186 } | |
187 | |
188 | |
189 DEFINE_NATIVE_ENTRY(DateNatives_getMonth, 2) { | |
190 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0)); | |
191 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(1)); | |
192 BrokenDownDate broken_down; | |
193 bool succeeded = | |
194 BreakDownSecondsSinceEpoch(dart_seconds, dart_is_utc, &broken_down); | |
195 if (!succeeded) { | |
196 UNIMPLEMENTED(); | |
197 } | |
198 const Smi& result = Smi::Handle(Smi::New(broken_down.month)); | |
199 arguments->SetReturn(result); | |
200 } | |
201 | |
202 | |
203 DEFINE_NATIVE_ENTRY(DateNatives_getDay, 2) { | |
204 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0)); | |
205 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(1)); | |
206 BrokenDownDate broken_down; | |
207 bool succeeded = | |
208 BreakDownSecondsSinceEpoch(dart_seconds, dart_is_utc, &broken_down); | |
209 if (!succeeded) { | |
210 UNIMPLEMENTED(); | |
211 } | |
212 const Smi& result = Smi::Handle(Smi::New(broken_down.day)); | |
213 arguments->SetReturn(result); | |
214 } | |
215 | |
216 | |
217 DEFINE_NATIVE_ENTRY(DateNatives_getHours, 2) { | |
218 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0)); | |
219 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(1)); | |
220 BrokenDownDate broken_down; | |
221 bool succeeded = | |
222 BreakDownSecondsSinceEpoch(dart_seconds, dart_is_utc, &broken_down); | |
223 if (!succeeded) { | |
224 UNIMPLEMENTED(); | |
225 } | |
226 const Smi& result = Smi::Handle(Smi::New(broken_down.hours)); | |
227 arguments->SetReturn(result); | |
228 } | |
229 | |
230 | |
231 DEFINE_NATIVE_ENTRY(DateNatives_getMinutes, 2) { | |
232 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0)); | |
233 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(1)); | |
234 BrokenDownDate broken_down; | |
235 bool succeeded = | |
236 BreakDownSecondsSinceEpoch(dart_seconds, dart_is_utc, &broken_down); | |
237 if (!succeeded) { | |
238 UNIMPLEMENTED(); | |
239 } | |
240 const Smi& result = Smi::Handle(Smi::New(broken_down.minutes)); | |
241 arguments->SetReturn(result); | |
242 } | |
243 | |
244 | |
245 DEFINE_NATIVE_ENTRY(DateNatives_getSeconds, 2) { | |
246 GET_NATIVE_ARGUMENT(Integer, dart_seconds, arguments->At(0)); | |
247 GET_NATIVE_ARGUMENT(Bool, dart_is_utc, arguments->At(1)); | |
248 BrokenDownDate broken_down; | |
249 bool succeeded = | |
250 BreakDownSecondsSinceEpoch(dart_seconds, dart_is_utc, &broken_down); | |
251 if (!succeeded) { | |
252 UNIMPLEMENTED(); | |
253 } | |
254 const Smi& result = Smi::Handle(Smi::New(broken_down.seconds)); | |
255 arguments->SetReturn(result); | |
256 } | |
257 | |
258 } // namespace dart | 59 } // namespace dart |
OLD | NEW |