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

Unified Diff: lib/compiler/implementation/lib/mockimpl.dart

Issue 10829102: Create 'unified' version of Date, with a dart2js implementation. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Update comment and group external methods. Created 8 years, 5 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
Index: lib/compiler/implementation/lib/mockimpl.dart
diff --git a/lib/compiler/implementation/lib/mockimpl.dart b/lib/compiler/implementation/lib/mockimpl.dart
index 238c087b6d8b86fc696c8121398da3b858dcc0e9..90f084ee82309edf106a982741a7633eb1658328 100644
--- a/lib/compiler/implementation/lib/mockimpl.dart
+++ b/lib/compiler/implementation/lib/mockimpl.dart
@@ -174,222 +174,6 @@ class StringBase {
}
}
-class DateImplementation implements Date {
- final int millisecondsSinceEpoch;
- final bool isUtc;
-
- DateImplementation(int years,
- [int month = 1,
- int day = 1,
- int hour = 0,
- int minute = 0,
- int second = 0,
- int millisecond = 0,
- bool isUtc = false])
- : this.isUtc = checkNull(isUtc),
- millisecondsSinceEpoch = Primitives.valueFromDecomposedDate(
- years, month, day, hour, minute, second, millisecond, isUtc) {
- _asJs();
- }
-
- DateImplementation.now()
- : isUtc = false,
- millisecondsSinceEpoch = Primitives.dateNow() {
- _asJs();
- }
-
- factory DateImplementation.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(
- @'^([+-]?\d?\d\d\d\d)-?(\d\d)-?(\d\d)' // The day part.
- @'(?:[ 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 Math.parseInt(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 Math.parseDouble(matched);
- }
-
- int years = Math.parseInt(match[1]);
- int month = Math.parseInt(match[2]);
- int day = Math.parseInt(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 = Primitives.valueFromDecomposedDate(
- years, month, day, hour, minute, second, millisecond, isUtc);
- if (millisecondsSinceEpoch === null) {
- throw new IllegalArgumentException(formattedString);
- }
- if (addOneMillisecond) millisecondsSinceEpoch++;
- return new DateImplementation.fromMillisecondsSinceEpoch(
- millisecondsSinceEpoch, isUtc);
- } else {
- throw new IllegalArgumentException(formattedString);
- }
- }
-
- static final int _MAX_MILLISECONDS_SINCE_EPOCH = 8640000000000000;
-
- DateImplementation.fromMillisecondsSinceEpoch(this.millisecondsSinceEpoch,
- [bool isUtc = false])
- : this.isUtc = checkNull(isUtc) {
- if (millisecondsSinceEpoch.abs() > _MAX_MILLISECONDS_SINCE_EPOCH) {
- throw new IllegalArgumentException(millisecondsSinceEpoch);
- }
- }
-
- bool operator ==(other) {
- if (!(other is DateImplementation)) return false;
- int ms = millisecondsSinceEpoch;
- int otherMs = other.millisecondsSinceEpoch;
- return (ms == otherMs);
- }
-
- 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 hashCode() => millisecondsSinceEpoch;
-
- Date toLocal() {
- if (isUtc) {
- int ms = millisecondsSinceEpoch;
- return new DateImplementation.fromMillisecondsSinceEpoch(ms, false);
- }
- return this;
- }
-
- Date toUtc() {
- if (isUtc) return this;
- int ms = millisecondsSinceEpoch;
- return new DateImplementation.fromMillisecondsSinceEpoch(ms, true);
- }
-
- String get timeZoneName() {
- if (isUtc) return "UTC";
- return Primitives.getTimeZoneName(this);
- }
-
- Duration get timeZoneOffset() {
- if (isUtc) return new Duration(0);
- return new Duration(minutes: Primitives.getTimeZoneOffsetInMinutes(this));
- }
-
- int get year() => Primitives.getYear(this);
-
- int get month() => Primitives.getMonth(this);
-
- int get day() => Primitives.getDay(this);
-
- int get hour() => Primitives.getHours(this);
-
- int get minute() => Primitives.getMinutes(this);
-
- int get second() => Primitives.getSeconds(this);
-
- int get millisecond() => Primitives.getMilliseconds(this);
-
- int get weekday() {
- // Adjust by one because JS weeks start on Sunday.
- var day = Primitives.getWeekday(this);
- return (day + 6) % 7 + Date.MON;
- }
-
- 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";
- }
- }
-
- // Adds the [duration] to this Date instance.
- Date add(Duration duration) {
- int ms = millisecondsSinceEpoch;
- return new DateImplementation.fromMillisecondsSinceEpoch(
- ms + duration.inMilliseconds, isUtc);
- }
-
- // Subtracts the [duration] from this Date instance.
- Date subtract(Duration duration) {
- int ms = millisecondsSinceEpoch;
- return new DateImplementation.fromMillisecondsSinceEpoch(
- ms - duration.inMilliseconds, isUtc);
- }
-
- // Returns a [Duration] with the difference of [this] and [other].
- Duration difference(Date other) {
- int ms = millisecondsSinceEpoch;
- int otherMs = other.millisecondsSinceEpoch;
- return new DurationImplementation(milliseconds: ms - otherMs);
- }
-
- // Lazily keep a JS Date stored in the dart object.
- Dynamic _asJs() => Primitives.lazyAsJsDate(this);
-}
-
class ListFactory<E> {
factory List([int length]) => Primitives.newList(length);
factory List.from(Iterable<E> other) {

Powered by Google App Engine
This is Rietveld 408576698