| 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 | 4 |
| 5 // Dart core library. | 5 // Dart core library. |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Date is the public interface to a point in time. | 8 * Date is the public interface to a point in time. |
| 9 * | 9 * |
| 10 * It can represent time values that are at a distance of at most | 10 * It can represent time values that are at a distance of at most |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 // - "2012-02-27 13:27:00.423z" | 254 // - "2012-02-27 13:27:00.423z" |
| 255 // - "20120227 13:27:00" | 255 // - "20120227 13:27:00" |
| 256 // - "20120227T132700" | 256 // - "20120227T132700" |
| 257 // - "20120227" | 257 // - "20120227" |
| 258 // - "2012-02-27T14Z" | 258 // - "2012-02-27T14Z" |
| 259 // - "-123450101 00:00:00 Z" // In the year -12345. | 259 // - "-123450101 00:00:00 Z" // In the year -12345. |
| 260 final RegExp re = const RegExp( | 260 final RegExp re = const RegExp( |
| 261 r'^([+-]?\d?\d\d\d\d)-?(\d\d)-?(\d\d)' // The day part. | 261 r'^([+-]?\d?\d\d\d\d)-?(\d\d)-?(\d\d)' // The day part. |
| 262 r'(?:[ T](\d\d)(?::?(\d\d)(?::?(\d\d)(.\d{1,6})?)?)? ?([zZ])?)?$'); | 262 r'(?:[ T](\d\d)(?::?(\d\d)(?::?(\d\d)(.\d{1,6})?)?)? ?([zZ])?)?$'); |
| 263 Match match = re.firstMatch(formattedString); | 263 Match match = re.firstMatch(formattedString); |
| 264 if (match !== null) { | 264 if (match != null) { |
| 265 int parseIntOrZero(String matched) { | 265 int parseIntOrZero(String matched) { |
| 266 // TODO(floitsch): we should not need to test against the empty string. | 266 // TODO(floitsch): we should not need to test against the empty string. |
| 267 if (matched === null || matched == "") return 0; | 267 if (matched == null || matched == "") return 0; |
| 268 return int.parse(matched); | 268 return int.parse(matched); |
| 269 } | 269 } |
| 270 | 270 |
| 271 double parseDoubleOrZero(String matched) { | 271 double parseDoubleOrZero(String matched) { |
| 272 // TODO(floitsch): we should not need to test against the empty string. | 272 // TODO(floitsch): we should not need to test against the empty string. |
| 273 if (matched === null || matched == "") return 0.0; | 273 if (matched == null || matched == "") return 0.0; |
| 274 return double.parse(matched); | 274 return double.parse(matched); |
| 275 } | 275 } |
| 276 | 276 |
| 277 int years = int.parse(match[1]); | 277 int years = int.parse(match[1]); |
| 278 int month = int.parse(match[2]); | 278 int month = int.parse(match[2]); |
| 279 int day = int.parse(match[3]); | 279 int day = int.parse(match[3]); |
| 280 int hour = parseIntOrZero(match[4]); | 280 int hour = parseIntOrZero(match[4]); |
| 281 int minute = parseIntOrZero(match[5]); | 281 int minute = parseIntOrZero(match[5]); |
| 282 int second = parseIntOrZero(match[6]); | 282 int second = parseIntOrZero(match[6]); |
| 283 bool addOneMillisecond = false; | 283 bool addOneMillisecond = false; |
| 284 int millisecond = (parseDoubleOrZero(match[7]) * 1000).round().toInt(); | 284 int millisecond = (parseDoubleOrZero(match[7]) * 1000).round().toInt(); |
| 285 if (millisecond == 1000) { | 285 if (millisecond == 1000) { |
| 286 addOneMillisecond = true; | 286 addOneMillisecond = true; |
| 287 millisecond = 999; | 287 millisecond = 999; |
| 288 } | 288 } |
| 289 // TODO(floitsch): we should not need to test against the empty string. | 289 // TODO(floitsch): we should not need to test against the empty string. |
| 290 bool isUtc = (match[8] !== null) && (match[8] != ""); | 290 bool isUtc = (match[8] != null) && (match[8] != ""); |
| 291 int millisecondsSinceEpoch = _brokenDownDateToMillisecondsSinceEpoch( | 291 int millisecondsSinceEpoch = _brokenDownDateToMillisecondsSinceEpoch( |
| 292 years, month, day, hour, minute, second, millisecond, isUtc); | 292 years, month, day, hour, minute, second, millisecond, isUtc); |
| 293 if (millisecondsSinceEpoch === null) { | 293 if (millisecondsSinceEpoch == null) { |
| 294 throw new ArgumentError(formattedString); | 294 throw new ArgumentError(formattedString); |
| 295 } | 295 } |
| 296 if (addOneMillisecond) millisecondsSinceEpoch++; | 296 if (addOneMillisecond) millisecondsSinceEpoch++; |
| 297 return new Date.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, | 297 return new Date.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, |
| 298 isUtc: isUtc); | 298 isUtc: isUtc); |
| 299 } else { | 299 } else { |
| 300 throw new ArgumentError(formattedString); | 300 throw new ArgumentError(formattedString); |
| 301 } | 301 } |
| 302 } | 302 } |
| 303 | 303 |
| 304 static const int _MAX_MILLISECONDS_SINCE_EPOCH = 8640000000000000; | 304 static const int _MAX_MILLISECONDS_SINCE_EPOCH = 8640000000000000; |
| 305 | 305 |
| 306 _DateImpl.fromMillisecondsSinceEpoch(this.millisecondsSinceEpoch, | 306 _DateImpl.fromMillisecondsSinceEpoch(this.millisecondsSinceEpoch, |
| 307 this.isUtc) { | 307 this.isUtc) { |
| 308 if (millisecondsSinceEpoch.abs() > _MAX_MILLISECONDS_SINCE_EPOCH) { | 308 if (millisecondsSinceEpoch.abs() > _MAX_MILLISECONDS_SINCE_EPOCH) { |
| 309 throw new ArgumentError(millisecondsSinceEpoch); | 309 throw new ArgumentError(millisecondsSinceEpoch); |
| 310 } | 310 } |
| 311 if (isUtc === null) throw new ArgumentError(isUtc); | 311 if (isUtc == null) throw new ArgumentError(isUtc); |
| 312 } | 312 } |
| 313 | 313 |
| 314 bool operator ==(other) { | 314 bool operator ==(other) { |
| 315 if (!(other is Date)) return false; | 315 if (!(other is Date)) return false; |
| 316 return (millisecondsSinceEpoch == other.millisecondsSinceEpoch); | 316 return (millisecondsSinceEpoch == other.millisecondsSinceEpoch); |
| 317 } | 317 } |
| 318 | 318 |
| 319 bool operator <(Date other) | 319 bool operator <(Date other) |
| 320 => millisecondsSinceEpoch < other.millisecondsSinceEpoch; | 320 => millisecondsSinceEpoch < other.millisecondsSinceEpoch; |
| 321 | 321 |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 419 external Duration get timeZoneOffset; | 419 external Duration get timeZoneOffset; |
| 420 external int get year; | 420 external int get year; |
| 421 external int get month; | 421 external int get month; |
| 422 external int get day; | 422 external int get day; |
| 423 external int get hour; | 423 external int get hour; |
| 424 external int get minute; | 424 external int get minute; |
| 425 external int get second; | 425 external int get second; |
| 426 external int get millisecond; | 426 external int get millisecond; |
| 427 external int get weekday; | 427 external int get weekday; |
| 428 } | 428 } |
| OLD | NEW |