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 return <int>[resultYear, resultMonth, resultDay]; |
277 } | 275 } |
278 | 276 |
279 // Given [secondsSinceEpoch] returns seconds such that they are at the same | 277 /** |
280 // time in an equivalent year (see [_equivalentYear]). | 278 * Returns the amount of milliseconds in UTC that represent the same values as |
281 // Leap seconds are ignored. | 279 * [this]. |
282 static int _equivalentSeconds(int secondsSinceEpoch) { | 280 * |
283 if (secondsSinceEpoch >= 0 && secondsSinceEpoch < _SECONDS_YEAR_2035) { | 281 * Say [:t:] is the result of this function, then |
284 return secondsSinceEpoch; | 282 * * [:this.year == new Date.fromEpoch(t, isUtc: true).year:], |
285 } | 283 * * [:this.month == new Date.fromEpoch(t, isUtc: true).month:], |
286 int year = _yearsFromSecondsSinceEpoch(secondsSinceEpoch); | 284 * * [:this.day == new Date.fromEpoch(t, isUtc: true).day:], |
287 int days = _dayFromYear(year); | 285 * * [:this.hours == new Date.fromEpoch(t, isUtc: true).hours:], |
288 int equivalentYear = _equivalentYear(year); | 286 * * ... |
289 int equivalentDays = _dayFromYear(equivalentYear); | 287 * |
290 int diffDays = equivalentDays - days; | 288 * Daylight savings is computed as if the date was computed in [1970..2037]. |
291 return secondsSinceEpoch + diffDays * Duration.SECONDS_PER_DAY; | 289 * If [this] lies outside this range then it is a year with similar properties |
| 290 * (leap year, weekdays) is used instead. |
| 291 */ |
| 292 int get _localDateInUtcValue() { |
| 293 if (isUtc()) return value; |
| 294 int offset = |
| 295 _timeZoneOffsetInSeconds(value) * Duration.MILLISECONDS_PER_SECOND; |
| 296 return value + offset; |
| 297 } |
| 298 |
| 299 static int _flooredDivision(int a, int b) { |
| 300 return (a - (a < 0 ? b - 1 : 0)) ~/ b; |
292 } | 301 } |
293 | 302 |
294 // Returns the days since 1970 for the start of the given [year]. | 303 // Returns the days since 1970 for the start of the given [year]. |
295 // [year] may be before epoch. | 304 // [year] may be before epoch. |
296 static int _dayFromYear(int year) { | 305 static int _dayFromYear(int year) { |
297 int flooredDivision(int a, int b) { | 306 return 365 * (year - 1970) |
298 return (a - (a < 0 ? b - 1 : 0)) ~/ b; | 307 + _flooredDivision(year - 1969, 4) |
| 308 - _flooredDivision(year - 1901, 100) |
| 309 + _flooredDivision(year - 1601, 400); |
| 310 } |
| 311 |
| 312 static bool _isLeapYear(y) { |
| 313 return (y.remainder(4) == 0) && |
| 314 ((y.remainder(100) != 0) || (y.remainder(400) == 0)); |
| 315 } |
| 316 |
| 317 static _brokenDownDateToMillisecondsSinceEpoch( |
| 318 int years, int month, int day, |
| 319 int hours, int minutes, int seconds, int milliseconds, |
| 320 bool isUtc) { |
| 321 if ((month < 1) || (month > 12)) return null; |
| 322 if ((day < 1) || (day > 31)) return null; |
| 323 // Leap seconds can lead to hours == 24. |
| 324 if ((hours < 0) || (hours > 24)) return null; |
| 325 if ((hours == 24) && ((minutes != 0) || (seconds != 0))) return null; |
| 326 if ((minutes < 0) || (minutes > 59)) return null; |
| 327 if ((seconds < 0) || (seconds > 59)) return null; |
| 328 if ((milliseconds < 0) || (milliseconds > 999)) return null; |
| 329 |
| 330 // First compute the seconds in UTC, independent of the [isUtc] flag. If |
| 331 // necessary we will add the time-zone offset later on. |
| 332 int days = day - 1; |
| 333 days += _DAYS_UNTIL_MONTH[_isLeapYear(years) ? 1 : 0][month - 1]; |
| 334 days += _dayFromYear(years); |
| 335 int millisecondsSinceEpoch = days * Duration.MILLISECONDS_PER_DAY + |
| 336 hours * Duration.MILLISECONDS_PER_HOUR + |
| 337 minutes * Duration.MILLISECONDS_PER_MINUTE+ |
| 338 seconds * Duration.MILLISECONDS_PER_SECOND + |
| 339 milliseconds; |
| 340 |
| 341 // Since [_timeZoneOffsetInSeconds] will crash if the input is far out of |
| 342 // the valid range we do a preliminary test that weeds out values that can |
| 343 // not become valid even with timezone adjustments. |
| 344 // The timezone adjustment is always less than a day, so adding a security |
| 345 // margin of one day should be enough. |
| 346 if (millisecondsSinceEpoch.abs() > |
| 347 (_MAX_VALUE + Duration.MILLISECONDS_PER_DAY)) { |
| 348 return null; |
299 } | 349 } |
300 | 350 |
301 return 365 * (year - 1970) | 351 if (!isUtc) { |
302 + flooredDivision(year - 1969, 4) | 352 // Note that we need to remove the local timezone adjustement before |
303 - flooredDivision(year - 1901, 100) | 353 // asking for the correct zone offset. |
304 + flooredDivision(year - 1601, 400); | 354 int adjustment = _localTimeZoneAdjustmentInSeconds() * |
| 355 Duration.MILLISECONDS_PER_SECOND; |
| 356 int zoneOffset = |
| 357 _timeZoneOffsetInSeconds(millisecondsSinceEpoch - adjustment); |
| 358 millisecondsSinceEpoch -= zoneOffset * Duration.MILLISECONDS_PER_SECOND; |
| 359 } |
| 360 if (millisecondsSinceEpoch.abs() > _MAX_VALUE) return null; |
| 361 return millisecondsSinceEpoch; |
305 } | 362 } |
306 | 363 |
307 // Returns a year in the range 2008-2035 matching | 364 /** |
308 // - leap year, and | 365 * Returns a year in the range 2008-2035 matching |
309 // - week day of first day. | 366 * * leap year, and |
310 // Leap seconds are ignored. | 367 * * week day of first day. |
311 // Adapted from V8's date implementation. See ECMA 262 - 15.9.1.9. | 368 * |
| 369 * Leap seconds are ignored. |
| 370 * Adapted from V8's date implementation. See ECMA 262 - 15.9.1.9. |
| 371 */ |
312 static _equivalentYear(int year) { | 372 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). | 373 // Returns the week day (in range 0 - 6). |
320 int weekDay(year) { | 374 int weekDay(y) { |
321 // 1/1/1970 was a Thursday. | 375 // 1/1/1970 was a Thursday. |
322 return (_dayFromYear(year) + 4) % 7; | 376 return (_dayFromYear(y) + 4) % 7; |
323 } | 377 } |
324 // 1/1/1956 was a Sunday (i.e. weekday 0). 1956 was a leap-year. | 378 // 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). | 379 // 1/1/1967 was a Sunday (i.e. weekday 0). |
326 // Without leap years a subsequent year has a week day + 1 (for example | 380 // 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 | 381 // 1/1/1968 was a Monday). With leap-years it jumps over one week day |
328 // (e.g. 1/1/1957 was a Tuesday). | 382 // (e.g. 1/1/1957 was a Tuesday). |
329 // After 12 years the weekdays have advanced by 12 days + 3 leap days = | 383 // 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 | 384 // 15 days. 15 % 7 = 1. So after 12 years the week day has always |
331 // (now independently of leap-years) advanced by one. | 385 // (now independently of leap-years) advanced by one. |
332 // weekDay * 12 gives thus a year starting with the wanted weekDay. | 386 // weekDay * 12 gives thus a year starting with the wanted weekDay. |
333 int recentYear = (inLeapYear(year) ? 1956 : 1967) + (weekDay(year) * 12); | 387 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 | 388 // Close to the year 2008 the calendar cycles every 4 * 7 years (4 for the |
335 // leap years, 7 for the weekdays). | 389 // leap years, 7 for the weekdays). |
336 // Find the year in the range 2008..2037 that is equivalent mod 28. | 390 // Find the year in the range 2008..2037 that is equivalent mod 28. |
337 return 2008 + (recentYear - 2008) % 28; | 391 return 2008 + (recentYear - 2008) % 28; |
338 } | 392 } |
339 | 393 |
340 static _brokenDownDateToMillisecondsSinceEpoch( | 394 /** |
341 int years, int month, int day, | 395 * Returns the UTC year for the corresponding [secondsSinceEpoch]. |
342 int hours, int minutes, int seconds, int milliseconds, | 396 * It is relatively fast for values in the range 0 to year 2098. |
343 bool isUtc) { | 397 * |
344 if ((month < 1) || (month > 12)) return null; | 398 * Code is adapted from V8. |
345 if ((day < 1) || (day > 31)) return null; | 399 */ |
346 // Leap seconds can lead to hours == 24. | 400 static int _yearsFromSecondsSinceEpoch(int secondsSinceEpoch) { |
347 if ((hours < 0) || (hours > 24)) return null; | 401 final int DAYS_IN_4_YEARS = 4 * 365 + 1; |
348 if ((hours == 24) && ((minutes != 0) || (seconds != 0))) return null; | 402 final int DAYS_IN_100_YEARS = 25 * DAYS_IN_4_YEARS - 1; |
349 if ((minutes < 0) || (minutes > 59)) return null; | 403 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 | 404 |
353 int equivalentYear; | 405 int days = secondsSinceEpoch ~/ Duration.SECONDS_PER_DAY; |
354 int offsetInSeconds; | 406 if (days > 0 && days < DAYS_YEAR_2098) { |
355 // According to V8 some library calls have troubles with negative values. | 407 // 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 | 408 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 } | 409 } |
369 int secondsSinceEpoch = _brokenDownDateToSecondsSinceEpoch( | 410 int ms = secondsSinceEpoch * Duration.MILLISECONDS_PER_SECOND; |
370 equivalentYear, month, day, hours, minutes, seconds, isUtc); | 411 return _decomposeIntoYearMonthDay(ms)[0]; |
371 int adjustedSeconds = secondsSinceEpoch + offsetInSeconds; | 412 } |
372 return adjustedSeconds * Duration.MILLISECONDS_PER_SECOND + milliseconds; | 413 |
| 414 /** |
| 415 * Returns a date in seconds that is equivalent to the current date. An |
| 416 * equivalent date has the same fields ([:month:], [:day:], etc.) as the |
| 417 * [this], but the [:year:] is in the range [1970..2037]. |
| 418 * |
| 419 * * The time since the beginning of the year is the same. |
| 420 * * If [this] is in a leap year then the returned seconds are in a leap |
| 421 * year, too. |
| 422 * * The week day of [this] is the same as the one for the returned date. |
| 423 */ |
| 424 static int _equivalentSeconds(int millisecondsSinceEpoch) { |
| 425 final int CUT_OFF_SECONDS = 2100000000; |
| 426 |
| 427 int secondsSinceEpoch = _flooredDivision(millisecondsSinceEpoch, |
| 428 Duration.MILLISECONDS_PER_SECOND); |
| 429 |
| 430 if (secondsSinceEpoch < 0 || secondsSinceEpoch >= CUT_OFF_SECONDS) { |
| 431 int year = _yearsFromSecondsSinceEpoch(secondsSinceEpoch); |
| 432 int days = _dayFromYear(year); |
| 433 int equivalentYear = _equivalentYear(year); |
| 434 int equivalentDays = _dayFromYear(equivalentYear); |
| 435 int diffDays = equivalentDays - days; |
| 436 secondsSinceEpoch += diffDays * Duration.SECONDS_PER_DAY; |
| 437 } |
| 438 return secondsSinceEpoch; |
| 439 } |
| 440 |
| 441 static int _timeZoneOffsetInSeconds(int millisecondsSinceEpoch) { |
| 442 int equivalentSeconds = _equivalentSeconds(millisecondsSinceEpoch); |
| 443 return _timeZoneOffsetInSecondsForClampedSeconds(equivalentSeconds); |
| 444 } |
| 445 |
| 446 static String _timeZoneName(int millisecondsSinceEpoch) { |
| 447 int equivalentSeconds = _equivalentSeconds(millisecondsSinceEpoch); |
| 448 return _timeZoneNameForClampedSeconds(equivalentSeconds); |
373 } | 449 } |
374 | 450 |
375 final bool _isUtc; | 451 final bool _isUtc; |
376 final int value; | 452 final int value; |
377 | 453 |
378 | |
379 // Natives | 454 // 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"; | 455 static int _getCurrentMs() native "DateNatives_currentTimeMillis"; |
385 | 456 |
386 static String _timeZoneName(int secondsSinceEpoch) | 457 static String _timeZoneNameForClampedSeconds(int secondsSinceEpoch) |
387 native "DateNatives_timeZoneName"; | 458 native "DateNatives_timeZoneName"; |
388 | 459 |
389 static int _timeZoneOffsetInSeconds(int secondsSinceEpoch) | 460 static int _timeZoneOffsetInSecondsForClampedSeconds(int secondsSinceEpoch) |
390 native "DateNatives_timeZoneOffsetInSeconds"; | 461 native "DateNatives_timeZoneOffsetInSeconds"; |
391 | 462 |
392 // TODO(floitsch): it would be more efficient if we didn't call the native | 463 static int _localTimeZoneAdjustmentInSeconds() |
393 // function for every member, but cached the broken-down date. | 464 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 } | 465 } |
OLD | NEW |