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

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

Issue 165723005: Add toIso8601String to DateTime. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Add test. Created 6 years, 10 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | tests/corelib/date_time_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 379 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 * 390 *
391 * new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, 391 * new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch,
392 * isUtc: true) 392 * isUtc: true)
393 */ 393 */
394 DateTime toUtc() { 394 DateTime toUtc() {
395 if (isUtc) return this; 395 if (isUtc) return this;
396 return new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, 396 return new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch,
397 isUtc: true); 397 isUtc: true);
398 } 398 }
399 399
400 static String _fourDigits(int n) {
401 int absN = n.abs();
402 String sign = n < 0 ? "-" : "";
403 if (absN >= 1000) return "$n";
404 if (absN >= 100) return "${sign}0$absN";
405 if (absN >= 10) return "${sign}00$absN";
406 return "${sign}000$absN";
407 }
408
409 static String _threeDigits(int n) {
410 if (n >= 100) return "${n}";
411 if (n >= 10) return "0${n}";
412 return "00${n}";
413 }
414
415 static String _twoDigits(int n) {
416 if (n >= 10) return "${n}";
417 return "0${n}";
418 }
419
400 /** 420 /**
401 * Returns a human-readable string for this instance. 421 * Returns a human-readable string for this instance.
402 * 422 *
403 * The returned string is constructed for the time zone of this instance. 423 * The returned string is constructed for the time zone of this instance.
404 * The `toString()` method provides a simply formatted string. 424 * The `toString()` method provides a simply formatted string.
405 * It does not support internationalized strings. 425 * It does not support internationalized strings.
406 * Use the [intl](http://pub.dartlang.org/packages/intl) package 426 * Use the [intl](http://pub.dartlang.org/packages/intl) package
407 * at the pub shared packages repo. 427 * at the pub shared packages repo.
408 */ 428 */
409 String toString() { 429 String toString() {
410 String fourDigits(int n) { 430 String y = _fourDigits(year);
411 int absN = n.abs(); 431 String m = _twoDigits(month);
412 String sign = n < 0 ? "-" : ""; 432 String d = _twoDigits(day);
413 if (absN >= 1000) return "$n"; 433 String h = _twoDigits(hour);
414 if (absN >= 100) return "${sign}0$absN"; 434 String min = _twoDigits(minute);
415 if (absN >= 10) return "${sign}00$absN"; 435 String sec = _twoDigits(second);
416 return "${sign}000$absN"; 436 String ms = _threeDigits(millisecond);
417 }
418
419 String threeDigits(int n) {
420 if (n >= 100) return "${n}";
421 if (n >= 10) return "0${n}";
422 return "00${n}";
423 }
424
425 String twoDigits(int n) {
426 if (n >= 10) return "${n}";
427 return "0${n}";
428 }
429
430 String y = fourDigits(year);
431 String m = twoDigits(month);
432 String d = twoDigits(day);
433 String h = twoDigits(hour);
434 String min = twoDigits(minute);
435 String sec = twoDigits(second);
436 String ms = threeDigits(millisecond);
437 if (isUtc) { 437 if (isUtc) {
438 return "$y-$m-$d $h:$min:$sec.${ms}Z"; 438 return "$y-$m-$d $h:$min:$sec.${ms}Z";
439 } else { 439 } else {
440 return "$y-$m-$d $h:$min:$sec.$ms"; 440 return "$y-$m-$d $h:$min:$sec.$ms";
441 } 441 }
442 } 442 }
443 443
444 /** 444 /**
445 * Returns an ISO-8601 full-precision extended format representation.
446 *
447 * The format is "YYYY-MM-DDTHH:mm:ss.sssZ" for UTC time, and
448 * "YYYY-MM-DDTHH:mm:ss.sss" (no trailing "Z") for local/non-UTC time.
449 */
450 String toIso8601String() {
451 String y = _fourDigits(year);
452 String m = _twoDigits(month);
453 String d = _twoDigits(day);
454 String h = _twoDigits(hour);
455 String min = _twoDigits(minute);
456 String sec = _twoDigits(second);
457 String ms = _threeDigits(millisecond);
458 if (isUtc) {
459 return "$y-$m-${d}T$h:$min:$sec.${ms}Z";
460 } else {
461 return "$y-$m-${d}T$h:$min:$sec.$ms";
462 }
463 }
464
465 /**
445 * Returns a new [DateTime] instance with [duration] added to [this]. 466 * Returns a new [DateTime] instance with [duration] added to [this].
446 * 467 *
447 * DateTime today = new DateTime.now(); 468 * DateTime today = new DateTime.now();
448 * DateTime sixtyDaysFromNow = today.add(new Duration(days: 60)); 469 * DateTime sixtyDaysFromNow = today.add(new Duration(days: 60));
449 */ 470 */
450
451 DateTime add(Duration duration) { 471 DateTime add(Duration duration) {
452 int ms = millisecondsSinceEpoch; 472 int ms = millisecondsSinceEpoch;
453 return new DateTime.fromMillisecondsSinceEpoch( 473 return new DateTime.fromMillisecondsSinceEpoch(
454 ms + duration.inMilliseconds, isUtc: isUtc); 474 ms + duration.inMilliseconds, isUtc: isUtc);
455 } 475 }
456 476
457 /** 477 /**
458 * Returns a new [DateTime] instance with [duration] subtracted from [this]. 478 * Returns a new [DateTime] instance with [duration] subtracted from [this].
459 * 479 *
460 * DateTime today = new DateTime.now(); 480 * DateTime today = new DateTime.now();
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
576 * In accordance with ISO 8601 596 * In accordance with ISO 8601
577 * a week starts with Monday, which has the value 1. 597 * a week starts with Monday, which has the value 1.
578 * 598 *
579 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00"); 599 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00");
580 * assert(moonLanding.weekday == 7); 600 * assert(moonLanding.weekday == 7);
581 * assert(moonLanding.weekday == DateTime.SUNDAY); 601 * assert(moonLanding.weekday == DateTime.SUNDAY);
582 * 602 *
583 */ 603 */
584 external int get weekday; 604 external int get weekday;
585 } 605 }
OLDNEW
« no previous file with comments | « no previous file | tests/corelib/date_time_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698