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

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

Issue 1508053002: Add warning on DateTime.difference that DST + local-time is dangerous. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 5 years 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
« no previous file with comments | « no previous file | no next file » | 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 562 matching lines...) Expand 10 before | Expand all | Expand 10 after
573 DateTime add(Duration duration) { 573 DateTime add(Duration duration) {
574 int ms = millisecondsSinceEpoch; 574 int ms = millisecondsSinceEpoch;
575 return new DateTime.fromMillisecondsSinceEpoch( 575 return new DateTime.fromMillisecondsSinceEpoch(
576 ms + duration.inMilliseconds, isUtc: isUtc); 576 ms + duration.inMilliseconds, isUtc: isUtc);
577 } 577 }
578 578
579 /** 579 /**
580 * Returns a new [DateTime] instance with [duration] subtracted from [this]. 580 * Returns a new [DateTime] instance with [duration] subtracted from [this].
581 * 581 *
582 * DateTime today = new DateTime.now(); 582 * DateTime today = new DateTime.now();
583 * DateTime sixtyDaysAgo = today.subtract(new Duration(days: 60)); 583 * DateTime sixtyDaysAgo = today.subtract(new Duration(days: 30));
584 *
585 * Notice that duration being subtracted is actually 30 * 24 * 60 * 60 seconds
586 * and if that crosses a daylight saving time change, the resulting `DateTime`
587 * won't have the same time of day as `today`, and may not actually hit the
588 * calendar date 30 days earlier. Be careful when working with dates in local
589 * time.
584 */ 590 */
585 DateTime subtract(Duration duration) { 591 DateTime subtract(Duration duration) {
586 int ms = millisecondsSinceEpoch; 592 int ms = millisecondsSinceEpoch;
587 return new DateTime.fromMillisecondsSinceEpoch( 593 return new DateTime.fromMillisecondsSinceEpoch(
588 ms - duration.inMilliseconds, isUtc: isUtc); 594 ms - duration.inMilliseconds, isUtc: isUtc);
589 } 595 }
590 596
591 /** 597 /**
592 * Returns a [Duration] with the difference between [this] and [other]. 598 * Returns a [Duration] with the difference between [this] and [other].
593 * 599 *
594 * DateTime berlinWallFell = new DateTime(1989, DateTime.NOVEMBER, 9); 600 * DateTime berlinWallFell = new DateTime.utc(1989, DateTime.NOVEMBER, 9);
595 * DateTime dDay = new DateTime(1944, DateTime.JUNE, 6); 601 * DateTime dDay = new DateTime.utc(1944, DateTime.JUNE, 6);
596 * 602 *
597 * Duration difference = berlinWallFell.difference(dDay); 603 * Duration difference = berlinWallFell.difference(dDay);
598 * assert(difference.inDays == 16592); 604 * assert(difference.inDays == 16592);
605 *
606 * The difference is measured in seconds and fractions of seconds.
607 * The difference above counts the number of fractional seconds between
608 * midnight at the beginning of those dates.
609 * If the dates above had been in local time, not UTC, then the difference
610 * between two midnights may not be a multiple of 24 hours due to daylight
611 * saving differences.
612 *
613 * For example, in Australia, similar code using local time instead of UTC:
614 *
615 * DateTime berlinWallFell = new DateTime(1989, DateTime.NOVEMBER, 9);
616 * DateTime dDay = new DateTime(1944, DateTime.JUNE, 6);
617 * Duration difference = berlinWallFell.difference(dDay);
618 * assert(difference.inDays == 16592);
619 *
620 * will fail because the difference is actually 16591 days and 23 hours, and
621 * [Duration.inDays] only returns the number of whole days.
599 */ 622 */
600
601 Duration difference(DateTime other) { 623 Duration difference(DateTime other) {
602 int ms = millisecondsSinceEpoch; 624 int ms = millisecondsSinceEpoch;
603 int otherMs = other.millisecondsSinceEpoch; 625 int otherMs = other.millisecondsSinceEpoch;
604 return new Duration(milliseconds: ms - otherMs); 626 return new Duration(milliseconds: ms - otherMs);
605 } 627 }
606 628
607 external DateTime._internal(int year, 629 external DateTime._internal(int year,
608 int month, 630 int month,
609 int day, 631 int day,
610 int hour, 632 int hour,
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
700 * In accordance with ISO 8601 722 * In accordance with ISO 8601
701 * a week starts with Monday, which has the value 1. 723 * a week starts with Monday, which has the value 1.
702 * 724 *
703 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00"); 725 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00");
704 * assert(moonLanding.weekday == 7); 726 * assert(moonLanding.weekday == 7);
705 * assert(moonLanding.weekday == DateTime.SUNDAY); 727 * assert(moonLanding.weekday == DateTime.SUNDAY);
706 * 728 *
707 */ 729 */
708 external int get weekday; 730 external int get weekday;
709 } 731 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698