| 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 _MAX_VALUE = 8640000000000000; | 8 static final int _SECONDS_YEAR_2035 = 2051222400; |
| 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 } | |
| 86 | 84 |
| 87 bool operator ==(Object other) { | 85 bool operator ==(Object other) { |
| 88 if (other is !DateImplementation) return false; | 86 if (other is !DateImplementation) return false; |
| 89 DateImplementation otherDate = other; | 87 return value == other.value; |
| 90 return value == otherDate.value; | |
| 91 } | 88 } |
| 92 | 89 |
| 93 bool operator <(Date other) => value < other.value; | 90 bool operator <(Date other) => value < other.value; |
| 94 | 91 |
| 95 bool operator <=(Date other) => value <= other.value; | 92 bool operator <=(Date other) => value <= other.value; |
| 96 | 93 |
| 97 bool operator >(Date other) => value > other.value; | 94 bool operator >(Date other) => value > other.value; |
| 98 | 95 |
| 99 bool operator >=(Date other) => value >= other.value; | 96 bool operator >=(Date other) => value >= other.value; |
| 100 | 97 |
| 101 int compareTo(Date other) => value.compareTo(other.value); | 98 int compareTo(Date other) => value.compareTo(other.value); |
| 102 int hashCode() => value; | 99 int hashCode() => value; |
| 103 | 100 |
| 104 Date toLocal() { | 101 Date toLocal() { |
| 105 if (isUtc()) return new DateImplementation.fromEpoch(value, false); | 102 if (isUtc()) return new DateImplementation.fromEpoch(value, false); |
| 106 return this; | 103 return this; |
| 107 } | 104 } |
| 108 | 105 |
| 109 Date toUtc() { | 106 Date toUtc() { |
| 110 if (isUtc()) return this; | 107 if (isUtc()) return this; |
| 111 return new DateImplementation.fromEpoch(value, true); | 108 return new DateImplementation.fromEpoch(value, true); |
| 112 } | 109 } |
| 113 | 110 |
| 114 String get timeZoneName() { | 111 String get timeZoneName() { |
| 115 if (isUtc()) return "UTC"; | 112 if (isUtc()) return "UTC"; |
| 116 return _timeZoneName(value); | 113 return _timeZoneName(_equivalentSeconds(_secondsSinceEpoch)); |
| 117 } | 114 } |
| 118 | 115 |
| 119 Duration get timeZoneOffset() { | 116 Duration get timeZoneOffset() { |
| 120 if (isUtc()) return new Duration(0); | 117 if (isUtc()) return new Duration(0); |
| 121 int offsetInSeconds = _timeZoneOffsetInSeconds(value); | 118 int offsetInSeconds = |
| 119 _timeZoneOffsetInSeconds(_equivalentSeconds(_secondsSinceEpoch)); |
| 122 return new Duration(seconds: offsetInSeconds); | 120 return new Duration(seconds: offsetInSeconds); |
| 123 } | 121 } |
| 124 | 122 |
| 125 int get year() { | 123 int get year() { |
| 126 return _decomposeIntoYearMonthDay(_localDateInUtcValue)[0]; | 124 int secondsSinceEpoch = _secondsSinceEpoch; |
| 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); |
| 127 } | 136 } |
| 128 | 137 |
| 129 int get month() { | 138 int get month() { |
| 130 return _decomposeIntoYearMonthDay(_localDateInUtcValue)[1]; | 139 return _getMonth(_equivalentSeconds(_secondsSinceEpoch), isUtc()); |
| 131 } | 140 } |
| 132 | 141 |
| 133 int get day() { | 142 int get day() { |
| 134 return _decomposeIntoYearMonthDay(_localDateInUtcValue)[2]; | 143 return _getDay(_equivalentSeconds(_secondsSinceEpoch), isUtc()); |
| 135 } | 144 } |
| 136 | 145 |
| 137 int get hours() { | 146 int get hours() { |
| 138 int valueInHours = _flooredDivision(_localDateInUtcValue, | 147 return _getHours(_equivalentSeconds(_secondsSinceEpoch), isUtc()); |
| 139 Duration.MILLISECONDS_PER_HOUR); | |
| 140 return valueInHours % Duration.HOURS_PER_DAY; | |
| 141 } | 148 } |
| 142 | 149 |
| 143 int get minutes() { | 150 int get minutes() { |
| 144 int valueInMinutes = _flooredDivision(_localDateInUtcValue, | 151 return _getMinutes(_equivalentSeconds(_secondsSinceEpoch), isUtc()); |
| 145 Duration.MILLISECONDS_PER_MINUTE); | |
| 146 return valueInMinutes % Duration.MINUTES_PER_HOUR; | |
| 147 } | 152 } |
| 148 | 153 |
| 149 int get seconds() { | 154 int get seconds() { |
| 150 // Seconds are unaffected by the timezone the user is in. So we can | 155 return _getSeconds(_equivalentSeconds(_secondsSinceEpoch), isUtc()); |
| 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; | |
| 155 } | 156 } |
| 156 | 157 |
| 157 int get milliseconds() { | 158 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]. | |
| 160 return value % Duration.MILLISECONDS_PER_SECOND; | 159 return value % Duration.MILLISECONDS_PER_SECOND; |
| 161 } | 160 } |
| 162 | 161 |
| 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 |
| 163 int get weekday() { | 171 int get weekday() { |
| 164 int daysSince1970 = | 172 final Date unixTimeStart = new Date(1970, 1, 1, 0, 0, 0, 0, isUtc()); |
| 165 _flooredDivision(_localDateInUtcValue, Duration.MILLISECONDS_PER_DAY); | 173 int msSince1970 = this.difference(unixTimeStart).inMilliseconds; |
| 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 } |
| 166 // 1970-1-1 was a Thursday. | 186 // 1970-1-1 was a Thursday. |
| 167 return ((daysSince1970 + Date.THU) % Date.DAYS_IN_WEEK); | 187 return ((daysSince1970 + Date.THU) % Date.DAYS_IN_WEEK); |
| 168 } | 188 } |
| 169 | 189 |
| 170 bool isUtc() => _isUtc; | 190 bool isUtc() => _isUtc; |
| 171 | 191 |
| 172 String toString() { | 192 String toString() { |
| 173 String fourDigits(int n) { | 193 String fourDigits(int n) { |
| 174 int absN = n.abs(); | 194 int absN = n.abs(); |
| 175 String sign = n < 0 ? "-" : ""; | 195 String sign = n < 0 ? "-" : ""; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 195 String min = twoDigits(minutes); | 215 String min = twoDigits(minutes); |
| 196 String sec = twoDigits(seconds); | 216 String sec = twoDigits(seconds); |
| 197 String ms = threeDigits(milliseconds); | 217 String ms = threeDigits(milliseconds); |
| 198 if (isUtc()) { | 218 if (isUtc()) { |
| 199 return "$y-$m-$d $h:$min:$sec.${ms}Z"; | 219 return "$y-$m-$d $h:$min:$sec.${ms}Z"; |
| 200 } else { | 220 } else { |
| 201 return "$y-$m-$d $h:$min:$sec.$ms"; | 221 return "$y-$m-$d $h:$min:$sec.$ms"; |
| 202 } | 222 } |
| 203 } | 223 } |
| 204 | 224 |
| 205 /** Returns a new [Date] with the [duration] added to [this]. */ | 225 // Adds the [duration] to this Date instance. |
| 206 Date add(Duration duration) { | 226 Date add(Duration duration) { |
| 207 return new DateImplementation.fromEpoch(value + duration.inMilliseconds, | 227 return new DateImplementation.fromEpoch(value + duration.inMilliseconds, |
| 208 isUtc()); | 228 isUtc()); |
| 209 } | 229 } |
| 210 | 230 |
| 211 /** Returns a new [Date] with the [duration] subtracted from [this]. */ | 231 // Subtracts the [duration] from this Date instance. |
| 212 Date subtract(Duration duration) { | 232 Date subtract(Duration duration) { |
| 213 return new DateImplementation.fromEpoch(value - duration.inMilliseconds, | 233 return new DateImplementation.fromEpoch(value - duration.inMilliseconds, |
| 214 isUtc()); | 234 isUtc()); |
| 215 } | 235 } |
| 216 | 236 |
| 217 /** Returns a [Duration] with the difference of [this] and [other]. */ | 237 // Returns a [Duration] with the difference of [this] and [other]. |
| 218 Duration difference(Date other) { | 238 Duration difference(Date other) { |
| 219 return new DurationImplementation(milliseconds: value - other.value); | 239 return new DurationImplementation(milliseconds: value - other.value); |
| 220 } | 240 } |
| 221 | 241 |
| 222 /** The first list contains the days until each month in non-leap years. The | 242 // Returns the UTC year for the corresponding [secondsSinceEpoch]. |
| 223 * second list contains the days in leap years. */ | 243 // It is relatively fast for values in the range 0 to year 2098. |
| 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]. | |
| 230 // Code is adapted from V8. | 244 // Code is adapted from V8. |
| 231 static List<int> _decomposeIntoYearMonthDay(int millisecondsSinceEpoch) { | 245 static int _yearsFromSecondsSinceEpoch(int secondsSinceEpoch) { |
| 232 // TODO(floitsch): cache result. | |
| 233 final int DAYS_IN_4_YEARS = 4 * 365 + 1; | 246 final int DAYS_IN_4_YEARS = 4 * 365 + 1; |
| 234 final int DAYS_IN_100_YEARS = 25 * DAYS_IN_4_YEARS - 1; | 247 final int DAYS_IN_100_YEARS = 25 * DAYS_IN_4_YEARS - 1; |
| 235 final int DAYS_IN_400_YEARS = 4 * DAYS_IN_100_YEARS + 1; | 248 final int DAYS_IN_400_YEARS = 4 * DAYS_IN_100_YEARS + 1; |
| 236 final int DAYS_1970_TO_2000 = 30 * 365 + 7; | 249 final int DAYS_1970_TO_2000 = 30 * 365 + 7; |
| 237 final int DAYS_OFFSET = 1000 * DAYS_IN_400_YEARS + 5 * DAYS_IN_400_YEARS - | 250 final int DAYS_OFFSET = 1000 * DAYS_IN_400_YEARS + 5 * DAYS_IN_400_YEARS - |
| 238 DAYS_1970_TO_2000; | 251 DAYS_1970_TO_2000; |
| 239 final int YEARS_OFFSET = 400000; | 252 final int YEARS_OFFSET = 400000; |
| 253 final int DAYS_YEAR_2098 = DAYS_IN_100_YEARS + 6 * DAYS_IN_4_YEARS; |
| 240 | 254 |
| 241 int resultYear = 0; | 255 int days = secondsSinceEpoch ~/ Duration.SECONDS_PER_DAY; |
| 242 int resultMonth = 0; | 256 if (days > 0 && days < DAYS_YEAR_2098) { |
| 243 int resultDay = 0; | 257 // According to V8 this fast case works for dates from 1970 to 2099. |
| 244 | 258 return 1970 + (4 * days + 2) ~/ DAYS_IN_4_YEARS; |
| 245 // Always round down. | 259 } else { |
| 246 int days = _flooredDivision(millisecondsSinceEpoch, | 260 days += DAYS_OFFSET; |
| 247 Duration.MILLISECONDS_PER_DAY); | 261 int result = 400 * (days ~/ DAYS_IN_400_YEARS) - YEARS_OFFSET; |
| 248 days += DAYS_OFFSET; | 262 days = days.remainder(DAYS_IN_400_YEARS); |
| 249 resultYear = 400 * (days ~/ DAYS_IN_400_YEARS) - YEARS_OFFSET; | 263 days--; |
| 250 days = days.remainder(DAYS_IN_400_YEARS); | 264 int yd1 = days ~/ DAYS_IN_100_YEARS; |
| 251 days--; | 265 days = days.remainder(DAYS_IN_100_YEARS); |
| 252 int yd1 = days ~/ DAYS_IN_100_YEARS; | 266 result += 100 * yd1; |
| 253 days = days.remainder(DAYS_IN_100_YEARS); | 267 days++; |
| 254 resultYear += 100 * yd1; | 268 int yd2 = days ~/ DAYS_IN_4_YEARS; |
| 255 days++; | 269 days = days.remainder(DAYS_IN_4_YEARS); |
| 256 int yd2 = days ~/ DAYS_IN_4_YEARS; | 270 result += 4 * yd2; |
| 257 days = days.remainder(DAYS_IN_4_YEARS); | 271 days--; |
| 258 resultYear += 4 * yd2; | 272 int yd3 = days ~/ 365; |
| 259 days--; | 273 days = days.remainder(365); |
| 260 int yd3 = days ~/ 365; | 274 result += yd3; |
| 261 days = days.remainder(365); | 275 return result; |
| 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. | |
| 272 } | 276 } |
| 273 resultDay = days - daysUntilMonth[resultMonth - 1] + 1; | |
| 274 return <int>[resultYear, resultMonth, resultDay]; | |
| 275 } | 277 } |
| 276 | 278 |
| 277 /** | 279 // Given [secondsSinceEpoch] returns seconds such that they are at the same |
| 278 * Returns the amount of milliseconds in UTC that represent the same values as | 280 // time in an equivalent year (see [_equivalentYear]). |
| 279 * [this]. | 281 // Leap seconds are ignored. |
| 280 * | 282 static int _equivalentSeconds(int secondsSinceEpoch) { |
| 281 * Say [:t:] is the result of this function, then | 283 if (secondsSinceEpoch >= 0 && secondsSinceEpoch < _SECONDS_YEAR_2035) { |
| 282 * * [:this.year == new Date.fromEpoch(t, isUtc: true).year:], | 284 return secondsSinceEpoch; |
| 283 * * [:this.month == new Date.fromEpoch(t, isUtc: true).month:], | 285 } |
| 284 * * [:this.day == new Date.fromEpoch(t, isUtc: true).day:], | 286 int year = _yearsFromSecondsSinceEpoch(secondsSinceEpoch); |
| 285 * * [:this.hours == new Date.fromEpoch(t, isUtc: true).hours:], | 287 int days = _dayFromYear(year); |
| 286 * * ... | 288 int equivalentYear = _equivalentYear(year); |
| 287 * | 289 int equivalentDays = _dayFromYear(equivalentYear); |
| 288 * Daylight savings is computed as if the date was computed in [1970..2037]. | 290 int diffDays = equivalentDays - days; |
| 289 * If [this] lies outside this range then it is a year with similar properties | 291 return secondsSinceEpoch + diffDays * Duration.SECONDS_PER_DAY; |
| 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; | |
| 301 } | 292 } |
| 302 | 293 |
| 303 // Returns the days since 1970 for the start of the given [year]. | 294 // Returns the days since 1970 for the start of the given [year]. |
| 304 // [year] may be before epoch. | 295 // [year] may be before epoch. |
| 305 static int _dayFromYear(int year) { | 296 static int _dayFromYear(int year) { |
| 297 int flooredDivision(int a, int b) { |
| 298 return (a - (a < 0 ? b - 1 : 0)) ~/ b; |
| 299 } |
| 300 |
| 306 return 365 * (year - 1970) | 301 return 365 * (year - 1970) |
| 307 + _flooredDivision(year - 1969, 4) | 302 + flooredDivision(year - 1969, 4) |
| 308 - _flooredDivision(year - 1901, 100) | 303 - flooredDivision(year - 1901, 100) |
| 309 + _flooredDivision(year - 1601, 400); | 304 + flooredDivision(year - 1601, 400); |
| 310 } | 305 } |
| 311 | 306 |
| 312 static bool _isLeapYear(y) { | 307 // Returns a year in the range 2008-2035 matching |
| 313 return (y.remainder(4) == 0) && | 308 // - leap year, and |
| 314 ((y.remainder(100) != 0) || (y.remainder(400) == 0)); | 309 // - week day of first day. |
| 315 } | 310 // Leap seconds are ignored. |
| 316 | 311 // Adapted from V8's date implementation. See ECMA 262 - 15.9.1.9. |
| 317 static _brokenDownDateToMillisecondsSinceEpoch( | 312 static _equivalentYear(int year) { |
| 318 int years, int month, int day, | 313 // Returns 1 if in leap year. 0 otherwise. |
| 319 int hours, int minutes, int seconds, int milliseconds, | 314 bool inLeapYear(year) { |
| 320 bool isUtc) { | 315 return (year.remainder(4) == 0) && |
| 321 if ((month < 1) || (month > 12)) return null; | 316 ((year.remainder(100) != 0) || (year.remainder(400) == 0)); |
| 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; | |
| 349 } | 317 } |
| 350 | 318 |
| 351 if (!isUtc) { | |
| 352 // Note that we need to add the local timezone adjustement before asking | |
| 353 // for the correct zone offset. | |
| 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; | |
| 362 } | |
| 363 | |
| 364 /** | |
| 365 * Returns a year in the range 2008-2035 matching | |
| 366 * * leap year, and | |
| 367 * * week day of first day. | |
| 368 * | |
| 369 * Leap seconds are ignored. | |
| 370 * Adapted from V8's date implementation. See ECMA 262 - 15.9.1.9. | |
| 371 */ | |
| 372 static _equivalentYear(int year) { | |
| 373 // Returns the week day (in range 0 - 6). | 319 // Returns the week day (in range 0 - 6). |
| 374 int weekDay(y) { | 320 int weekDay(year) { |
| 375 // 1/1/1970 was a Thursday. | 321 // 1/1/1970 was a Thursday. |
| 376 return (_dayFromYear(y) + 4) % 7; | 322 return (_dayFromYear(year) + 4) % 7; |
| 377 } | 323 } |
| 378 // 1/1/1956 was a Sunday (i.e. weekday 0). 1956 was a leap-year. | 324 // 1/1/1956 was a Sunday (i.e. weekday 0). 1956 was a leap-year. |
| 379 // 1/1/1967 was a Sunday (i.e. weekday 0). | 325 // 1/1/1967 was a Sunday (i.e. weekday 0). |
| 380 // Without leap years a subsequent year has a week day + 1 (for example | 326 // Without leap years a subsequent year has a week day + 1 (for example |
| 381 // 1/1/1968 was a Monday). With leap-years it jumps over one week day | 327 // 1/1/1968 was a Monday). With leap-years it jumps over one week day |
| 382 // (e.g. 1/1/1957 was a Tuesday). | 328 // (e.g. 1/1/1957 was a Tuesday). |
| 383 // After 12 years the weekdays have advanced by 12 days + 3 leap days = | 329 // After 12 years the weekdays have advanced by 12 days + 3 leap days = |
| 384 // 15 days. 15 % 7 = 1. So after 12 years the week day has always | 330 // 15 days. 15 % 7 = 1. So after 12 years the week day has always |
| 385 // (now independently of leap-years) advanced by one. | 331 // (now independently of leap-years) advanced by one. |
| 386 // weekDay * 12 gives thus a year starting with the wanted weekDay. | 332 // weekDay * 12 gives thus a year starting with the wanted weekDay. |
| 387 int recentYear = (_isLeapYear(year) ? 1956 : 1967) + (weekDay(year) * 12); | 333 int recentYear = (inLeapYear(year) ? 1956 : 1967) + (weekDay(year) * 12); |
| 388 // Close to the year 2008 the calendar cycles every 4 * 7 years (4 for the | 334 // Close to the year 2008 the calendar cycles every 4 * 7 years (4 for the |
| 389 // leap years, 7 for the weekdays). | 335 // leap years, 7 for the weekdays). |
| 390 // Find the year in the range 2008..2037 that is equivalent mod 28. | 336 // Find the year in the range 2008..2037 that is equivalent mod 28. |
| 391 return 2008 + (recentYear - 2008) % 28; | 337 return 2008 + (recentYear - 2008) % 28; |
| 392 } | 338 } |
| 393 | 339 |
| 394 /** | 340 static _brokenDownDateToMillisecondsSinceEpoch( |
| 395 * Returns the UTC year for the corresponding [secondsSinceEpoch]. | 341 int years, int month, int day, |
| 396 * It is relatively fast for values in the range 0 to year 2098. | 342 int hours, int minutes, int seconds, int milliseconds, |
| 397 * | 343 bool isUtc) { |
| 398 * Code is adapted from V8. | 344 if ((month < 1) || (month > 12)) return null; |
| 399 */ | 345 if ((day < 1) || (day > 31)) return null; |
| 400 static int _yearsFromSecondsSinceEpoch(int secondsSinceEpoch) { | 346 // Leap seconds can lead to hours == 24. |
| 401 final int DAYS_IN_4_YEARS = 4 * 365 + 1; | 347 if ((hours < 0) || (hours > 24)) return null; |
| 402 final int DAYS_IN_100_YEARS = 25 * DAYS_IN_4_YEARS - 1; | 348 if ((hours == 24) && ((minutes != 0) || (seconds != 0))) return null; |
| 403 final int DAYS_YEAR_2098 = DAYS_IN_100_YEARS + 6 * DAYS_IN_4_YEARS; | 349 if ((minutes < 0) || (minutes > 59)) return null; |
| 350 if ((seconds < 0) || (seconds > 59)) return null; |
| 351 if ((milliseconds < 0) || (milliseconds > 999)) return null; |
| 404 | 352 |
| 405 int days = secondsSinceEpoch ~/ Duration.SECONDS_PER_DAY; | 353 int equivalentYear; |
| 406 if (days > 0 && days < DAYS_YEAR_2098) { | 354 int offsetInSeconds; |
| 407 // According to V8 this fast case works for dates from 1970 to 2099. | 355 // According to V8 some library calls have troubles with negative values. |
| 408 return 1970 + (4 * days + 2) ~/ DAYS_IN_4_YEARS; | 356 // Therefore clamp to 1970 - year 2035 (which is less than the size of |
| 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; |
| 409 } | 368 } |
| 410 int ms = secondsSinceEpoch * Duration.MILLISECONDS_PER_SECOND; | 369 int secondsSinceEpoch = _brokenDownDateToSecondsSinceEpoch( |
| 411 return _decomposeIntoYearMonthDay(ms)[0]; | 370 equivalentYear, month, day, hours, minutes, seconds, isUtc); |
| 412 } | 371 int adjustedSeconds = secondsSinceEpoch + offsetInSeconds; |
| 413 | 372 return adjustedSeconds * Duration.MILLISECONDS_PER_SECOND + milliseconds; |
| 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); | |
| 449 } | 373 } |
| 450 | 374 |
| 451 final bool _isUtc; | 375 final bool _isUtc; |
| 452 final int value; | 376 final int value; |
| 453 | 377 |
| 378 |
| 454 // Natives | 379 // Natives |
| 380 static _brokenDownDateToSecondsSinceEpoch( |
| 381 int years, int month, int day, int hours, int minutes, int seconds, |
| 382 bool isUtc) native "DateNatives_brokenDownToSecondsSinceEpoch"; |
| 383 |
| 455 static int _getCurrentMs() native "DateNatives_currentTimeMillis"; | 384 static int _getCurrentMs() native "DateNatives_currentTimeMillis"; |
| 456 | 385 |
| 457 static String _timeZoneNameForClampedSeconds(int secondsSinceEpoch) | 386 static String _timeZoneName(int secondsSinceEpoch) |
| 458 native "DateNatives_timeZoneName"; | 387 native "DateNatives_timeZoneName"; |
| 459 | 388 |
| 460 static int _timeZoneOffsetInSecondsForClampedSeconds(int secondsSinceEpoch) | 389 static int _timeZoneOffsetInSeconds(int secondsSinceEpoch) |
| 461 native "DateNatives_timeZoneOffsetInSeconds"; | 390 native "DateNatives_timeZoneOffsetInSeconds"; |
| 462 | 391 |
| 463 static int _localTimeZoneAdjustmentInSeconds() | 392 // TODO(floitsch): it would be more efficient if we didn't call the native |
| 464 native "DateNatives_localTimeZoneAdjustmentInSeconds"; | 393 // function for every member, but cached the broken-down date. |
| 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"; |
| 465 } | 411 } |
| OLD | NEW |