Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: sdk/lib/core/date_time.dart

Issue 2754013002: Format all dart: library files (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 part of dart.core; 5 part of dart.core;
6 6
7 /** 7 /**
8 * An instant in time, such as July 20, 1969, 8:18pm GMT. 8 * An instant in time, such as July 20, 1969, 8:18pm GMT.
9 * 9 *
10 * Create a DateTime object by using one of the constructors 10 * Create a DateTime object by using one of the constructors
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 148
149 /** 149 /**
150 * Constructs a [DateTime] instance specified in the local time zone. 150 * Constructs a [DateTime] instance specified in the local time zone.
151 * 151 *
152 * For example, 152 * For example,
153 * to create a new DateTime object representing April 29, 2014, 6:04am: 153 * to create a new DateTime object representing April 29, 2014, 6:04am:
154 * 154 *
155 * DateTime annularEclipse = new DateTime(2014, DateTime.APRIL, 29, 6, 4); 155 * DateTime annularEclipse = new DateTime(2014, DateTime.APRIL, 29, 6, 4);
156 */ 156 */
157 DateTime(int year, 157 DateTime(int year,
158 [int month = 1, 158 [int month = 1,
159 int day = 1, 159 int day = 1,
160 int hour = 0, 160 int hour = 0,
161 int minute = 0, 161 int minute = 0,
162 int second = 0, 162 int second = 0,
163 int millisecond = 0, 163 int millisecond = 0,
164 int microsecond = 0]) 164 int microsecond = 0])
165 : this._internal( 165 : this._internal(year, month, day, hour, minute, second, millisecond,
166 year, month, day, hour, minute, second, millisecond, microsecond, 166 microsecond, false);
167 false);
168 167
169 /** 168 /**
170 * Constructs a [DateTime] instance specified in the UTC time zone. 169 * Constructs a [DateTime] instance specified in the UTC time zone.
171 * 170 *
172 * DateTime dDay = new DateTime.utc(1944, DateTime.JUNE, 6); 171 * DateTime dDay = new DateTime.utc(1944, DateTime.JUNE, 6);
173 */ 172 */
174 DateTime.utc(int year, 173 DateTime.utc(int year,
175 [int month = 1, 174 [int month = 1,
176 int day = 1, 175 int day = 1,
177 int hour = 0, 176 int hour = 0,
178 int minute = 0, 177 int minute = 0,
179 int second = 0, 178 int second = 0,
180 int millisecond = 0, 179 int millisecond = 0,
181 int microsecond = 0]) 180 int microsecond = 0])
182 : this._internal( 181 : this._internal(year, month, day, hour, minute, second, millisecond,
183 year, month, day, hour, minute, second, millisecond, microsecond, 182 microsecond, true);
184 true);
185 183
186 /** 184 /**
187 * Constructs a [DateTime] instance with current date and time in the 185 * Constructs a [DateTime] instance with current date and time in the
188 * local time zone. 186 * local time zone.
189 * 187 *
190 * DateTime thisInstant = new DateTime.now(); 188 * DateTime thisInstant = new DateTime.now();
191 * 189 *
192 */ 190 */
193 DateTime.now() : this._now(); 191 DateTime.now() : this._now();
194 192
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 * day ::= digit{2} 254 * day ::= digit{2}
257 * time_opt ::= <empty> | (' ' | 'T') hour minutes_opt 255 * time_opt ::= <empty> | (' ' | 'T') hour minutes_opt
258 * minutes_opt ::= <empty> | colon_opt digit{2} seconds_opt 256 * minutes_opt ::= <empty> | colon_opt digit{2} seconds_opt
259 * seconds_opt ::= <empty> | colon_opt digit{2} millis_opt 257 * seconds_opt ::= <empty> | colon_opt digit{2} millis_opt
260 * micros_opt ::= <empty> | '.' digit{1,6} 258 * micros_opt ::= <empty> | '.' digit{1,6}
261 * timezone_opt ::= <empty> | space_opt timezone 259 * timezone_opt ::= <empty> | space_opt timezone
262 * space_opt :: ' ' | <empty> 260 * space_opt :: ' ' | <empty>
263 * timezone ::= 'z' | 'Z' | sign digit{2} timezonemins_opt 261 * timezone ::= 'z' | 'Z' | sign digit{2} timezonemins_opt
264 * timezonemins_opt ::= <empty> | colon_opt digit{2} 262 * timezonemins_opt ::= <empty> | colon_opt digit{2}
265 */ 263 */
266 final RegExp re = new RegExp( 264 final RegExp re = new RegExp(r'^([+-]?\d{4,6})-?(\d\d)-?(\d\d)' // Day part.
267 r'^([+-]?\d{4,6})-?(\d\d)-?(\d\d)' // Day part.
268 r'(?:[ T](\d\d)(?::?(\d\d)(?::?(\d\d)(?:\.(\d{1,6}))?)?)?' // Time part. 265 r'(?:[ T](\d\d)(?::?(\d\d)(?::?(\d\d)(?:\.(\d{1,6}))?)?)?' // Time part.
269 r'( ?[zZ]| ?([-+])(\d\d)(?::?(\d\d))?)?)?$'); // Timezone part. 266 r'( ?[zZ]| ?([-+])(\d\d)(?::?(\d\d))?)?)?$'); // Timezone part.
270 267
271 Match match = re.firstMatch(formattedString); 268 Match match = re.firstMatch(formattedString);
272 if (match != null) { 269 if (match != null) {
273 int parseIntOrZero(String matched) { 270 int parseIntOrZero(String matched) {
274 if (matched == null) return 0; 271 if (matched == null) return 0;
275 return int.parse(matched); 272 return int.parse(matched);
276 } 273 }
277 274
(...skipping 21 matching lines...) Expand all
299 int hour = parseIntOrZero(match[4]); 296 int hour = parseIntOrZero(match[4]);
300 int minute = parseIntOrZero(match[5]); 297 int minute = parseIntOrZero(match[5]);
301 int second = parseIntOrZero(match[6]); 298 int second = parseIntOrZero(match[6]);
302 bool addOneMillisecond = false; 299 bool addOneMillisecond = false;
303 int milliAndMicroseconds = parseMilliAndMicroseconds(match[7]); 300 int milliAndMicroseconds = parseMilliAndMicroseconds(match[7]);
304 int millisecond = 301 int millisecond =
305 milliAndMicroseconds ~/ Duration.MICROSECONDS_PER_MILLISECOND; 302 milliAndMicroseconds ~/ Duration.MICROSECONDS_PER_MILLISECOND;
306 int microsecond = 303 int microsecond =
307 milliAndMicroseconds.remainder(Duration.MICROSECONDS_PER_MILLISECOND); 304 milliAndMicroseconds.remainder(Duration.MICROSECONDS_PER_MILLISECOND);
308 bool isUtc = false; 305 bool isUtc = false;
309 if (match[8] != null) { // timezone part 306 if (match[8] != null) {
307 // timezone part
310 isUtc = true; 308 isUtc = true;
311 if (match[9] != null) { 309 if (match[9] != null) {
312 // timezone other than 'Z' and 'z'. 310 // timezone other than 'Z' and 'z'.
313 int sign = (match[9] == '-') ? -1 : 1; 311 int sign = (match[9] == '-') ? -1 : 1;
314 int hourDifference = int.parse(match[10]); 312 int hourDifference = int.parse(match[10]);
315 int minuteDifference = parseIntOrZero(match[11]); 313 int minuteDifference = parseIntOrZero(match[11]);
316 minuteDifference += 60 * hourDifference; 314 minuteDifference += 60 * hourDifference;
317 minute -= sign * minuteDifference; 315 minute -= sign * minuteDifference;
318 } 316 }
319 } 317 }
320 int value = _brokenDownDateToValue( 318 int value = _brokenDownDateToValue(years, month, day, hour, minute,
321 years, month, day, hour, minute, second, millisecond, microsecond, 319 second, millisecond, microsecond, isUtc);
322 isUtc);
323 if (value == null) { 320 if (value == null) {
324 throw new FormatException("Time out of range", formattedString); 321 throw new FormatException("Time out of range", formattedString);
325 } 322 }
326 return new DateTime._withValue(value, isUtc: isUtc); 323 return new DateTime._withValue(value, isUtc: isUtc);
327 } else { 324 } else {
328 throw new FormatException("Invalid date format", formattedString); 325 throw new FormatException("Invalid date format", formattedString);
329 } 326 }
330 } 327 }
331 328
332 static const int _MAX_MILLISECONDS_SINCE_EPOCH = 8640000000000000; 329 static const int _MAX_MILLISECONDS_SINCE_EPOCH = 8640000000000000;
333 330
334 /** 331 /**
335 * Constructs a new [DateTime] instance 332 * Constructs a new [DateTime] instance
336 * with the given [millisecondsSinceEpoch]. 333 * with the given [millisecondsSinceEpoch].
337 * 334 *
338 * If [isUtc] is false then the date is in the local time zone. 335 * If [isUtc] is false then the date is in the local time zone.
339 * 336 *
340 * The constructed [DateTime] represents 337 * The constructed [DateTime] represents
341 * 1970-01-01T00:00:00Z + [millisecondsSinceEpoch] ms in the given 338 * 1970-01-01T00:00:00Z + [millisecondsSinceEpoch] ms in the given
342 * time zone (local or UTC). 339 * time zone (local or UTC).
343 */ 340 */
344 external DateTime.fromMillisecondsSinceEpoch(int millisecondsSinceEpoch, 341 external DateTime.fromMillisecondsSinceEpoch(int millisecondsSinceEpoch,
345 {bool isUtc: false}); 342 {bool isUtc: false});
346 343
347 /** 344 /**
348 * Constructs a new [DateTime] instance 345 * Constructs a new [DateTime] instance
349 * with the given [microsecondsSinceEpoch]. 346 * with the given [microsecondsSinceEpoch].
350 * 347 *
351 * If [isUtc] is false then the date is in the local time zone. 348 * If [isUtc] is false then the date is in the local time zone.
352 * 349 *
353 * The constructed [DateTime] represents 350 * The constructed [DateTime] represents
354 * 1970-01-01T00:00:00Z + [microsecondsSinceEpoch] us in the given 351 * 1970-01-01T00:00:00Z + [microsecondsSinceEpoch] us in the given
355 * time zone (local or UTC). 352 * time zone (local or UTC).
356 */ 353 */
357 external DateTime.fromMicrosecondsSinceEpoch(int microsecondsSinceEpoch, 354 external DateTime.fromMicrosecondsSinceEpoch(int microsecondsSinceEpoch,
358 {bool isUtc: false}); 355 {bool isUtc: false});
359 356
360 /** 357 /**
361 * Constructs a new [DateTime] instance with the given value. 358 * Constructs a new [DateTime] instance with the given value.
362 * 359 *
363 * If [isUtc] is false then the date is in the local time zone. 360 * If [isUtc] is false then the date is in the local time zone.
364 */ 361 */
365 DateTime._withValue(this._value, {this.isUtc}) { 362 DateTime._withValue(this._value, {this.isUtc}) {
366 if (millisecondsSinceEpoch.abs() > _MAX_MILLISECONDS_SINCE_EPOCH || 363 if (millisecondsSinceEpoch.abs() > _MAX_MILLISECONDS_SINCE_EPOCH ||
367 (millisecondsSinceEpoch.abs() == _MAX_MILLISECONDS_SINCE_EPOCH && 364 (millisecondsSinceEpoch.abs() == _MAX_MILLISECONDS_SINCE_EPOCH &&
368 microsecond != 0)) { 365 microsecond != 0)) {
369 throw new ArgumentError(millisecondsSinceEpoch); 366 throw new ArgumentError(millisecondsSinceEpoch);
370 } 367 }
371 if (isUtc == null) throw new ArgumentError(isUtc); 368 if (isUtc == null) throw new ArgumentError(isUtc);
372 } 369 }
373 370
374 /** 371 /**
375 * Returns true if [other] is a [DateTime] at the same moment and in the 372 * Returns true if [other] is a [DateTime] at the same moment and in the
376 * same time zone (UTC or local). 373 * same time zone (UTC or local).
377 * 374 *
378 * DateTime dDayUtc = new DateTime.utc(1944, DateTime.JUNE, 6); 375 * DateTime dDayUtc = new DateTime.utc(1944, DateTime.JUNE, 6);
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
546 * * `HH` are hours in the range 00 to 23, 543 * * `HH` are hours in the range 00 to 23,
547 * * `mm` are minutes in the range 00 to 59, 544 * * `mm` are minutes in the range 00 to 59,
548 * * `ss` are seconds in the range 00 to 59 (no leap seconds), 545 * * `ss` are seconds in the range 00 to 59 (no leap seconds),
549 * * `mmm` are milliseconds in the range 000 to 999, and 546 * * `mmm` are milliseconds in the range 000 to 999, and
550 * * `uuu` are microseconds in the range 001 to 999. If [microsecond] equals 547 * * `uuu` are microseconds in the range 001 to 999. If [microsecond] equals
551 * 0, then this part is omitted. 548 * 0, then this part is omitted.
552 * 549 *
553 * The resulting string can be parsed back using [parse]. 550 * The resulting string can be parsed back using [parse].
554 */ 551 */
555 String toIso8601String() { 552 String toIso8601String() {
556 String y = (year >= -9999 && year <= 9999) ? _fourDigits(year) 553 String y =
557 : _sixDigits(year); 554 (year >= -9999 && year <= 9999) ? _fourDigits(year) : _sixDigits(year);
558 String m = _twoDigits(month); 555 String m = _twoDigits(month);
559 String d = _twoDigits(day); 556 String d = _twoDigits(day);
560 String h = _twoDigits(hour); 557 String h = _twoDigits(hour);
561 String min = _twoDigits(minute); 558 String min = _twoDigits(minute);
562 String sec = _twoDigits(second); 559 String sec = _twoDigits(second);
563 String ms = _threeDigits(millisecond); 560 String ms = _threeDigits(millisecond);
564 String us = microsecond == 0 ? "" : _threeDigits(microsecond); 561 String us = microsecond == 0 ? "" : _threeDigits(microsecond);
565 if (isUtc) { 562 if (isUtc) {
566 return "$y-$m-${d}T$h:$min:$sec.$ms${us}Z"; 563 return "$y-$m-${d}T$h:$min:$sec.$ms${us}Z";
567 } else { 564 } else {
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
620 * DateTime berlinWallFell = new DateTime(1989, DateTime.NOVEMBER, 9); 617 * DateTime berlinWallFell = new DateTime(1989, DateTime.NOVEMBER, 9);
621 * DateTime dDay = new DateTime(1944, DateTime.JUNE, 6); 618 * DateTime dDay = new DateTime(1944, DateTime.JUNE, 6);
622 * Duration difference = berlinWallFell.difference(dDay); 619 * Duration difference = berlinWallFell.difference(dDay);
623 * assert(difference.inDays == 16592); 620 * assert(difference.inDays == 16592);
624 * 621 *
625 * will fail because the difference is actually 16591 days and 23 hours, and 622 * will fail because the difference is actually 16591 days and 23 hours, and
626 * [Duration.inDays] only returns the number of whole days. 623 * [Duration.inDays] only returns the number of whole days.
627 */ 624 */
628 external Duration difference(DateTime other); 625 external Duration difference(DateTime other);
629 626
630 external DateTime._internal(int year, 627 external DateTime._internal(int year, int month, int day, int hour,
631 int month, 628 int minute, int second, int millisecond, int microsecond, bool isUtc);
632 int day,
633 int hour,
634 int minute,
635 int second,
636 int millisecond,
637 int microsecond,
638 bool isUtc);
639 629
640 external DateTime._now(); 630 external DateTime._now();
641 631
642 /// Returns the time as value (millisecond or microsecond since epoch), or 632 /// Returns the time as value (millisecond or microsecond since epoch), or
643 /// null if the values are out of range. 633 /// null if the values are out of range.
644 external static int _brokenDownDateToValue( 634 external static int _brokenDownDateToValue(
645 int year, int month, int day, int hour, int minute, int second, 635 int year,
646 int millisecond, int microsecond, bool isUtc); 636 int month,
637 int day,
638 int hour,
639 int minute,
640 int second,
641 int millisecond,
642 int microsecond,
643 bool isUtc);
647 644
648 /** 645 /**
649 * The number of milliseconds since 646 * The number of milliseconds since
650 * the "Unix epoch" 1970-01-01T00:00:00Z (UTC). 647 * the "Unix epoch" 1970-01-01T00:00:00Z (UTC).
651 * 648 *
652 * This value is independent of the time zone. 649 * This value is independent of the time zone.
653 * 650 *
654 * This value is at most 651 * This value is at most
655 * 8,640,000,000,000,000ms (100,000,000 days) from the Unix epoch. 652 * 8,640,000,000,000,000ms (100,000,000 days) from the Unix epoch.
656 * In other words: `millisecondsSinceEpoch.abs() <= 8640000000000000`. 653 * In other words: `millisecondsSinceEpoch.abs() <= 8640000000000000`.
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
767 * In accordance with ISO 8601 764 * In accordance with ISO 8601
768 * a week starts with Monday, which has the value 1. 765 * a week starts with Monday, which has the value 1.
769 * 766 *
770 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00"); 767 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00");
771 * assert(moonLanding.weekday == 7); 768 * assert(moonLanding.weekday == 7);
772 * assert(moonLanding.weekday == DateTime.SUNDAY); 769 * assert(moonLanding.weekday == DateTime.SUNDAY);
773 * 770 *
774 */ 771 */
775 external int get weekday; 772 external int get weekday;
776 } 773 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698