| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 // Dart core library. | 4 // Dart core library. |
| 5 | 5 |
| 6 // VM implementation of DateTime. | 6 // VM implementation of DateTime. |
| 7 patch class DateTime { | 7 @patch class DateTime { |
| 8 // Natives. | 8 // Natives. |
| 9 // The natives have been moved up here to work around Issue 10401. | 9 // The natives have been moved up here to work around Issue 10401. |
| 10 static int _getCurrentMicros() native "DateTime_currentTimeMicros"; | 10 static int _getCurrentMicros() native "DateTime_currentTimeMicros"; |
| 11 | 11 |
| 12 static String _timeZoneNameForClampedSeconds(int secondsSinceEpoch) | 12 static String _timeZoneNameForClampedSeconds(int secondsSinceEpoch) |
| 13 native "DateTime_timeZoneName"; | 13 native "DateTime_timeZoneName"; |
| 14 | 14 |
| 15 static int _timeZoneOffsetInSecondsForClampedSeconds(int secondsSinceEpoch) | 15 static int _timeZoneOffsetInSecondsForClampedSeconds(int secondsSinceEpoch) |
| 16 native "DateTime_timeZoneOffsetInSeconds"; | 16 native "DateTime_timeZoneOffsetInSeconds"; |
| 17 | 17 |
| 18 static int _localTimeZoneAdjustmentInSeconds() | 18 static int _localTimeZoneAdjustmentInSeconds() |
| 19 native "DateTime_localTimeZoneAdjustmentInSeconds"; | 19 native "DateTime_localTimeZoneAdjustmentInSeconds"; |
| 20 | 20 |
| 21 static const _MICROSECOND_INDEX = 0; | 21 static const _MICROSECOND_INDEX = 0; |
| 22 static const _MILLISECOND_INDEX = 1; | 22 static const _MILLISECOND_INDEX = 1; |
| 23 static const _SECOND_INDEX = 2; | 23 static const _SECOND_INDEX = 2; |
| 24 static const _MINUTE_INDEX = 3; | 24 static const _MINUTE_INDEX = 3; |
| 25 static const _HOUR_INDEX = 4; | 25 static const _HOUR_INDEX = 4; |
| 26 static const _DAY_INDEX = 5; | 26 static const _DAY_INDEX = 5; |
| 27 static const _WEEKDAY_INDEX = 6; | 27 static const _WEEKDAY_INDEX = 6; |
| 28 static const _MONTH_INDEX = 7; | 28 static const _MONTH_INDEX = 7; |
| 29 static const _YEAR_INDEX = 8; | 29 static const _YEAR_INDEX = 8; |
| 30 | 30 |
| 31 List __parts; | 31 List __parts; |
| 32 | 32 |
| 33 /* patch */ DateTime.fromMillisecondsSinceEpoch(int millisecondsSinceEpoch, | 33 /* @patch */ DateTime.fromMillisecondsSinceEpoch(int millisecondsSinceEpoch, |
| 34 {bool isUtc: false}) | 34 {bool isUtc: false}) |
| 35 : this._withValue( | 35 : this._withValue( |
| 36 millisecondsSinceEpoch * Duration.MICROSECONDS_PER_MILLISECOND, | 36 millisecondsSinceEpoch * Duration.MICROSECONDS_PER_MILLISECOND, |
| 37 isUtc: isUtc); | 37 isUtc: isUtc); |
| 38 | 38 |
| 39 /* patch */ DateTime.fromMicrosecondsSinceEpoch(int microsecondsSinceEpoch, | 39 /* @patch */ DateTime.fromMicrosecondsSinceEpoch(int microsecondsSinceEpoch, |
| 40 {bool isUtc: false}) | 40 {bool isUtc: false}) |
| 41 : this._withValue(microsecondsSinceEpoch, isUtc: isUtc); | 41 : this._withValue(microsecondsSinceEpoch, isUtc: isUtc); |
| 42 | 42 |
| 43 /* patch */ DateTime._internal(int year, | 43 /* @patch */ DateTime._internal(int year, |
| 44 int month, | 44 int month, |
| 45 int day, | 45 int day, |
| 46 int hour, | 46 int hour, |
| 47 int minute, | 47 int minute, |
| 48 int second, | 48 int second, |
| 49 int millisecond, | 49 int millisecond, |
| 50 int microsecond, | 50 int microsecond, |
| 51 bool isUtc) | 51 bool isUtc) |
| 52 : this.isUtc = isUtc, | 52 : this.isUtc = isUtc, |
| 53 this._value = _brokenDownDateToValue( | 53 this._value = _brokenDownDateToValue( |
| 54 year, month, day, hour, minute, second, millisecond, microsecond, | 54 year, month, day, hour, minute, second, millisecond, microsecond, |
| 55 isUtc) { | 55 isUtc) { |
| 56 if (_value == null) throw new ArgumentError(); | 56 if (_value == null) throw new ArgumentError(); |
| 57 if (isUtc == null) throw new ArgumentError(); | 57 if (isUtc == null) throw new ArgumentError(); |
| 58 } | 58 } |
| 59 | 59 |
| 60 /* patch */ DateTime._now() | 60 /* @patch */ DateTime._now() |
| 61 : isUtc = false, | 61 : isUtc = false, |
| 62 _value = _getCurrentMicros() { | 62 _value = _getCurrentMicros() { |
| 63 } | 63 } |
| 64 | 64 |
| 65 /* patch */ String get timeZoneName { | 65 /* @patch */ String get timeZoneName { |
| 66 if (isUtc) return "UTC"; | 66 if (isUtc) return "UTC"; |
| 67 return _timeZoneName(microsecondsSinceEpoch); | 67 return _timeZoneName(microsecondsSinceEpoch); |
| 68 } | 68 } |
| 69 | 69 |
| 70 /* patch */ Duration get timeZoneOffset { | 70 /* @patch */ Duration get timeZoneOffset { |
| 71 if (isUtc) return new Duration(); | 71 if (isUtc) return new Duration(); |
| 72 int offsetInSeconds = _timeZoneOffsetInSeconds(microsecondsSinceEpoch); | 72 int offsetInSeconds = _timeZoneOffsetInSeconds(microsecondsSinceEpoch); |
| 73 return new Duration(seconds: offsetInSeconds); | 73 return new Duration(seconds: offsetInSeconds); |
| 74 } | 74 } |
| 75 | 75 |
| 76 /** The first list contains the days until each month in non-leap years. The | 76 /** The first list contains the days until each month in non-leap years. The |
| 77 * second list contains the days in leap years. */ | 77 * second list contains the days in leap years. */ |
| 78 static const List<List<int>> _DAYS_UNTIL_MONTH = | 78 static const List<List<int>> _DAYS_UNTIL_MONTH = |
| 79 const [const [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334], | 79 const [const [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334], |
| 80 const [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335]]; | 80 const [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335]]; |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 return list; | 158 return list; |
| 159 } | 159 } |
| 160 | 160 |
| 161 get _parts { | 161 get _parts { |
| 162 if (__parts == null) { | 162 if (__parts == null) { |
| 163 __parts = _computeUpperPart(_localDateInUtcMicros); | 163 __parts = _computeUpperPart(_localDateInUtcMicros); |
| 164 } | 164 } |
| 165 return __parts; | 165 return __parts; |
| 166 } | 166 } |
| 167 | 167 |
| 168 /* patch */ DateTime add(Duration duration) { | 168 /* @patch */ DateTime add(Duration duration) { |
| 169 return new DateTime._withValue( | 169 return new DateTime._withValue( |
| 170 _value + duration.inMicroseconds, isUtc: isUtc); | 170 _value + duration.inMicroseconds, isUtc: isUtc); |
| 171 } | 171 } |
| 172 | 172 |
| 173 /* patch */ DateTime subtract(Duration duration) { | 173 /* @patch */ DateTime subtract(Duration duration) { |
| 174 return new DateTime._withValue( | 174 return new DateTime._withValue( |
| 175 _value - duration.inMicroseconds, isUtc: isUtc); | 175 _value - duration.inMicroseconds, isUtc: isUtc); |
| 176 } | 176 } |
| 177 | 177 |
| 178 /* patch */ Duration difference(DateTime other) { | 178 /* @patch */ Duration difference(DateTime other) { |
| 179 return new Duration(microseconds: _value - other._value); | 179 return new Duration(microseconds: _value - other._value); |
| 180 } | 180 } |
| 181 | 181 |
| 182 /* patch */ int get millisecondsSinceEpoch => | 182 /* @patch */ int get millisecondsSinceEpoch => |
| 183 _value ~/ Duration.MICROSECONDS_PER_MILLISECOND; | 183 _value ~/ Duration.MICROSECONDS_PER_MILLISECOND; |
| 184 | 184 |
| 185 /* patch */ int get microsecondsSinceEpoch => _value; | 185 /* @patch */ int get microsecondsSinceEpoch => _value; |
| 186 | 186 |
| 187 /* patch */ int get microsecond => _parts[_MICROSECOND_INDEX]; | 187 /* @patch */ int get microsecond => _parts[_MICROSECOND_INDEX]; |
| 188 | 188 |
| 189 /* patch */ int get millisecond => _parts[_MILLISECOND_INDEX]; | 189 /* @patch */ int get millisecond => _parts[_MILLISECOND_INDEX]; |
| 190 | 190 |
| 191 /* patch */ int get second => _parts[_SECOND_INDEX]; | 191 /* @patch */ int get second => _parts[_SECOND_INDEX]; |
| 192 | 192 |
| 193 /* patch */ int get minute => _parts[_MINUTE_INDEX]; | 193 /* @patch */ int get minute => _parts[_MINUTE_INDEX]; |
| 194 | 194 |
| 195 /* patch */ int get hour => _parts[_HOUR_INDEX]; | 195 /* @patch */ int get hour => _parts[_HOUR_INDEX]; |
| 196 | 196 |
| 197 /* patch */ int get day => _parts[_DAY_INDEX]; | 197 /* @patch */ int get day => _parts[_DAY_INDEX]; |
| 198 | 198 |
| 199 /* patch */ int get weekday => _parts[_WEEKDAY_INDEX]; | 199 /* @patch */ int get weekday => _parts[_WEEKDAY_INDEX]; |
| 200 | 200 |
| 201 /* patch */ int get month => _parts[_MONTH_INDEX]; | 201 /* @patch */ int get month => _parts[_MONTH_INDEX]; |
| 202 | 202 |
| 203 /* patch */ int get year => _parts[_YEAR_INDEX]; | 203 /* @patch */ int get year => _parts[_YEAR_INDEX]; |
| 204 | 204 |
| 205 /** | 205 /** |
| 206 * Returns the amount of microseconds in UTC that represent the same values | 206 * Returns the amount of microseconds in UTC that represent the same values |
| 207 * as [this]. | 207 * as [this]. |
| 208 * | 208 * |
| 209 * Say `t` is the result of this function, then | 209 * Say `t` is the result of this function, then |
| 210 * * `this.year == new DateTime.fromMicrosecondsSinceEpoch(t, true).year`, | 210 * * `this.year == new DateTime.fromMicrosecondsSinceEpoch(t, true).year`, |
| 211 * * `this.month == new DateTime.fromMicrosecondsSinceEpoch(t, true).month`, | 211 * * `this.month == new DateTime.fromMicrosecondsSinceEpoch(t, true).month`, |
| 212 * * `this.day == new DateTime.fromMicrosecondsSinceEpoch(t, true).day`, | 212 * * `this.day == new DateTime.fromMicrosecondsSinceEpoch(t, true).day`, |
| 213 * * `this.hour == new DateTime.fromMicrosecondsSinceEpoch(t, true).hour`, | 213 * * `this.hour == new DateTime.fromMicrosecondsSinceEpoch(t, true).hour`, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 237 - _flooredDivision(year - 1901, 100) | 237 - _flooredDivision(year - 1901, 100) |
| 238 + _flooredDivision(year - 1601, 400); | 238 + _flooredDivision(year - 1601, 400); |
| 239 } | 239 } |
| 240 | 240 |
| 241 static bool _isLeapYear(y) { | 241 static bool _isLeapYear(y) { |
| 242 // (y % 16 == 0) matches multiples of 400, and is faster than % 400. | 242 // (y % 16 == 0) matches multiples of 400, and is faster than % 400. |
| 243 return (y % 4 == 0) && ((y % 16 == 0) || (y % 100 != 0)); | 243 return (y % 4 == 0) && ((y % 16 == 0) || (y % 100 != 0)); |
| 244 } | 244 } |
| 245 | 245 |
| 246 /// Converts the given broken down date to microseconds. | 246 /// Converts the given broken down date to microseconds. |
| 247 /* patch */ static int _brokenDownDateToValue( | 247 /* @patch */ static int _brokenDownDateToValue( |
| 248 int year, int month, int day, | 248 int year, int month, int day, |
| 249 int hour, int minute, int second, int millisecond, int microsecond, | 249 int hour, int minute, int second, int millisecond, int microsecond, |
| 250 bool isUtc) { | 250 bool isUtc) { |
| 251 // Simplify calculations by working with zero-based month. | 251 // Simplify calculations by working with zero-based month. |
| 252 --month; | 252 --month; |
| 253 // Deal with under and overflow. | 253 // Deal with under and overflow. |
| 254 if (month >= 12) { | 254 if (month >= 12) { |
| 255 year += month ~/ 12; | 255 year += month ~/ 12; |
| 256 month = month % 12; | 256 month = month % 12; |
| 257 } else if (month < 0) { | 257 } else if (month < 0) { |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 static int _timeZoneOffsetInSeconds(int microsecondsSinceEpoch) { | 394 static int _timeZoneOffsetInSeconds(int microsecondsSinceEpoch) { |
| 395 int equivalentSeconds = _equivalentSeconds(microsecondsSinceEpoch); | 395 int equivalentSeconds = _equivalentSeconds(microsecondsSinceEpoch); |
| 396 return _timeZoneOffsetInSecondsForClampedSeconds(equivalentSeconds); | 396 return _timeZoneOffsetInSecondsForClampedSeconds(equivalentSeconds); |
| 397 } | 397 } |
| 398 | 398 |
| 399 static String _timeZoneName(int microsecondsSinceEpoch) { | 399 static String _timeZoneName(int microsecondsSinceEpoch) { |
| 400 int equivalentSeconds = _equivalentSeconds(microsecondsSinceEpoch); | 400 int equivalentSeconds = _equivalentSeconds(microsecondsSinceEpoch); |
| 401 return _timeZoneNameForClampedSeconds(equivalentSeconds); | 401 return _timeZoneNameForClampedSeconds(equivalentSeconds); |
| 402 } | 402 } |
| 403 } | 403 } |
| OLD | NEW |