| OLD | NEW |
| 1 // Copyright (c) 2012, 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 patch class DateImplementation { | 7 class DateImplementation implements Date { |
| 8 /* patch */ DateImplementation(int years, | 8 static final int _MAX_MILLISECONDS_SINCE_EPOCH = 8640000000000000; |
| 9 [int month = 1, | 9 |
| 10 int day = 1, | 10 DateImplementation(int years, |
| 11 int hour = 0, | 11 [int month = 1, |
| 12 int minute = 0, | 12 int day = 1, |
| 13 int second = 0, | 13 int hour = 0, |
| 14 int millisecond = 0, | 14 int minute = 0, |
| 15 bool isUtc = false]) | 15 int second = 0, |
| 16 int millisecond = 0, |
| 17 bool isUtc = false]) |
| 16 : this.isUtc = isUtc, | 18 : this.isUtc = isUtc, |
| 17 this.millisecondsSinceEpoch = _brokenDownDateToMillisecondsSinceEpoch( | 19 this.millisecondsSinceEpoch = _brokenDownDateToMillisecondsSinceEpoch( |
| 18 years, month, day, hour, minute, second, millisecond, isUtc) { | 20 years, month, day, hour, minute, second, millisecond, isUtc) { |
| 19 if (millisecondsSinceEpoch === null) throw new IllegalArgumentException(); | 21 if (millisecondsSinceEpoch === null) throw new IllegalArgumentException(); |
| 20 if (isUtc === null) throw new IllegalArgumentException(); | 22 if (isUtc === null) throw new IllegalArgumentException(); |
| 21 } | 23 } |
| 22 | 24 |
| 23 /* patch */ DateImplementation.now() | 25 DateImplementation.now() |
| 24 : isUtc = false, | 26 : isUtc = false, |
| 25 millisecondsSinceEpoch = _getCurrentMs() { | 27 millisecondsSinceEpoch = _getCurrentMs() { |
| 26 } | 28 } |
| 27 | 29 |
| 28 /* patch */ String get timeZoneName() { | 30 factory DateImplementation.fromString(String formattedString) { |
| 31 // Read in (a subset of) ISO 8601. |
| 32 // Examples: |
| 33 // - "2012-02-27 13:27:00" |
| 34 // - "2012-02-27 13:27:00.423z" |
| 35 // - "20120227 13:27:00" |
| 36 // - "20120227T132700" |
| 37 // - "20120227" |
| 38 // - "2012-02-27T14Z" |
| 39 // - "-123450101 00:00:00 Z" // In the year -12345. |
| 40 final RegExp re = const RegExp( |
| 41 @'^([+-]?\d?\d\d\d\d)-?(\d\d)-?(\d\d)' // The day part. |
| 42 @'(?:[ T](\d\d)(?::?(\d\d)(?::?(\d\d)(.\d{1,6})?)?)? ?([zZ])?)?$'); |
| 43 Match match = re.firstMatch(formattedString); |
| 44 if (match !== null) { |
| 45 int parseIntOrZero(String matched) { |
| 46 // TODO(floitsch): we should not need to test against the empty string. |
| 47 if (matched === null || matched == "") return 0; |
| 48 return Math.parseInt(matched); |
| 49 } |
| 50 |
| 51 double parseDoubleOrZero(String matched) { |
| 52 // TODO(floitsch): we should not need to test against the empty string. |
| 53 if (matched === null || matched == "") return 0.0; |
| 54 return Math.parseDouble(matched); |
| 55 } |
| 56 |
| 57 int years = Math.parseInt(match[1]); |
| 58 int month = Math.parseInt(match[2]); |
| 59 int day = Math.parseInt(match[3]); |
| 60 int hour = parseIntOrZero(match[4]); |
| 61 int minute = parseIntOrZero(match[5]); |
| 62 int second = parseIntOrZero(match[6]); |
| 63 bool addOneMillisecond = false; |
| 64 int millisecond = (parseDoubleOrZero(match[7]) * 1000).round().toInt(); |
| 65 if (millisecond == 1000) { |
| 66 addOneMillisecond = true; |
| 67 millisecond = 999; |
| 68 } |
| 69 // TODO(floitsch): we should not need to test against the empty string. |
| 70 bool isUtc = (match[8] !== null) && (match[8] != ""); |
| 71 int millisecondsSinceEpoch = _brokenDownDateToMillisecondsSinceEpoch( |
| 72 years, month, day, hour, minute, second, millisecond, isUtc); |
| 73 if (millisecondsSinceEpoch === null) { |
| 74 throw new IllegalArgumentException(formattedString); |
| 75 } |
| 76 if (addOneMillisecond) millisecondsSinceEpoch++; |
| 77 return new DateImplementation.fromMillisecondsSinceEpoch( |
| 78 millisecondsSinceEpoch, isUtc); |
| 79 } else { |
| 80 throw new IllegalArgumentException(formattedString); |
| 81 } |
| 82 } |
| 83 |
| 84 DateImplementation.fromMillisecondsSinceEpoch( |
| 85 int this.millisecondsSinceEpoch, [bool isUtc = false]) |
| 86 : this.isUtc = isUtc { |
| 87 if (millisecondsSinceEpoch.abs() > _MAX_MILLISECONDS_SINCE_EPOCH) { |
| 88 throw new IllegalArgumentException(millisecondsSinceEpoch); |
| 89 } |
| 90 if (isUtc === null) { |
| 91 throw new IllegalArgumentException(isUtc); |
| 92 } |
| 93 } |
| 94 |
| 95 bool operator ==(Object other) { |
| 96 if (other is !DateImplementation) return false; |
| 97 DateImplementation otherDate = other; |
| 98 return millisecondsSinceEpoch == otherDate.millisecondsSinceEpoch; |
| 99 } |
| 100 |
| 101 bool operator <(Date other) |
| 102 => millisecondsSinceEpoch < other.millisecondsSinceEpoch; |
| 103 |
| 104 bool operator <=(Date other) |
| 105 => millisecondsSinceEpoch <= other.millisecondsSinceEpoch; |
| 106 |
| 107 bool operator >(Date other) |
| 108 => millisecondsSinceEpoch > other.millisecondsSinceEpoch; |
| 109 |
| 110 bool operator >=(Date other) |
| 111 => millisecondsSinceEpoch >= other.millisecondsSinceEpoch; |
| 112 |
| 113 int compareTo(Date other) |
| 114 => millisecondsSinceEpoch.compareTo(other.millisecondsSinceEpoch); |
| 115 |
| 116 int hashCode() => millisecondsSinceEpoch; |
| 117 |
| 118 Date toLocal() { |
| 119 if (isUtc) { |
| 120 return new DateImplementation.fromMillisecondsSinceEpoch( |
| 121 millisecondsSinceEpoch, false); |
| 122 } |
| 123 return this; |
| 124 } |
| 125 |
| 126 Date toUtc() { |
| 127 if (isUtc) return this; |
| 128 return new DateImplementation.fromMillisecondsSinceEpoch( |
| 129 millisecondsSinceEpoch, true); |
| 130 } |
| 131 |
| 132 String get timeZoneName() { |
| 29 if (isUtc) return "UTC"; | 133 if (isUtc) return "UTC"; |
| 30 return _timeZoneName(millisecondsSinceEpoch); | 134 return _timeZoneName(millisecondsSinceEpoch); |
| 31 } | 135 } |
| 32 | 136 |
| 33 /* patch */ Duration get timeZoneOffset() { | 137 Duration get timeZoneOffset() { |
| 34 if (isUtc) return new Duration(0); | 138 if (isUtc) return new Duration(0); |
| 35 int offsetInSeconds = _timeZoneOffsetInSeconds(millisecondsSinceEpoch); | 139 int offsetInSeconds = _timeZoneOffsetInSeconds(millisecondsSinceEpoch); |
| 36 return new Duration(seconds: offsetInSeconds); | 140 return new Duration(seconds: offsetInSeconds); |
| 37 } | 141 } |
| 38 | 142 |
| 39 /* patch */ int get year() => _decomposeIntoYearMonthDay(_localDateInUtcMs)[0]
; | 143 int get year() { |
| 144 return _decomposeIntoYearMonthDay(_localDateInUtcMs)[0]; |
| 145 } |
| 40 | 146 |
| 41 /* patch */ int get month() => _decomposeIntoYearMonthDay(_localDateInUtcMs)[1
]; | 147 int get month() { |
| 148 return _decomposeIntoYearMonthDay(_localDateInUtcMs)[1]; |
| 149 } |
| 42 | 150 |
| 43 /* patch */ int get day() => _decomposeIntoYearMonthDay(_localDateInUtcMs)[2]; | 151 int get day() { |
| 152 return _decomposeIntoYearMonthDay(_localDateInUtcMs)[2]; |
| 153 } |
| 44 | 154 |
| 45 /* patch */ int get hour() { | 155 int get hour() { |
| 46 int valueInHours = _flooredDivision(_localDateInUtcMs, | 156 int valueInHours = _flooredDivision(_localDateInUtcMs, |
| 47 Duration.MILLISECONDS_PER_HOUR); | 157 Duration.MILLISECONDS_PER_HOUR); |
| 48 return valueInHours % Duration.HOURS_PER_DAY; | 158 return valueInHours % Duration.HOURS_PER_DAY; |
| 49 } | 159 } |
| 50 | 160 |
| 51 /* patch */ int get minute() { | 161 int get minute() { |
| 52 int valueInMinutes = _flooredDivision(_localDateInUtcMs, | 162 int valueInMinutes = _flooredDivision(_localDateInUtcMs, |
| 53 Duration.MILLISECONDS_PER_MINUTE); | 163 Duration.MILLISECONDS_PER_MINUTE); |
| 54 return valueInMinutes % Duration.MINUTES_PER_HOUR; | 164 return valueInMinutes % Duration.MINUTES_PER_HOUR; |
| 55 } | 165 } |
| 56 | 166 |
| 57 /* patch */ int get second() { | 167 int get second() { |
| 58 // Seconds are unaffected by the timezone the user is in. So we can | 168 // Seconds are unaffected by the timezone the user is in. So we can |
| 59 // directly use the millisecondsSinceEpoch and not [_localDateInUtcMs]. | 169 // directly use the millisecondsSinceEpoch and not [_localDateInUtcMs]. |
| 60 int valueInSeconds = | 170 int valueInSeconds = |
| 61 _flooredDivision(millisecondsSinceEpoch, | 171 _flooredDivision(millisecondsSinceEpoch, |
| 62 Duration.MILLISECONDS_PER_SECOND); | 172 Duration.MILLISECONDS_PER_SECOND); |
| 63 return valueInSeconds % Duration.SECONDS_PER_MINUTE; | 173 return valueInSeconds % Duration.SECONDS_PER_MINUTE; |
| 64 } | 174 } |
| 65 | 175 |
| 66 /* patch */ int get millisecond() { | 176 int get millisecond() { |
| 67 // Milliseconds are unaffected by the timezone the user is in. So we can | 177 // Milliseconds are unaffected by the timezone the user is in. So we can |
| 68 // directly use the value and not the [_localDateInUtcValue]. | 178 // directly use the value and not the [_localDateInUtcValue]. |
| 69 return millisecondsSinceEpoch % Duration.MILLISECONDS_PER_SECOND; | 179 return millisecondsSinceEpoch % Duration.MILLISECONDS_PER_SECOND; |
| 70 } | 180 } |
| 71 | 181 |
| 72 /** Returns the weekday of [this]. In accordance with ISO 8601 a week | 182 /** Returns the weekday of [this]. In accordance with ISO 8601 a week |
| 73 * starts with Monday. Monday has the value 1 up to Sunday with 7. */ | 183 * starts with Monday. Monday has the value 1 up to Sunday with 7. */ |
| 74 /* patch */ int get weekday() { | 184 int get weekday() { |
| 75 int daysSince1970 = | 185 int daysSince1970 = |
| 76 _flooredDivision(_localDateInUtcMs, Duration.MILLISECONDS_PER_DAY); | 186 _flooredDivision(_localDateInUtcMs, Duration.MILLISECONDS_PER_DAY); |
| 77 // 1970-1-1 was a Thursday. | 187 // 1970-1-1 was a Thursday. |
| 78 return ((daysSince1970 + Date.THU - Date.MON) % Date.DAYS_IN_WEEK) + | 188 return ((daysSince1970 + Date.THU - Date.MON) % Date.DAYS_IN_WEEK) + |
| 79 Date.MON; | 189 Date.MON; |
| 80 } | 190 } |
| 81 | 191 |
| 192 String toString() { |
| 193 String fourDigits(int n) { |
| 194 int absN = n.abs(); |
| 195 String sign = n < 0 ? "-" : ""; |
| 196 if (absN >= 1000) return "$n"; |
| 197 if (absN >= 100) return "${sign}0$absN"; |
| 198 if (absN >= 10) return "${sign}00$absN"; |
| 199 return "${sign}000$absN"; |
| 200 } |
| 201 String threeDigits(int n) { |
| 202 if (n >= 100) return "${n}"; |
| 203 if (n >= 10) return "0${n}"; |
| 204 return "00${n}"; |
| 205 } |
| 206 String twoDigits(int n) { |
| 207 if (n >= 10) return "${n}"; |
| 208 return "0${n}"; |
| 209 } |
| 210 |
| 211 String y = fourDigits(year); |
| 212 String m = twoDigits(month); |
| 213 String d = twoDigits(day); |
| 214 String h = twoDigits(hour); |
| 215 String min = twoDigits(minute); |
| 216 String sec = twoDigits(second); |
| 217 String ms = threeDigits(millisecond); |
| 218 if (isUtc) { |
| 219 return "$y-$m-$d $h:$min:$sec.${ms}Z"; |
| 220 } else { |
| 221 return "$y-$m-$d $h:$min:$sec.$ms"; |
| 222 } |
| 223 } |
| 224 |
| 225 /** Returns a new [Date] with the [duration] added to [this]. */ |
| 226 Date add(Duration duration) { |
| 227 int ms = millisecondsSinceEpoch; |
| 228 return new DateImplementation.fromMillisecondsSinceEpoch( |
| 229 ms + duration.inMilliseconds, isUtc); |
| 230 } |
| 231 |
| 232 /** Returns a new [Date] with the [duration] subtracted from [this]. */ |
| 233 Date subtract(Duration duration) { |
| 234 int ms = millisecondsSinceEpoch; |
| 235 return new DateImplementation.fromMillisecondsSinceEpoch( |
| 236 ms - duration.inMilliseconds, isUtc); |
| 237 } |
| 238 |
| 239 /** Returns a [Duration] with the difference of [this] and [other]. */ |
| 240 Duration difference(Date other) { |
| 241 int ms = millisecondsSinceEpoch; |
| 242 int otherMs = other.millisecondsSinceEpoch; |
| 243 return new DurationImplementation(milliseconds: ms - otherMs); |
| 244 } |
| 82 | 245 |
| 83 /** The first list contains the days until each month in non-leap years. The | 246 /** The first list contains the days until each month in non-leap years. The |
| 84 * second list contains the days in leap years. */ | 247 * second list contains the days in leap years. */ |
| 85 static final List<List<int>> _DAYS_UNTIL_MONTH = | 248 static final List<List<int>> _DAYS_UNTIL_MONTH = |
| 86 const [const [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334], | 249 const [const [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334], |
| 87 const [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335]]; | 250 const [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335]]; |
| 88 | 251 |
| 89 // Returns the UTC year, month and day for the corresponding | 252 // Returns the UTC year, month and day for the corresponding |
| 90 // [millisecondsSinceEpoch]. | 253 // [millisecondsSinceEpoch]. |
| 91 // Code is adapted from V8. | 254 // Code is adapted from V8. |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 int zoneOffset = | 381 int zoneOffset = |
| 219 _timeZoneOffsetInSeconds(millisecondsSinceEpoch - adjustment); | 382 _timeZoneOffsetInSeconds(millisecondsSinceEpoch - adjustment); |
| 220 millisecondsSinceEpoch -= zoneOffset * Duration.MILLISECONDS_PER_SECOND; | 383 millisecondsSinceEpoch -= zoneOffset * Duration.MILLISECONDS_PER_SECOND; |
| 221 } | 384 } |
| 222 if (millisecondsSinceEpoch.abs() > _MAX_MILLISECONDS_SINCE_EPOCH) { | 385 if (millisecondsSinceEpoch.abs() > _MAX_MILLISECONDS_SINCE_EPOCH) { |
| 223 return null; | 386 return null; |
| 224 } | 387 } |
| 225 return millisecondsSinceEpoch; | 388 return millisecondsSinceEpoch; |
| 226 } | 389 } |
| 227 | 390 |
| 228 static int _weekDay(y) { | |
| 229 // 1/1/1970 was a Thursday. | |
| 230 return (_dayFromYear(y) + 4) % 7; | |
| 231 } | |
| 232 | |
| 233 /** | 391 /** |
| 234 * Returns a year in the range 2008-2035 matching | 392 * Returns a year in the range 2008-2035 matching |
| 235 * * leap year, and | 393 * * leap year, and |
| 236 * * week day of first day. | 394 * * week day of first day. |
| 237 * | 395 * |
| 238 * Leap seconds are ignored. | 396 * Leap seconds are ignored. |
| 239 * Adapted from V8's date implementation. See ECMA 262 - 15.9.1.9. | 397 * Adapted from V8's date implementation. See ECMA 262 - 15.9.1.9. |
| 240 */ | 398 */ |
| 241 static _equivalentYear(int year) { | 399 static _equivalentYear(int year) { |
| 242 // Returns the week day (in range 0 - 6). | 400 // Returns the week day (in range 0 - 6). |
| 401 int weekDay(y) { |
| 402 // 1/1/1970 was a Thursday. |
| 403 return (_dayFromYear(y) + 4) % 7; |
| 404 } |
| 243 // 1/1/1956 was a Sunday (i.e. weekday 0). 1956 was a leap-year. | 405 // 1/1/1956 was a Sunday (i.e. weekday 0). 1956 was a leap-year. |
| 244 // 1/1/1967 was a Sunday (i.e. weekday 0). | 406 // 1/1/1967 was a Sunday (i.e. weekday 0). |
| 245 // Without leap years a subsequent year has a week day + 1 (for example | 407 // Without leap years a subsequent year has a week day + 1 (for example |
| 246 // 1/1/1968 was a Monday). With leap-years it jumps over one week day | 408 // 1/1/1968 was a Monday). With leap-years it jumps over one week day |
| 247 // (e.g. 1/1/1957 was a Tuesday). | 409 // (e.g. 1/1/1957 was a Tuesday). |
| 248 // After 12 years the weekdays have advanced by 12 days + 3 leap days = | 410 // After 12 years the weekdays have advanced by 12 days + 3 leap days = |
| 249 // 15 days. 15 % 7 = 1. So after 12 years the week day has always | 411 // 15 days. 15 % 7 = 1. So after 12 years the week day has always |
| 250 // (now independently of leap-years) advanced by one. | 412 // (now independently of leap-years) advanced by one. |
| 251 // weekDay * 12 gives thus a year starting with the wanted weekDay. | 413 // weekDay * 12 gives thus a year starting with the wanted weekDay. |
| 252 int recentYear = (_isLeapYear(year) ? 1956 : 1967) + (_weekDay(year) * 12); | 414 int recentYear = (_isLeapYear(year) ? 1956 : 1967) + (weekDay(year) * 12); |
| 253 // Close to the year 2008 the calendar cycles every 4 * 7 years (4 for the | 415 // Close to the year 2008 the calendar cycles every 4 * 7 years (4 for the |
| 254 // leap years, 7 for the weekdays). | 416 // leap years, 7 for the weekdays). |
| 255 // Find the year in the range 2008..2037 that is equivalent mod 28. | 417 // Find the year in the range 2008..2037 that is equivalent mod 28. |
| 256 return 2008 + (recentYear - 2008) % 28; | 418 return 2008 + (recentYear - 2008) % 28; |
| 257 } | 419 } |
| 258 | 420 |
| 259 /** | 421 /** |
| 260 * Returns the UTC year for the corresponding [secondsSinceEpoch]. | 422 * Returns the UTC year for the corresponding [secondsSinceEpoch]. |
| 261 * It is relatively fast for values in the range 0 to year 2098. | 423 * It is relatively fast for values in the range 0 to year 2098. |
| 262 * | 424 * |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 static int _timeZoneOffsetInSeconds(int millisecondsSinceEpoch) { | 468 static int _timeZoneOffsetInSeconds(int millisecondsSinceEpoch) { |
| 307 int equivalentSeconds = _equivalentSeconds(millisecondsSinceEpoch); | 469 int equivalentSeconds = _equivalentSeconds(millisecondsSinceEpoch); |
| 308 return _timeZoneOffsetInSecondsForClampedSeconds(equivalentSeconds); | 470 return _timeZoneOffsetInSecondsForClampedSeconds(equivalentSeconds); |
| 309 } | 471 } |
| 310 | 472 |
| 311 static String _timeZoneName(int millisecondsSinceEpoch) { | 473 static String _timeZoneName(int millisecondsSinceEpoch) { |
| 312 int equivalentSeconds = _equivalentSeconds(millisecondsSinceEpoch); | 474 int equivalentSeconds = _equivalentSeconds(millisecondsSinceEpoch); |
| 313 return _timeZoneNameForClampedSeconds(equivalentSeconds); | 475 return _timeZoneNameForClampedSeconds(equivalentSeconds); |
| 314 } | 476 } |
| 315 | 477 |
| 478 final bool isUtc; |
| 479 final int millisecondsSinceEpoch; |
| 480 |
| 316 // Natives | 481 // Natives |
| 317 static int _getCurrentMs() native "DateNatives_currentTimeMillis"; | 482 static int _getCurrentMs() native "DateNatives_currentTimeMillis"; |
| 318 | 483 |
| 319 static String _timeZoneNameForClampedSeconds(int secondsSinceEpoch) | 484 static String _timeZoneNameForClampedSeconds(int secondsSinceEpoch) |
| 320 native "DateNatives_timeZoneName"; | 485 native "DateNatives_timeZoneName"; |
| 321 | 486 |
| 322 static int _timeZoneOffsetInSecondsForClampedSeconds(int secondsSinceEpoch) | 487 static int _timeZoneOffsetInSecondsForClampedSeconds(int secondsSinceEpoch) |
| 323 native "DateNatives_timeZoneOffsetInSeconds"; | 488 native "DateNatives_timeZoneOffsetInSeconds"; |
| 324 | 489 |
| 325 static int _localTimeZoneAdjustmentInSeconds() | 490 static int _localTimeZoneAdjustmentInSeconds() |
| 326 native "DateNatives_localTimeZoneAdjustmentInSeconds"; | 491 native "DateNatives_localTimeZoneAdjustmentInSeconds"; |
| 327 } | 492 } |
| OLD | NEW |