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 DateImplementation. | 6 // VM implementation of DateImplementation. |
7 patch class DateImplementation { | 7 patch class DateImplementation { |
8 /* patch */ DateImplementation(int years, | 8 /* patch */ DateImplementation(int year, |
9 [int month = 1, | 9 [int month = 1, |
10 int day = 1, | 10 int day = 1, |
11 int hour = 0, | 11 int hour = 0, |
12 int minute = 0, | 12 int minute = 0, |
13 int second = 0, | 13 int second = 0, |
14 int millisecond = 0, | 14 int millisecond = 0, |
15 bool isUtc = false]) | 15 bool isUtc = false]) |
16 : this.isUtc = isUtc, | 16 : this.isUtc = isUtc, |
17 this.millisecondsSinceEpoch = _brokenDownDateToMillisecondsSinceEpoch( | 17 this.millisecondsSinceEpoch = _brokenDownDateToMillisecondsSinceEpoch( |
18 years, month, day, hour, minute, second, millisecond, isUtc) { | 18 year, month, day, hour, minute, second, millisecond, isUtc) { |
19 if (millisecondsSinceEpoch === null) throw new IllegalArgumentException(); | 19 if (millisecondsSinceEpoch === null) throw new IllegalArgumentException(); |
20 if (isUtc === null) throw new IllegalArgumentException(); | 20 if (isUtc === null) throw new IllegalArgumentException(); |
21 } | 21 } |
22 | 22 |
23 /* patch */ DateImplementation.now() | 23 /* patch */ DateImplementation.now() |
24 : isUtc = false, | 24 : isUtc = false, |
25 millisecondsSinceEpoch = _getCurrentMs() { | 25 millisecondsSinceEpoch = _getCurrentMs() { |
26 } | 26 } |
27 | 27 |
28 /* patch */ String get timeZoneName() { | 28 /* patch */ String get timeZoneName() { |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 - _flooredDivision(year - 1901, 100) | 170 - _flooredDivision(year - 1901, 100) |
171 + _flooredDivision(year - 1601, 400); | 171 + _flooredDivision(year - 1601, 400); |
172 } | 172 } |
173 | 173 |
174 static bool _isLeapYear(y) { | 174 static bool _isLeapYear(y) { |
175 return (y.remainder(4) == 0) && | 175 return (y.remainder(4) == 0) && |
176 ((y.remainder(100) != 0) || (y.remainder(400) == 0)); | 176 ((y.remainder(100) != 0) || (y.remainder(400) == 0)); |
177 } | 177 } |
178 | 178 |
179 static _brokenDownDateToMillisecondsSinceEpoch( | 179 static _brokenDownDateToMillisecondsSinceEpoch( |
180 int years, int month, int day, | 180 int year, int month, int day, |
181 int hour, int minute, int second, int millisecond, | 181 int hour, int minute, int second, int millisecond, |
182 bool isUtc) { | 182 bool isUtc) { |
183 if ((month < 1) || (month > 12)) return null; | 183 // Simplify calculations by working with zero-based month. |
184 if ((day < 1) || (day > 31)) return null; | 184 --month; |
185 // Leap seconds can lead to hour == 24. | 185 // Deal with under and overflow. |
186 if ((hour < 0) || (hour > 24)) return null; | 186 year += (month / 12).floor().toInt(); |
187 if ((hour == 24) && ((minute != 0) || (second != 0))) return null; | 187 month = month % 12; |
188 if ((minute < 0) || (minute > 59)) return null; | |
189 if ((second < 0) || (second > 59)) return null; | |
190 if ((millisecond < 0) || (millisecond > 999)) return null; | |
191 | 188 |
192 // First compute the seconds in UTC, independent of the [isUtc] flag. If | 189 // First compute the seconds in UTC, independent of the [isUtc] flag. If |
193 // necessary we will add the time-zone offset later on. | 190 // necessary we will add the time-zone offset later on. |
194 int days = day - 1; | 191 int days = day - 1; |
195 days += _DAYS_UNTIL_MONTH[_isLeapYear(years) ? 1 : 0][month - 1]; | 192 days += _DAYS_UNTIL_MONTH[_isLeapYear(year) ? 1 : 0][month]; |
196 days += _dayFromYear(years); | 193 days += _dayFromYear(year); |
197 int millisecondsSinceEpoch = days * Duration.MILLISECONDS_PER_DAY + | 194 int millisecondsSinceEpoch = days * Duration.MILLISECONDS_PER_DAY + |
198 hour * Duration.MILLISECONDS_PER_HOUR + | 195 hour * Duration.MILLISECONDS_PER_HOUR + |
199 minute * Duration.MILLISECONDS_PER_MINUTE+ | 196 minute * Duration.MILLISECONDS_PER_MINUTE+ |
200 second * Duration.MILLISECONDS_PER_SECOND + | 197 second * Duration.MILLISECONDS_PER_SECOND + |
201 millisecond; | 198 millisecond; |
202 | 199 |
203 // Since [_timeZoneOffsetInSeconds] will crash if the input is far out of | 200 // Since [_timeZoneOffsetInSeconds] will crash if the input is far out of |
204 // the valid range we do a preliminary test that weeds out values that can | 201 // the valid range we do a preliminary test that weeds out values that can |
205 // not become valid even with timezone adjustments. | 202 // not become valid even with timezone adjustments. |
206 // The timezone adjustment is always less than a day, so adding a security | 203 // The timezone adjustment is always less than a day, so adding a security |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 | 315 |
319 static String _timeZoneNameForClampedSeconds(int secondsSinceEpoch) | 316 static String _timeZoneNameForClampedSeconds(int secondsSinceEpoch) |
320 native "DateNatives_timeZoneName"; | 317 native "DateNatives_timeZoneName"; |
321 | 318 |
322 static int _timeZoneOffsetInSecondsForClampedSeconds(int secondsSinceEpoch) | 319 static int _timeZoneOffsetInSecondsForClampedSeconds(int secondsSinceEpoch) |
323 native "DateNatives_timeZoneOffsetInSeconds"; | 320 native "DateNatives_timeZoneOffsetInSeconds"; |
324 | 321 |
325 static int _localTimeZoneAdjustmentInSeconds() | 322 static int _localTimeZoneAdjustmentInSeconds() |
326 native "DateNatives_localTimeZoneAdjustmentInSeconds"; | 323 native "DateNatives_localTimeZoneAdjustmentInSeconds"; |
327 } | 324 } |
OLD | NEW |