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 // Dart core library. | 4 // Dart core library. |
5 | 5 |
6 // VM implementation of DateTime. | 6 // VM implementation of DateTime. |
7 patch class DateTime { | 7 patch class DateTime { |
8 // Natives. | 8 // Natives. |
9 // The natives have been moved up here to work around Issue 10401. | 9 // The natives have been moved up here to work around Issue 10401. |
10 static int _getCurrentMs() native "DateNatives_currentTimeMillis"; | 10 static int _getCurrentMs() native "DateNatives_currentTimeMillis"; |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
183 + _flooredDivision(year - 1969, 4) | 183 + _flooredDivision(year - 1969, 4) |
184 - _flooredDivision(year - 1901, 100) | 184 - _flooredDivision(year - 1901, 100) |
185 + _flooredDivision(year - 1601, 400); | 185 + _flooredDivision(year - 1601, 400); |
186 } | 186 } |
187 | 187 |
188 static bool _isLeapYear(y) { | 188 static bool _isLeapYear(y) { |
189 return (y.remainder(4) == 0) && | 189 return (y.remainder(4) == 0) && |
190 ((y.remainder(100) != 0) || (y.remainder(400) == 0)); | 190 ((y.remainder(100) != 0) || (y.remainder(400) == 0)); |
191 } | 191 } |
192 | 192 |
193 static _brokenDownDateToMillisecondsSinceEpoch( | 193 /* patch */ static int _brokenDownDateToMillisecondsSinceEpoch( |
194 int year, int month, int day, | 194 int year, int month, int day, |
195 int hour, int minute, int second, int millisecond, | 195 int hour, int minute, int second, int millisecond, |
196 bool isUtc) { | 196 bool isUtc) { |
197 // Simplify calculations by working with zero-based month. | 197 // Simplify calculations by working with zero-based month. |
198 --month; | 198 --month; |
199 // Deal with under and overflow. | 199 // Deal with under and overflow. |
200 year += (month / 12).floor(); | 200 year += (month / 12).floor(); |
201 month = month % 12; | 201 month = month % 12; |
202 | 202 |
203 // First compute the seconds in UTC, independent of the [isUtc] flag. If | 203 // First compute the seconds in UTC, independent of the [isUtc] flag. If |
204 // necessary we will add the time-zone offset later on. | 204 // necessary we will add the time-zone offset later on. |
205 int days = day - 1; | 205 int days = day - 1; |
206 days += _DAYS_UNTIL_MONTH[_isLeapYear(year) ? 1 : 0][month]; | 206 days += _DAYS_UNTIL_MONTH[_isLeapYear(year) ? 1 : 0][month]; |
207 days += _dayFromYear(year); | 207 days += _dayFromYear(year); |
208 int millisecondsSinceEpoch = days * Duration.MILLISECONDS_PER_DAY + | 208 int millisecondsSinceEpoch = days * Duration.MILLISECONDS_PER_DAY + |
209 hour * Duration.MILLISECONDS_PER_HOUR + | 209 hour * Duration.MILLISECONDS_PER_HOUR + |
210 minute * Duration.MILLISECONDS_PER_MINUTE+ | 210 minute * Duration.MILLISECONDS_PER_MINUTE+ |
211 second * Duration.MILLISECONDS_PER_SECOND + | 211 second * Duration.MILLISECONDS_PER_SECOND + |
212 millisecond; | 212 millisecond; |
213 | 213 |
214 // Since [_timeZoneOffsetInSeconds] will crash if the input is far out of | 214 // Since [_timeZoneOffsetInSeconds] will crash if the input is far out of |
215 // the valid range we do a preliminary test that weeds out values that can | 215 // the valid range we do a preliminary test that weeds out values that can |
216 // not become valid even with timezone adjustments. | 216 // not become valid even with timezone adjustments. |
217 // The timezone adjustment is always less than a day, so adding a security | 217 // The timezone adjustment is always less than a day, so adding a security |
218 // margin of one day should be enough. | 218 // margin of one day should be enough. |
219 if (millisecondsSinceEpoch.abs() > | 219 if (millisecondsSinceEpoch.abs() > |
220 (_MAX_MILLISECONDS_SINCE_EPOCH + Duration.MILLISECONDS_PER_DAY)) { | 220 (_MAX_MILLISECONDS_SINCE_EPOCH + Duration.MILLISECONDS_PER_DAY)) { |
221 return null; | 221 return null; |
siva
2013/08/12 02:56:43
should this now return 0 or -1 and not null?
regis
2013/08/12 17:45:27
null is actually a valid value to return to indica
| |
222 } | 222 } |
223 | 223 |
224 if (!isUtc) { | 224 if (!isUtc) { |
225 // Note that we need to remove the local timezone adjustement before | 225 // Note that we need to remove the local timezone adjustement before |
226 // asking for the correct zone offset. | 226 // asking for the correct zone offset. |
227 int adjustment = _localTimeZoneAdjustmentInSeconds() * | 227 int adjustment = _localTimeZoneAdjustmentInSeconds() * |
228 Duration.MILLISECONDS_PER_SECOND; | 228 Duration.MILLISECONDS_PER_SECOND; |
229 int zoneOffset = | 229 int zoneOffset = |
230 _timeZoneOffsetInSeconds(millisecondsSinceEpoch - adjustment); | 230 _timeZoneOffsetInSeconds(millisecondsSinceEpoch - adjustment); |
231 millisecondsSinceEpoch -= zoneOffset * Duration.MILLISECONDS_PER_SECOND; | 231 millisecondsSinceEpoch -= zoneOffset * Duration.MILLISECONDS_PER_SECOND; |
232 } | 232 } |
233 if (millisecondsSinceEpoch.abs() > _MAX_MILLISECONDS_SINCE_EPOCH) { | 233 if (millisecondsSinceEpoch.abs() > _MAX_MILLISECONDS_SINCE_EPOCH) { |
234 return null; | 234 return null; |
siva
2013/08/12 02:56:43
ditto comment here.
regis
2013/08/12 17:45:27
ditto
| |
235 } | 235 } |
236 return millisecondsSinceEpoch; | 236 return millisecondsSinceEpoch; |
237 } | 237 } |
238 | 238 |
239 static int _weekDay(y) { | 239 static int _weekDay(y) { |
240 // 1/1/1970 was a Thursday. | 240 // 1/1/1970 was a Thursday. |
241 return (_dayFromYear(y) + 4) % 7; | 241 return (_dayFromYear(y) + 4) % 7; |
242 } | 242 } |
243 | 243 |
244 /** | 244 /** |
245 * Returns a year in the range 2008-2035 matching | 245 * Returns a year in the range 2008-2035 matching |
246 * * leap year, and | 246 * * leap year, and |
247 * * week day of first day. | 247 * * week day of first day. |
248 * | 248 * |
249 * Leap seconds are ignored. | 249 * Leap seconds are ignored. |
250 * Adapted from V8's date implementation. See ECMA 262 - 15.9.1.9. | 250 * Adapted from V8's date implementation. See ECMA 262 - 15.9.1.9. |
251 */ | 251 */ |
252 static _equivalentYear(int year) { | 252 static int _equivalentYear(int year) { |
253 // Returns the week day (in range 0 - 6). | 253 // Returns the week day (in range 0 - 6). |
254 // 1/1/1956 was a Sunday (i.e. weekday 0). 1956 was a leap-year. | 254 // 1/1/1956 was a Sunday (i.e. weekday 0). 1956 was a leap-year. |
255 // 1/1/1967 was a Sunday (i.e. weekday 0). | 255 // 1/1/1967 was a Sunday (i.e. weekday 0). |
256 // Without leap years a subsequent year has a week day + 1 (for example | 256 // Without leap years a subsequent year has a week day + 1 (for example |
257 // 1/1/1968 was a Monday). With leap-years it jumps over one week day | 257 // 1/1/1968 was a Monday). With leap-years it jumps over one week day |
258 // (e.g. 1/1/1957 was a Tuesday). | 258 // (e.g. 1/1/1957 was a Tuesday). |
259 // After 12 years the weekdays have advanced by 12 days + 3 leap days = | 259 // After 12 years the weekdays have advanced by 12 days + 3 leap days = |
260 // 15 days. 15 % 7 = 1. So after 12 years the week day has always | 260 // 15 days. 15 % 7 = 1. So after 12 years the week day has always |
261 // (now independently of leap-years) advanced by one. | 261 // (now independently of leap-years) advanced by one. |
262 // weekDay * 12 gives thus a year starting with the wanted weekDay. | 262 // weekDay * 12 gives thus a year starting with the wanted weekDay. |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
317 static int _timeZoneOffsetInSeconds(int millisecondsSinceEpoch) { | 317 static int _timeZoneOffsetInSeconds(int millisecondsSinceEpoch) { |
318 int equivalentSeconds = _equivalentSeconds(millisecondsSinceEpoch); | 318 int equivalentSeconds = _equivalentSeconds(millisecondsSinceEpoch); |
319 return _timeZoneOffsetInSecondsForClampedSeconds(equivalentSeconds); | 319 return _timeZoneOffsetInSecondsForClampedSeconds(equivalentSeconds); |
320 } | 320 } |
321 | 321 |
322 static String _timeZoneName(int millisecondsSinceEpoch) { | 322 static String _timeZoneName(int millisecondsSinceEpoch) { |
323 int equivalentSeconds = _equivalentSeconds(millisecondsSinceEpoch); | 323 int equivalentSeconds = _equivalentSeconds(millisecondsSinceEpoch); |
324 return _timeZoneNameForClampedSeconds(equivalentSeconds); | 324 return _timeZoneNameForClampedSeconds(equivalentSeconds); |
325 } | 325 } |
326 } | 326 } |
OLD | NEW |