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

Unified Diff: lib/core/date.dart

Issue 11227055: Move DateImplementation from coreimpl to core, making it private. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 2 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/compiler/implementation/lib/coreimpl_patch.dart ('k') | lib/coreimpl/coreimpl.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/core/date.dart
diff --git a/lib/core/date.dart b/lib/core/date.dart
index 1c496ae17a3a44326b1fa2698d61a6a6895dcebf..7ab46cf07baed8de1341cfc2f0b0a5ae6b92cd8c 100644
--- a/lib/core/date.dart
+++ b/lib/core/date.dart
@@ -52,9 +52,8 @@ abstract class Date implements Comparable {
int minute = 0,
int second = 0,
int millisecond = 0]) {
- return new DateImplementation(year, month, day,
- hour, minute, second,
- millisecond, false);
+ return new _DateImpl(
+ year, month, day, hour, minute, second, millisecond, false);
}
/**
@@ -72,22 +71,21 @@ abstract class Date implements Comparable {
int minute = 0,
int second = 0,
int millisecond = 0]) {
- return new DateImplementation(year, month, day,
- hour, minute, second,
- millisecond, true);
+ return new _DateImpl(
+ year, month, day, hour, minute, second, millisecond, true);
}
/**
* Constructs a new [Date] instance with current date time value in the
* local time zone.
*/
- factory Date.now() => new DateImplementation.now();
+ factory Date.now() => new _DateImpl.now();
/**
* Constructs a new [Date] instance based on [formattedString].
*/
factory Date.fromString(String formattedString)
- => new DateImplementation.fromString(formattedString);
+ => new _DateImpl.fromString(formattedString);
/**
* Constructs a new [Date] instance with the given [millisecondsSinceEpoch].
@@ -102,8 +100,8 @@ abstract class Date implements Comparable {
// TODO(lrn): Have two constructors instead of taking an optional bool.
factory Date.fromMillisecondsSinceEpoch(int millisecondsSinceEpoch,
{bool isUtc: false}) {
- return new DateImplementation.fromMillisecondsSinceEpoch(
- millisecondsSinceEpoch, isUtc);
+ return new _DateImpl.fromMillisecondsSinceEpoch(millisecondsSinceEpoch,
+ isUtc);
}
/**
@@ -244,3 +242,187 @@ abstract class Date implements Comparable {
*/
Duration difference(Date other);
}
+
+class _DateImpl implements Date {
+ final int millisecondsSinceEpoch;
+ final bool isUtc;
+
+ factory _DateImpl.fromString(String formattedString) {
+ // Read in (a subset of) ISO 8601.
+ // Examples:
+ // - "2012-02-27 13:27:00"
+ // - "2012-02-27 13:27:00.423z"
+ // - "20120227 13:27:00"
+ // - "20120227T132700"
+ // - "20120227"
+ // - "2012-02-27T14Z"
+ // - "-123450101 00:00:00 Z" // In the year -12345.
+ final RegExp re = const RegExp(
+ r'^([+-]?\d?\d\d\d\d)-?(\d\d)-?(\d\d)' // The day part.
+ r'(?:[ T](\d\d)(?::?(\d\d)(?::?(\d\d)(.\d{1,6})?)?)? ?([zZ])?)?$');
+ Match match = re.firstMatch(formattedString);
+ if (match !== null) {
+ int parseIntOrZero(String matched) {
+ // TODO(floitsch): we should not need to test against the empty string.
+ if (matched === null || matched == "") return 0;
+ return int.parse(matched);
+ }
+
+ double parseDoubleOrZero(String matched) {
+ // TODO(floitsch): we should not need to test against the empty string.
+ if (matched === null || matched == "") return 0.0;
+ return double.parse(matched);
+ }
+
+ int years = int.parse(match[1]);
+ int month = int.parse(match[2]);
+ int day = int.parse(match[3]);
+ int hour = parseIntOrZero(match[4]);
+ int minute = parseIntOrZero(match[5]);
+ int second = parseIntOrZero(match[6]);
+ bool addOneMillisecond = false;
+ int millisecond = (parseDoubleOrZero(match[7]) * 1000).round().toInt();
+ if (millisecond == 1000) {
+ addOneMillisecond = true;
+ millisecond = 999;
+ }
+ // TODO(floitsch): we should not need to test against the empty string.
+ bool isUtc = (match[8] !== null) && (match[8] != "");
+ int millisecondsSinceEpoch = _brokenDownDateToMillisecondsSinceEpoch(
+ years, month, day, hour, minute, second, millisecond, isUtc);
+ if (millisecondsSinceEpoch === null) {
+ throw new ArgumentError(formattedString);
+ }
+ if (addOneMillisecond) millisecondsSinceEpoch++;
+ return new Date.fromMillisecondsSinceEpoch(millisecondsSinceEpoch,
+ isUtc: isUtc);
+ } else {
+ throw new ArgumentError(formattedString);
+ }
+ }
+
+ static const int _MAX_MILLISECONDS_SINCE_EPOCH = 8640000000000000;
+
+ _DateImpl.fromMillisecondsSinceEpoch(this.millisecondsSinceEpoch,
+ this.isUtc) {
+ if (millisecondsSinceEpoch.abs() > _MAX_MILLISECONDS_SINCE_EPOCH) {
+ throw new ArgumentError(millisecondsSinceEpoch);
+ }
+ if (isUtc === null) throw new ArgumentError(isUtc);
+ }
+
+ bool operator ==(other) {
+ if (!(other is Date)) return false;
+ return (millisecondsSinceEpoch == other.millisecondsSinceEpoch);
+ }
+
+ bool operator <(Date other)
+ => millisecondsSinceEpoch < other.millisecondsSinceEpoch;
+
+ bool operator <=(Date other)
+ => millisecondsSinceEpoch <= other.millisecondsSinceEpoch;
+
+ bool operator >(Date other)
+ => millisecondsSinceEpoch > other.millisecondsSinceEpoch;
+
+ bool operator >=(Date other)
+ => millisecondsSinceEpoch >= other.millisecondsSinceEpoch;
+
+ int compareTo(Date other)
+ => millisecondsSinceEpoch.compareTo(other.millisecondsSinceEpoch);
+
+ int get hashCode => millisecondsSinceEpoch;
+
+ Date toLocal() {
+ if (isUtc) {
+ return new Date.fromMillisecondsSinceEpoch(millisecondsSinceEpoch,
+ isUtc: false);
+ }
+ return this;
+ }
+
+ Date toUtc() {
+ if (isUtc) return this;
+ return new Date.fromMillisecondsSinceEpoch(millisecondsSinceEpoch,
+ isUtc: true);
+ }
+
+ String toString() {
+ String fourDigits(int n) {
+ int absN = n.abs();
+ String sign = n < 0 ? "-" : "";
+ if (absN >= 1000) return "$n";
+ if (absN >= 100) return "${sign}0$absN";
+ if (absN >= 10) return "${sign}00$absN";
+ return "${sign}000$absN";
+ }
+
+ String threeDigits(int n) {
+ if (n >= 100) return "${n}";
+ if (n >= 10) return "0${n}";
+ return "00${n}";
+ }
+
+ String twoDigits(int n) {
+ if (n >= 10) return "${n}";
+ return "0${n}";
+ }
+
+ String y = fourDigits(year);
+ String m = twoDigits(month);
+ String d = twoDigits(day);
+ String h = twoDigits(hour);
+ String min = twoDigits(minute);
+ String sec = twoDigits(second);
+ String ms = threeDigits(millisecond);
+ if (isUtc) {
+ return "$y-$m-$d $h:$min:$sec.${ms}Z";
+ } else {
+ return "$y-$m-$d $h:$min:$sec.$ms";
+ }
+ }
+
+ /** Returns a new [Date] with the [duration] added to [this]. */
+ Date add(Duration duration) {
+ int ms = millisecondsSinceEpoch;
+ return new Date.fromMillisecondsSinceEpoch(
+ ms + duration.inMilliseconds, isUtc: isUtc);
+ }
+
+ /** Returns a new [Date] with the [duration] subtracted from [this]. */
+ Date subtract(Duration duration) {
+ int ms = millisecondsSinceEpoch;
+ return new Date.fromMillisecondsSinceEpoch(
+ ms - duration.inMilliseconds, isUtc: isUtc);
+ }
+
+ /** Returns a [Duration] with the difference of [this] and [other]. */
+ Duration difference(Date other) {
+ int ms = millisecondsSinceEpoch;
+ int otherMs = other.millisecondsSinceEpoch;
+ return new Duration(milliseconds: ms - otherMs);
+ }
+
+ external _DateImpl(int year,
+ int month,
+ int day,
+ int hour,
+ int minute,
+ int second,
+ int millisecond,
+ bool isUtc);
+ external _DateImpl.now();
+ external static int _brokenDownDateToMillisecondsSinceEpoch(
+ int year, int month, int day, int hour, int minute, int second,
+ int millisecond, bool isUtc);
+ external String get timeZoneName;
+ external Duration get timeZoneOffset;
+ external int get year;
+ external int get month;
+ external int get day;
+ external int get hour;
+ external int get minute;
+ external int get second;
+ external int get millisecond;
+ external int get weekday;
+}
« no previous file with comments | « lib/compiler/implementation/lib/coreimpl_patch.dart ('k') | lib/coreimpl/coreimpl.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698