OLD | NEW |
---|---|
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 class DateImplementation implements Date { | 7 class DateImplementation implements Date { |
8 static final int _SECONDS_YEAR_2035 = 2051222400; | 8 static final int _MAX_VALUE = 8640000000000000; |
9 | 9 |
10 DateImplementation(int years, | 10 DateImplementation(int years, |
11 [int month = 1, | 11 [int month = 1, |
12 int day = 1, | 12 int day = 1, |
13 int hours = 0, | 13 int hours = 0, |
14 int minutes = 0, | 14 int minutes = 0, |
15 int seconds = 0, | 15 int seconds = 0, |
16 int milliseconds = 0, | 16 int milliseconds = 0, |
17 bool isUtc = false]) | 17 bool isUtc = false]) |
18 : _isUtc = isUtc, | 18 : _isUtc = isUtc, |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
73 throw new IllegalArgumentException(formattedString); | 73 throw new IllegalArgumentException(formattedString); |
74 } | 74 } |
75 if (addOneMillisecond) epochValue++; | 75 if (addOneMillisecond) epochValue++; |
76 return new DateImplementation.fromEpoch(epochValue, isUtc); | 76 return new DateImplementation.fromEpoch(epochValue, isUtc); |
77 } else { | 77 } else { |
78 throw new IllegalArgumentException(formattedString); | 78 throw new IllegalArgumentException(formattedString); |
79 } | 79 } |
80 } | 80 } |
81 | 81 |
82 DateImplementation.fromEpoch(int this.value, [bool isUtc = false]) | 82 DateImplementation.fromEpoch(int this.value, [bool isUtc = false]) |
83 : _isUtc = isUtc; | 83 : _isUtc = isUtc { |
84 if (value.abs() > _MAX_VALUE) throw new IllegalArgumentException(value); | |
85 } | |
84 | 86 |
85 bool operator ==(Object other) { | 87 bool operator ==(Object other) { |
86 if (other is !DateImplementation) return false; | 88 if (other is !DateImplementation) return false; |
87 return value == other.value; | 89 DateImplementation otherDate = other; |
90 return value == otherDate.value; | |
88 } | 91 } |
89 | 92 |
90 bool operator <(Date other) => value < other.value; | 93 bool operator <(Date other) => value < other.value; |
91 | 94 |
92 bool operator <=(Date other) => value <= other.value; | 95 bool operator <=(Date other) => value <= other.value; |
93 | 96 |
94 bool operator >(Date other) => value > other.value; | 97 bool operator >(Date other) => value > other.value; |
95 | 98 |
96 bool operator >=(Date other) => value >= other.value; | 99 bool operator >=(Date other) => value >= other.value; |
97 | 100 |
98 int compareTo(Date other) => value.compareTo(other.value); | 101 int compareTo(Date other) => value.compareTo(other.value); |
99 int hashCode() => value; | 102 int hashCode() => value; |
100 | 103 |
101 Date toLocal() { | 104 Date toLocal() { |
102 if (isUtc()) return new DateImplementation.fromEpoch(value, false); | 105 if (isUtc()) return new DateImplementation.fromEpoch(value, false); |
103 return this; | 106 return this; |
104 } | 107 } |
105 | 108 |
106 Date toUtc() { | 109 Date toUtc() { |
107 if (isUtc()) return this; | 110 if (isUtc()) return this; |
108 return new DateImplementation.fromEpoch(value, true); | 111 return new DateImplementation.fromEpoch(value, true); |
109 } | 112 } |
110 | 113 |
111 String get timeZoneName() { | 114 String get timeZoneName() { |
112 if (isUtc()) return "UTC"; | 115 if (isUtc()) return "UTC"; |
113 return _timeZoneName(_equivalentSeconds(_secondsSinceEpoch)); | 116 return _timeZoneName(value); |
114 } | 117 } |
115 | 118 |
116 Duration get timeZoneOffset() { | 119 Duration get timeZoneOffset() { |
117 if (isUtc()) return new Duration(0); | 120 if (isUtc()) return new Duration(0); |
118 int offsetInSeconds = | 121 int offsetInSeconds = _timeZoneOffsetInSeconds(value); |
119 _timeZoneOffsetInSeconds(_equivalentSeconds(_secondsSinceEpoch)); | |
120 return new Duration(seconds: offsetInSeconds); | 122 return new Duration(seconds: offsetInSeconds); |
121 } | 123 } |
122 | 124 |
123 int get year() { | 125 int get year() { |
124 int secondsSinceEpoch = _secondsSinceEpoch; | 126 return _decomposeIntoYearMonthDay(_localDateInUtcValue)[0]; |
125 // According to V8 some library calls have troubles with negative values. | |
126 // Therefore clamp to 0 - year 2035 (which is less than the size of 32bit). | |
127 if (secondsSinceEpoch >= 0 && secondsSinceEpoch < _SECONDS_YEAR_2035) { | |
128 return _getYear(secondsSinceEpoch, isUtc()); | |
129 } | |
130 | |
131 // Approximate the result. We don't take timeZone into account. | |
132 int approximateYear = _yearsFromSecondsSinceEpoch(secondsSinceEpoch); | |
133 int equivalentYear = _equivalentYear(approximateYear); | |
134 int y = _getYear(_equivalentSeconds(_secondsSinceEpoch), isUtc()); | |
135 return approximateYear + (y - equivalentYear); | |
136 } | 127 } |
137 | 128 |
138 int get month() { | 129 int get month() { |
139 return _getMonth(_equivalentSeconds(_secondsSinceEpoch), isUtc()); | 130 return _decomposeIntoYearMonthDay(_localDateInUtcValue)[1]; |
140 } | 131 } |
141 | 132 |
142 int get day() { | 133 int get day() { |
143 return _getDay(_equivalentSeconds(_secondsSinceEpoch), isUtc()); | 134 return _decomposeIntoYearMonthDay(_localDateInUtcValue)[2]; |
144 } | 135 } |
145 | 136 |
146 int get hours() { | 137 int get hours() { |
147 return _getHours(_equivalentSeconds(_secondsSinceEpoch), isUtc()); | 138 int valueInHours = _flooredDivision(_localDateInUtcValue, |
139 Duration.MILLISECONDS_PER_HOUR); | |
140 return valueInHours % Duration.HOURS_PER_DAY; | |
148 } | 141 } |
149 | 142 |
150 int get minutes() { | 143 int get minutes() { |
151 return _getMinutes(_equivalentSeconds(_secondsSinceEpoch), isUtc()); | 144 int valueInMinutes = _flooredDivision(_localDateInUtcValue, |
145 Duration.MILLISECONDS_PER_MINUTE); | |
146 return valueInMinutes % Duration.MINUTES_PER_HOUR; | |
152 } | 147 } |
153 | 148 |
154 int get seconds() { | 149 int get seconds() { |
155 return _getSeconds(_equivalentSeconds(_secondsSinceEpoch), isUtc()); | 150 // Seconds are unaffected by the timezone the user is in. So we can |
151 // directly use the value and not the [_localDateInUtcValue]. | |
152 int valueInSeconds = | |
153 _flooredDivision(value, Duration.MILLISECONDS_PER_SECOND); | |
154 return valueInSeconds % Duration.SECONDS_PER_MINUTE; | |
156 } | 155 } |
157 | 156 |
158 int get milliseconds() { | 157 int get milliseconds() { |
158 // Milliseconds are unaffected by the timezone the user is in. So we can | |
159 // directly use the value and not the [_localDateInUtcValue]. | |
159 return value % Duration.MILLISECONDS_PER_SECOND; | 160 return value % Duration.MILLISECONDS_PER_SECOND; |
160 } | 161 } |
161 | 162 |
162 int get _secondsSinceEpoch() { | |
163 // Always round down. | |
164 if (value < 0) { | |
165 return (value + 1) ~/ Duration.MILLISECONDS_PER_SECOND - 1; | |
166 } else { | |
167 return value ~/ Duration.MILLISECONDS_PER_SECOND; | |
168 } | |
169 } | |
170 | |
171 int get weekday() { | 163 int get weekday() { |
172 final Date unixTimeStart = new Date(1970, 1, 1, 0, 0, 0, 0, isUtc()); | 164 int daysSince1970 = |
173 int msSince1970 = this.difference(unixTimeStart).inMilliseconds; | 165 _flooredDivision(_localDateInUtcValue, Duration.MILLISECONDS_PER_DAY); |
174 // Adjust the milliseconds to avoid problems with summer-time. | |
175 if (hours < 2) { | |
176 msSince1970 += 2 * Duration.MILLISECONDS_PER_HOUR; | |
177 } | |
178 // Compute the floor of msSince1970 / Duration.MS_PER_DAY. | |
179 int daysSince1970; | |
180 if (msSince1970 >= 0) { | |
181 daysSince1970 = msSince1970 ~/ Duration.MILLISECONDS_PER_DAY; | |
182 } else { | |
183 daysSince1970 = (msSince1970 - Duration.MILLISECONDS_PER_DAY + 1) ~/ | |
184 Duration.MILLISECONDS_PER_DAY; | |
185 } | |
186 // 1970-1-1 was a Thursday. | 166 // 1970-1-1 was a Thursday. |
187 return ((daysSince1970 + Date.THU) % Date.DAYS_IN_WEEK); | 167 return ((daysSince1970 + Date.THU) % Date.DAYS_IN_WEEK); |
188 } | 168 } |
189 | 169 |
190 bool isUtc() => _isUtc; | 170 bool isUtc() => _isUtc; |
191 | 171 |
192 String toString() { | 172 String toString() { |
193 String fourDigits(int n) { | 173 String fourDigits(int n) { |
194 int absN = n.abs(); | 174 int absN = n.abs(); |
195 String sign = n < 0 ? "-" : ""; | 175 String sign = n < 0 ? "-" : ""; |
(...skipping 19 matching lines...) Expand all Loading... | |
215 String min = twoDigits(minutes); | 195 String min = twoDigits(minutes); |
216 String sec = twoDigits(seconds); | 196 String sec = twoDigits(seconds); |
217 String ms = threeDigits(milliseconds); | 197 String ms = threeDigits(milliseconds); |
218 if (isUtc()) { | 198 if (isUtc()) { |
219 return "$y-$m-$d $h:$min:$sec.${ms}Z"; | 199 return "$y-$m-$d $h:$min:$sec.${ms}Z"; |
220 } else { | 200 } else { |
221 return "$y-$m-$d $h:$min:$sec.$ms"; | 201 return "$y-$m-$d $h:$min:$sec.$ms"; |
222 } | 202 } |
223 } | 203 } |
224 | 204 |
225 // Adds the [duration] to this Date instance. | 205 /** Returns a new [Date] with the [duration] added to [this]. */ |
226 Date add(Duration duration) { | 206 Date add(Duration duration) { |
227 return new DateImplementation.fromEpoch(value + duration.inMilliseconds, | 207 return new DateImplementation.fromEpoch(value + duration.inMilliseconds, |
228 isUtc()); | 208 isUtc()); |
229 } | 209 } |
230 | 210 |
231 // Subtracts the [duration] from this Date instance. | 211 /** Returns a new [Date] with the [duration] subtracted from [this]. */ |
232 Date subtract(Duration duration) { | 212 Date subtract(Duration duration) { |
233 return new DateImplementation.fromEpoch(value - duration.inMilliseconds, | 213 return new DateImplementation.fromEpoch(value - duration.inMilliseconds, |
234 isUtc()); | 214 isUtc()); |
235 } | 215 } |
236 | 216 |
237 // Returns a [Duration] with the difference of [this] and [other]. | 217 /** Returns a [Duration] with the difference of [this] and [other]. */ |
238 Duration difference(Date other) { | 218 Duration difference(Date other) { |
239 return new DurationImplementation(milliseconds: value - other.value); | 219 return new DurationImplementation(milliseconds: value - other.value); |
240 } | 220 } |
241 | 221 |
242 // Returns the UTC year for the corresponding [secondsSinceEpoch]. | 222 /** The first list contains the days until each month in non-leap years. The |
243 // It is relatively fast for values in the range 0 to year 2098. | 223 * second list contains the days in leap years. */ |
224 static final List<List<int>> _DAYS_UNTIL_MONTH = | |
225 const [const [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334], | |
226 const [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335]]; | |
227 | |
228 // Returns the UTC year, month and day for the corresponding | |
229 // [millisecondsSinceEpoch]. | |
244 // Code is adapted from V8. | 230 // Code is adapted from V8. |
245 static int _yearsFromSecondsSinceEpoch(int secondsSinceEpoch) { | 231 static List<int> _decomposeIntoYearMonthDay(int millisecondsSinceEpoch) { |
232 // TODO(floitsch): cache result. | |
246 final int DAYS_IN_4_YEARS = 4 * 365 + 1; | 233 final int DAYS_IN_4_YEARS = 4 * 365 + 1; |
247 final int DAYS_IN_100_YEARS = 25 * DAYS_IN_4_YEARS - 1; | 234 final int DAYS_IN_100_YEARS = 25 * DAYS_IN_4_YEARS - 1; |
248 final int DAYS_IN_400_YEARS = 4 * DAYS_IN_100_YEARS + 1; | 235 final int DAYS_IN_400_YEARS = 4 * DAYS_IN_100_YEARS + 1; |
249 final int DAYS_1970_TO_2000 = 30 * 365 + 7; | 236 final int DAYS_1970_TO_2000 = 30 * 365 + 7; |
250 final int DAYS_OFFSET = 1000 * DAYS_IN_400_YEARS + 5 * DAYS_IN_400_YEARS - | 237 final int DAYS_OFFSET = 1000 * DAYS_IN_400_YEARS + 5 * DAYS_IN_400_YEARS - |
251 DAYS_1970_TO_2000; | 238 DAYS_1970_TO_2000; |
252 final int YEARS_OFFSET = 400000; | 239 final int YEARS_OFFSET = 400000; |
253 final int DAYS_YEAR_2098 = DAYS_IN_100_YEARS + 6 * DAYS_IN_4_YEARS; | |
254 | 240 |
255 int days = secondsSinceEpoch ~/ Duration.SECONDS_PER_DAY; | 241 int resultYear = 0; |
256 if (days > 0 && days < DAYS_YEAR_2098) { | 242 int resultMonth = 0; |
257 // According to V8 this fast case works for dates from 1970 to 2099. | 243 int resultDay = 0; |
258 return 1970 + (4 * days + 2) ~/ DAYS_IN_4_YEARS; | 244 |
259 } else { | 245 // Always round down. |
260 days += DAYS_OFFSET; | 246 int days = _flooredDivision(millisecondsSinceEpoch, |
261 int result = 400 * (days ~/ DAYS_IN_400_YEARS) - YEARS_OFFSET; | 247 Duration.MILLISECONDS_PER_DAY); |
262 days = days.remainder(DAYS_IN_400_YEARS); | 248 days += DAYS_OFFSET; |
263 days--; | 249 resultYear = 400 * (days ~/ DAYS_IN_400_YEARS) - YEARS_OFFSET; |
264 int yd1 = days ~/ DAYS_IN_100_YEARS; | 250 days = days.remainder(DAYS_IN_400_YEARS); |
265 days = days.remainder(DAYS_IN_100_YEARS); | 251 days--; |
266 result += 100 * yd1; | 252 int yd1 = days ~/ DAYS_IN_100_YEARS; |
267 days++; | 253 days = days.remainder(DAYS_IN_100_YEARS); |
268 int yd2 = days ~/ DAYS_IN_4_YEARS; | 254 resultYear += 100 * yd1; |
269 days = days.remainder(DAYS_IN_4_YEARS); | 255 days++; |
270 result += 4 * yd2; | 256 int yd2 = days ~/ DAYS_IN_4_YEARS; |
271 days--; | 257 days = days.remainder(DAYS_IN_4_YEARS); |
272 int yd3 = days ~/ 365; | 258 resultYear += 4 * yd2; |
273 days = days.remainder(365); | 259 days--; |
274 result += yd3; | 260 int yd3 = days ~/ 365; |
275 return result; | 261 days = days.remainder(365); |
262 resultYear += yd3; | |
263 | |
264 bool isLeap = (yd1 == 0 || yd2 != 0) && yd3 == 0; | |
265 if (isLeap) days++; | |
266 | |
267 List<int> daysUntilMonth = _DAYS_UNTIL_MONTH[isLeap ? 1 : 0]; | |
268 for (resultMonth = 12; | |
269 daysUntilMonth[resultMonth - 1] > days; | |
270 resultMonth--) { | |
271 // Do nothing. | |
276 } | 272 } |
273 resultDay = days - daysUntilMonth[resultMonth - 1] + 1; | |
274 List<int> result = new List<int>(3); | |
kasperl
2012/06/11 12:02:28
return <int>[resultYear, resultMonth, resultDay]?
floitsch
2012/06/11 13:46:20
Done.
| |
275 result[0] = resultYear; | |
276 result[1] = resultMonth; | |
277 result[2] = resultDay; | |
278 return result; | |
277 } | 279 } |
278 | 280 |
279 // Given [secondsSinceEpoch] returns seconds such that they are at the same | 281 /** |
280 // time in an equivalent year (see [_equivalentYear]). | 282 * Returns the amount of milliseconds in UTC that represent the same values as |
281 // Leap seconds are ignored. | 283 * [this]. |
282 static int _equivalentSeconds(int secondsSinceEpoch) { | 284 * |
283 if (secondsSinceEpoch >= 0 && secondsSinceEpoch < _SECONDS_YEAR_2035) { | 285 * Say [:t:] is the result of this function, then |
284 return secondsSinceEpoch; | 286 * * [:this.year == new Date.fromEpoch(t, isUtc: true).year:], |
285 } | 287 * * [:this.month == new Date.fromEpoch(t, isUtc: true).month:], |
286 int year = _yearsFromSecondsSinceEpoch(secondsSinceEpoch); | 288 * * [:this.day == new Date.fromEpoch(t, isUtc: true).day:], |
287 int days = _dayFromYear(year); | 289 * * [:this.hours == new Date.fromEpoch(t, isUtc: true).hours:], |
288 int equivalentYear = _equivalentYear(year); | 290 * * ... |
289 int equivalentDays = _dayFromYear(equivalentYear); | 291 * |
290 int diffDays = equivalentDays - days; | 292 * Daylight savings is computed as if the date was computed in [1970..2037]. |
291 return secondsSinceEpoch + diffDays * Duration.SECONDS_PER_DAY; | 293 * If [this] lies outside this range then it is a year with similar properties |
294 * (leap year, weekdays) is used instead. | |
295 */ | |
296 int get _localDateInUtcValue() { | |
297 if (isUtc()) return value; | |
298 int offset = | |
299 _timeZoneOffsetInSeconds(value) * Duration.MILLISECONDS_PER_SECOND; | |
300 return value - offset; | |
301 } | |
302 | |
303 static int _flooredDivision(int a, int b) { | |
304 return (a - (a < 0 ? b - 1 : 0)) ~/ b; | |
292 } | 305 } |
293 | 306 |
294 // Returns the days since 1970 for the start of the given [year]. | 307 // Returns the days since 1970 for the start of the given [year]. |
295 // [year] may be before epoch. | 308 // [year] may be before epoch. |
296 static int _dayFromYear(int year) { | 309 static int _dayFromYear(int year) { |
297 int flooredDivision(int a, int b) { | 310 return 365 * (year - 1970) |
298 return (a - (a < 0 ? b - 1 : 0)) ~/ b; | 311 + _flooredDivision(year - 1969, 4) |
312 - _flooredDivision(year - 1901, 100) | |
313 + _flooredDivision(year - 1601, 400); | |
314 } | |
315 | |
316 static bool _isLeapYear(y) { | |
317 return (y.remainder(4) == 0) && | |
318 ((y.remainder(100) != 0) || (y.remainder(400) == 0)); | |
319 } | |
320 | |
321 static _brokenDownDateToMillisecondsSinceEpoch( | |
322 int years, int month, int day, | |
323 int hours, int minutes, int seconds, int milliseconds, | |
324 bool isUtc) { | |
325 if ((month < 1) || (month > 12)) return null; | |
326 if ((day < 1) || (day > 31)) return null; | |
327 // Leap seconds can lead to hours == 24. | |
328 if ((hours < 0) || (hours > 24)) return null; | |
329 if ((hours == 24) && ((minutes != 0) || (seconds != 0))) return null; | |
330 if ((minutes < 0) || (minutes > 59)) return null; | |
331 if ((seconds < 0) || (seconds > 59)) return null; | |
332 if ((milliseconds < 0) || (milliseconds > 999)) return null; | |
333 | |
334 // First compute the seconds in UTC, independent of the [isUtc] flag. If | |
335 // necessary we will add the time-zone offset later on. | |
336 int days = day - 1; | |
337 days += _DAYS_UNTIL_MONTH[_isLeapYear(years) ? 1 : 0][month - 1]; | |
338 days += _dayFromYear(years); | |
339 int millisecondsSinceEpoch = days * Duration.MILLISECONDS_PER_DAY + | |
340 hours * Duration.MILLISECONDS_PER_HOUR + | |
341 minutes * Duration.MILLISECONDS_PER_MINUTE+ | |
342 seconds * Duration.MILLISECONDS_PER_SECOND + | |
343 milliseconds; | |
344 | |
345 // Since [_timeZoneOffsetInSeconds] will crash if the input is far out of | |
346 // the valid range we do a preliminary test that weeds out values that can | |
347 // not become valid even with timezone adjustments. | |
348 // The timezone adjustment is always less than a day, so adding a security | |
349 // margin of one day should be enough. | |
350 if (millisecondsSinceEpoch.abs() > | |
351 (_MAX_VALUE + Duration.MILLISECONDS_PER_DAY)) { | |
352 return null; | |
299 } | 353 } |
300 | 354 |
301 return 365 * (year - 1970) | 355 if (!isUtc) { |
302 + flooredDivision(year - 1969, 4) | 356 // Note that we need to add the local timezone adjustement before asking |
303 - flooredDivision(year - 1901, 100) | 357 // for the correct zone offset. |
304 + flooredDivision(year - 1601, 400); | 358 int adjustment = _localTimeZoneAdjustmentInSeconds() * |
359 Duration.MILLISECONDS_PER_SECOND; | |
360 int zoneOffset = | |
361 _timeZoneOffsetInSeconds(millisecondsSinceEpoch + adjustment); | |
362 millisecondsSinceEpoch += zoneOffset * Duration.MILLISECONDS_PER_SECOND; | |
363 } | |
364 if (millisecondsSinceEpoch.abs() > _MAX_VALUE) return null; | |
365 return millisecondsSinceEpoch; | |
305 } | 366 } |
306 | 367 |
307 // Returns a year in the range 2008-2035 matching | 368 /** |
308 // - leap year, and | 369 * Returns a year in the range 2008-2035 matching |
309 // - week day of first day. | 370 * * leap year, and |
310 // Leap seconds are ignored. | 371 * * week day of first day. |
311 // Adapted from V8's date implementation. See ECMA 262 - 15.9.1.9. | 372 * |
373 * Leap seconds are ignored. | |
374 * Adapted from V8's date implementation. See ECMA 262 - 15.9.1.9. | |
375 */ | |
312 static _equivalentYear(int year) { | 376 static _equivalentYear(int year) { |
313 // Returns 1 if in leap year. 0 otherwise. | |
314 bool inLeapYear(year) { | |
315 return (year.remainder(4) == 0) && | |
316 ((year.remainder(100) != 0) || (year.remainder(400) == 0)); | |
317 } | |
318 | |
319 // Returns the week day (in range 0 - 6). | 377 // Returns the week day (in range 0 - 6). |
320 int weekDay(year) { | 378 int weekDay(y) { |
321 // 1/1/1970 was a Thursday. | 379 // 1/1/1970 was a Thursday. |
322 return (_dayFromYear(year) + 4) % 7; | 380 return (_dayFromYear(y) + 4) % 7; |
323 } | 381 } |
324 // 1/1/1956 was a Sunday (i.e. weekday 0). 1956 was a leap-year. | 382 // 1/1/1956 was a Sunday (i.e. weekday 0). 1956 was a leap-year. |
325 // 1/1/1967 was a Sunday (i.e. weekday 0). | 383 // 1/1/1967 was a Sunday (i.e. weekday 0). |
326 // Without leap years a subsequent year has a week day + 1 (for example | 384 // Without leap years a subsequent year has a week day + 1 (for example |
327 // 1/1/1968 was a Monday). With leap-years it jumps over one week day | 385 // 1/1/1968 was a Monday). With leap-years it jumps over one week day |
328 // (e.g. 1/1/1957 was a Tuesday). | 386 // (e.g. 1/1/1957 was a Tuesday). |
329 // After 12 years the weekdays have advanced by 12 days + 3 leap days = | 387 // After 12 years the weekdays have advanced by 12 days + 3 leap days = |
330 // 15 days. 15 % 7 = 1. So after 12 years the week day has always | 388 // 15 days. 15 % 7 = 1. So after 12 years the week day has always |
331 // (now independently of leap-years) advanced by one. | 389 // (now independently of leap-years) advanced by one. |
332 // weekDay * 12 gives thus a year starting with the wanted weekDay. | 390 // weekDay * 12 gives thus a year starting with the wanted weekDay. |
333 int recentYear = (inLeapYear(year) ? 1956 : 1967) + (weekDay(year) * 12); | 391 int recentYear = (_isLeapYear(year) ? 1956 : 1967) + (weekDay(year) * 12); |
334 // Close to the year 2008 the calendar cycles every 4 * 7 years (4 for the | 392 // Close to the year 2008 the calendar cycles every 4 * 7 years (4 for the |
335 // leap years, 7 for the weekdays). | 393 // leap years, 7 for the weekdays). |
336 // Find the year in the range 2008..2037 that is equivalent mod 28. | 394 // Find the year in the range 2008..2037 that is equivalent mod 28. |
337 return 2008 + (recentYear - 2008) % 28; | 395 return 2008 + (recentYear - 2008) % 28; |
338 } | 396 } |
339 | 397 |
340 static _brokenDownDateToMillisecondsSinceEpoch( | 398 /** |
341 int years, int month, int day, | 399 * Returns the UTC year for the corresponding [secondsSinceEpoch]. |
342 int hours, int minutes, int seconds, int milliseconds, | 400 * It is relatively fast for values in the range 0 to year 2098. |
343 bool isUtc) { | 401 * |
344 if ((month < 1) || (month > 12)) return null; | 402 * Code is adapted from V8. |
345 if ((day < 1) || (day > 31)) return null; | 403 */ |
346 // Leap seconds can lead to hours == 24. | 404 static int _yearsFromSecondsSinceEpoch(int secondsSinceEpoch) { |
347 if ((hours < 0) || (hours > 24)) return null; | 405 final int DAYS_IN_4_YEARS = 4 * 365 + 1; |
348 if ((hours == 24) && ((minutes != 0) || (seconds != 0))) return null; | 406 final int DAYS_IN_100_YEARS = 25 * DAYS_IN_4_YEARS - 1; |
349 if ((minutes < 0) || (minutes > 59)) return null; | 407 final int DAYS_YEAR_2098 = DAYS_IN_100_YEARS + 6 * DAYS_IN_4_YEARS; |
350 if ((seconds < 0) || (seconds > 59)) return null; | |
351 if ((milliseconds < 0) || (milliseconds > 999)) return null; | |
352 | 408 |
353 int equivalentYear; | 409 int days = secondsSinceEpoch ~/ Duration.SECONDS_PER_DAY; |
354 int offsetInSeconds; | 410 if (days > 0 && days < DAYS_YEAR_2098) { |
355 // According to V8 some library calls have troubles with negative values. | 411 // According to V8 this fast case works for dates from 1970 to 2099. |
356 // Therefore clamp to 1970 - year 2035 (which is less than the size of | 412 return 1970 + (4 * days + 2) ~/ DAYS_IN_4_YEARS; |
357 // 32bit). | |
358 // We exclude the year 1970 when the time is not UTC, since the epoch | |
359 // value could then be negative. | |
360 if (years < (isUtc ? 1970 : 1971) || years > 2035) { | |
361 equivalentYear = _equivalentYear(years); | |
362 int offsetInDays = (_dayFromYear(years) - _dayFromYear(equivalentYear)); | |
363 // Leap seconds are ignored. | |
364 offsetInSeconds = offsetInDays * Duration.SECONDS_PER_DAY; | |
365 } else { | |
366 equivalentYear = years; | |
367 offsetInSeconds = 0; | |
368 } | 413 } |
369 int secondsSinceEpoch = _brokenDownDateToSecondsSinceEpoch( | 414 int ms = secondsSinceEpoch * Duration.MILLISECONDS_PER_SECOND; |
370 equivalentYear, month, day, hours, minutes, seconds, isUtc); | 415 return _decomposeIntoYearMonthDay(ms)[0]; |
371 int adjustedSeconds = secondsSinceEpoch + offsetInSeconds; | 416 } |
372 return adjustedSeconds * Duration.MILLISECONDS_PER_SECOND + milliseconds; | 417 |
418 /** | |
419 * Returns a date in seconds that is equivalent to the current date. An | |
420 * equivalent date has the same fields ([:month:], [:day:], etc.) as the | |
421 * [this], but the [:year:] is in the range [1970..2037]. | |
422 * | |
423 * * The time since the beginning of the year is the same. | |
424 * * If [this] is in a leap year then the returned seconds are in a leap | |
425 * year, too. | |
426 * * The week day of [this] is the same as the one for the returned date. | |
427 */ | |
428 static int _equivalentSeconds(int millisecondsSinceEpoch) { | |
429 final int CUT_OFF_SECONDS = 2100000000; | |
430 | |
431 int secondsSinceEpoch = _flooredDivision(millisecondsSinceEpoch, | |
432 Duration.MILLISECONDS_PER_SECOND); | |
433 | |
434 if (secondsSinceEpoch < 0 || secondsSinceEpoch >= CUT_OFF_SECONDS) { | |
435 int year = _yearsFromSecondsSinceEpoch(secondsSinceEpoch); | |
436 int days = _dayFromYear(year); | |
437 int equivalentYear = _equivalentYear(year); | |
438 int equivalentDays = _dayFromYear(equivalentYear); | |
439 int diffDays = equivalentDays - days; | |
440 secondsSinceEpoch += diffDays * Duration.SECONDS_PER_DAY; | |
441 } | |
442 return secondsSinceEpoch; | |
443 } | |
444 | |
445 static int _timeZoneOffsetInSeconds(int millisecondsSinceEpoch) { | |
446 int equivalentSeconds = _equivalentSeconds(millisecondsSinceEpoch); | |
447 return _timeZoneOffsetInSecondsForClampedSeconds(equivalentSeconds); | |
448 } | |
449 | |
450 static String _timeZoneName(int millisecondsSinceEpoch) { | |
451 int equivalentSeconds = _equivalentSeconds(millisecondsSinceEpoch); | |
452 return _timeZoneNameForClampedSeconds(equivalentSeconds); | |
373 } | 453 } |
374 | 454 |
375 final bool _isUtc; | 455 final bool _isUtc; |
376 final int value; | 456 final int value; |
377 | 457 |
378 | |
379 // Natives | 458 // Natives |
380 static _brokenDownDateToSecondsSinceEpoch( | |
381 int years, int month, int day, int hours, int minutes, int seconds, | |
382 bool isUtc) native "DateNatives_brokenDownToSecondsSinceEpoch"; | |
383 | |
384 static int _getCurrentMs() native "DateNatives_currentTimeMillis"; | 459 static int _getCurrentMs() native "DateNatives_currentTimeMillis"; |
385 | 460 |
386 static String _timeZoneName(int secondsSinceEpoch) | 461 static String _timeZoneNameForClampedSeconds(int secondsSinceEpoch) |
387 native "DateNatives_timeZoneName"; | 462 native "DateNatives_timeZoneName"; |
388 | 463 |
389 static int _timeZoneOffsetInSeconds(int secondsSinceEpoch) | 464 static int _timeZoneOffsetInSecondsForClampedSeconds(int secondsSinceEpoch) |
390 native "DateNatives_timeZoneOffsetInSeconds"; | 465 native "DateNatives_timeZoneOffsetInSeconds"; |
391 | 466 |
392 // TODO(floitsch): it would be more efficient if we didn't call the native | 467 static int _localTimeZoneAdjustmentInSeconds() |
393 // function for every member, but cached the broken-down date. | 468 native "DateNatives_localTimeZoneAdjustmentInSeconds"; |
394 static int _getYear(int secondsSinceEpoch, bool isUtc) | |
395 native "DateNatives_getYear"; | |
396 | |
397 static int _getMonth(int secondsSinceEpoch, bool isUtc) | |
398 native "DateNatives_getMonth"; | |
399 | |
400 static int _getDay(int secondsSinceEpoch, bool isUtc) | |
401 native "DateNatives_getDay"; | |
402 | |
403 static int _getHours(int secondsSinceEpoch, bool isUtc) | |
404 native "DateNatives_getHours"; | |
405 | |
406 static int _getMinutes(int secondsSinceEpoch, bool isUtc) | |
407 native "DateNatives_getMinutes"; | |
408 | |
409 static int _getSeconds(int secondsSinceEpoch, bool isUtc) | |
410 native "DateNatives_getSeconds"; | |
411 } | 469 } |
OLD | NEW |