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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 return new Duration(seconds: offsetInSeconds); | 60 return new Duration(seconds: offsetInSeconds); |
61 } | 61 } |
62 | 62 |
63 /** The first list contains the days until each month in non-leap years. The | 63 /** The first list contains the days until each month in non-leap years. The |
64 * second list contains the days in leap years. */ | 64 * second list contains the days in leap years. */ |
65 static const List<List<int>> _DAYS_UNTIL_MONTH = | 65 static const List<List<int>> _DAYS_UNTIL_MONTH = |
66 const [const [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334], | 66 const [const [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334], |
67 const [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335]]; | 67 const [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335]]; |
68 | 68 |
69 static List _computeUpperPart(int localMs) { | 69 static List _computeUpperPart(int localMs) { |
70 final int DAYS_IN_4_YEARS = 4 * 365 + 1; | 70 const int DAYS_IN_4_YEARS = 4 * 365 + 1; |
71 final int DAYS_IN_100_YEARS = 25 * DAYS_IN_4_YEARS - 1; | 71 const int DAYS_IN_100_YEARS = 25 * DAYS_IN_4_YEARS - 1; |
72 final int DAYS_IN_400_YEARS = 4 * DAYS_IN_100_YEARS + 1; | 72 const int DAYS_IN_400_YEARS = 4 * DAYS_IN_100_YEARS + 1; |
73 final int DAYS_1970_TO_2000 = 30 * 365 + 7; | 73 const int DAYS_1970_TO_2000 = 30 * 365 + 7; |
74 final int DAYS_OFFSET = 1000 * DAYS_IN_400_YEARS + 5 * DAYS_IN_400_YEARS - | 74 const int DAYS_OFFSET = 1000 * DAYS_IN_400_YEARS + 5 * DAYS_IN_400_YEARS - |
75 DAYS_1970_TO_2000; | 75 DAYS_1970_TO_2000; |
76 final int YEARS_OFFSET = 400000; | 76 const int YEARS_OFFSET = 400000; |
77 | 77 |
78 int resultYear = 0; | 78 int resultYear = 0; |
79 int resultMonth = 0; | 79 int resultMonth = 0; |
80 int resultDay = 0; | 80 int resultDay = 0; |
81 | 81 |
82 // Always round down. | 82 // Always round down. |
83 final int daysSince1970 = _flooredDivision(localMs, | 83 final int daysSince1970 = _flooredDivision(localMs, |
84 Duration.MILLISECONDS_PER_DAY); | 84 Duration.MILLISECONDS_PER_DAY); |
85 int days = daysSince1970; | 85 int days = daysSince1970; |
86 days += DAYS_OFFSET; | 86 days += DAYS_OFFSET; |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 return 2008 + (recentYear - 2008) % 28; | 288 return 2008 + (recentYear - 2008) % 28; |
289 } | 289 } |
290 | 290 |
291 /** | 291 /** |
292 * Returns the UTC year for the corresponding [secondsSinceEpoch]. | 292 * Returns the UTC year for the corresponding [secondsSinceEpoch]. |
293 * It is relatively fast for values in the range 0 to year 2098. | 293 * It is relatively fast for values in the range 0 to year 2098. |
294 * | 294 * |
295 * Code is adapted from V8. | 295 * Code is adapted from V8. |
296 */ | 296 */ |
297 static int _yearsFromSecondsSinceEpoch(int secondsSinceEpoch) { | 297 static int _yearsFromSecondsSinceEpoch(int secondsSinceEpoch) { |
298 final int DAYS_IN_4_YEARS = 4 * 365 + 1; | 298 const int DAYS_IN_4_YEARS = 4 * 365 + 1; |
299 final int DAYS_IN_100_YEARS = 25 * DAYS_IN_4_YEARS - 1; | 299 const int DAYS_IN_100_YEARS = 25 * DAYS_IN_4_YEARS - 1; |
300 final int DAYS_YEAR_2098 = DAYS_IN_100_YEARS + 6 * DAYS_IN_4_YEARS; | 300 const int DAYS_YEAR_2098 = DAYS_IN_100_YEARS + 6 * DAYS_IN_4_YEARS; |
301 | 301 |
302 int days = secondsSinceEpoch ~/ Duration.SECONDS_PER_DAY; | 302 int days = secondsSinceEpoch ~/ Duration.SECONDS_PER_DAY; |
303 if (days > 0 && days < DAYS_YEAR_2098) { | 303 if (days > 0 && days < DAYS_YEAR_2098) { |
304 // According to V8 this fast case works for dates from 1970 to 2099. | 304 // According to V8 this fast case works for dates from 1970 to 2099. |
305 return 1970 + (4 * days + 2) ~/ DAYS_IN_4_YEARS; | 305 return 1970 + (4 * days + 2) ~/ DAYS_IN_4_YEARS; |
306 } | 306 } |
307 int ms = secondsSinceEpoch * Duration.MILLISECONDS_PER_SECOND; | 307 int ms = secondsSinceEpoch * Duration.MILLISECONDS_PER_SECOND; |
308 return _computeUpperPart(ms)[_YEAR_INDEX]; | 308 return _computeUpperPart(ms)[_YEAR_INDEX]; |
309 } | 309 } |
310 | 310 |
311 /** | 311 /** |
312 * Returns a date in seconds that is equivalent to the current date. An | 312 * Returns a date in seconds that is equivalent to the current date. An |
313 * equivalent date has the same fields ([:month:], [:day:], etc.) as the | 313 * equivalent date has the same fields ([:month:], [:day:], etc.) as the |
314 * [this], but the [:year:] is in the range [1970..2037]. | 314 * [this], but the [:year:] is in the range [1970..2037]. |
315 * | 315 * |
316 * * The time since the beginning of the year is the same. | 316 * * The time since the beginning of the year is the same. |
317 * * If [this] is in a leap year then the returned seconds are in a leap | 317 * * If [this] is in a leap year then the returned seconds are in a leap |
318 * year, too. | 318 * year, too. |
319 * * The week day of [this] is the same as the one for the returned date. | 319 * * The week day of [this] is the same as the one for the returned date. |
320 */ | 320 */ |
321 static int _equivalentSeconds(int millisecondsSinceEpoch) { | 321 static int _equivalentSeconds(int millisecondsSinceEpoch) { |
322 final int CUT_OFF_SECONDS = 2100000000; | 322 const int CUT_OFF_SECONDS = 2100000000; |
323 | 323 |
324 int secondsSinceEpoch = _flooredDivision(millisecondsSinceEpoch, | 324 int secondsSinceEpoch = _flooredDivision(millisecondsSinceEpoch, |
325 Duration.MILLISECONDS_PER_SECOND); | 325 Duration.MILLISECONDS_PER_SECOND); |
326 | 326 |
327 if (secondsSinceEpoch < 0 || secondsSinceEpoch >= CUT_OFF_SECONDS) { | 327 if (secondsSinceEpoch < 0 || secondsSinceEpoch >= CUT_OFF_SECONDS) { |
328 int year = _yearsFromSecondsSinceEpoch(secondsSinceEpoch); | 328 int year = _yearsFromSecondsSinceEpoch(secondsSinceEpoch); |
329 int days = _dayFromYear(year); | 329 int days = _dayFromYear(year); |
330 int equivalentYear = _equivalentYear(year); | 330 int equivalentYear = _equivalentYear(year); |
331 int equivalentDays = _dayFromYear(equivalentYear); | 331 int equivalentDays = _dayFromYear(equivalentYear); |
332 int diffDays = equivalentDays - days; | 332 int diffDays = equivalentDays - days; |
333 secondsSinceEpoch += diffDays * Duration.SECONDS_PER_DAY; | 333 secondsSinceEpoch += diffDays * Duration.SECONDS_PER_DAY; |
334 } | 334 } |
335 return secondsSinceEpoch; | 335 return secondsSinceEpoch; |
336 } | 336 } |
337 | 337 |
338 static int _timeZoneOffsetInSeconds(int millisecondsSinceEpoch) { | 338 static int _timeZoneOffsetInSeconds(int millisecondsSinceEpoch) { |
339 int equivalentSeconds = _equivalentSeconds(millisecondsSinceEpoch); | 339 int equivalentSeconds = _equivalentSeconds(millisecondsSinceEpoch); |
340 return _timeZoneOffsetInSecondsForClampedSeconds(equivalentSeconds); | 340 return _timeZoneOffsetInSecondsForClampedSeconds(equivalentSeconds); |
341 } | 341 } |
342 | 342 |
343 static String _timeZoneName(int millisecondsSinceEpoch) { | 343 static String _timeZoneName(int millisecondsSinceEpoch) { |
344 int equivalentSeconds = _equivalentSeconds(millisecondsSinceEpoch); | 344 int equivalentSeconds = _equivalentSeconds(millisecondsSinceEpoch); |
345 return _timeZoneNameForClampedSeconds(equivalentSeconds); | 345 return _timeZoneNameForClampedSeconds(equivalentSeconds); |
346 } | 346 } |
347 } | 347 } |
OLD | NEW |