Chromium Code Reviews| Index: runtime/lib/date.dart |
| diff --git a/runtime/lib/date.dart b/runtime/lib/date.dart |
| index 97395aed124284628348d47c4de312c9fddf5805..33d7bf3a490776b7180cf2cb9763d83ae7f30d19 100644 |
| --- a/runtime/lib/date.dart |
| +++ b/runtime/lib/date.dart |
| @@ -17,17 +17,23 @@ class TimeZoneImplementation implements TimeZone { |
| // JavaScript implementation of DateImplementation. |
|
Mads Ager (google)
2012/05/22 09:11:22
JavaScript? Not your code, but could you update? :
floitsch
2012/05/22 14:50:38
It probably is my code :)
done.
|
| class DateImplementation implements Date { |
| - factory DateImplementation(int years, |
| - [int month = 1, |
| - int day = 1, |
| - int hours = 0, |
| - int minutes = 0, |
| - int seconds = 0, |
| - int milliseconds = 0]) { |
| - return new DateImplementation.withTimeZone( |
| - years, month, day, |
| - hours, minutes, seconds, milliseconds, |
| - new TimeZoneImplementation.local()); |
| + final bool _isUtcField; |
|
Mads Ager (google)
2012/05/22 09:11:22
You should move the instance fields to the end of
floitsch
2012/05/22 14:50:38
Done.
|
| + final int value; |
| + |
| + static final int _SECONDS_YEAR_2035 = 2051222400; |
| + |
| + DateImplementation(int years, |
| + [int month = 1, |
| + int day = 1, |
| + int hours = 0, |
| + int minutes = 0, |
| + int seconds = 0, |
| + int milliseconds = 0, |
| + bool isUtc = false]) |
| + : _isUtcField = isUtc, |
| + value = _brokenDownDateToMillisecondsSinceEpoch( |
| + years, month, day, hours, minutes, seconds, milliseconds, isUtc) { |
| + if (value === null) throw new IllegalArgumentException(); |
| } |
| DateImplementation.withTimeZone(int years, |
| @@ -38,15 +44,11 @@ class DateImplementation implements Date { |
| int seconds, |
| int milliseconds, |
| TimeZone timeZone) |
| - : timeZone = timeZone, |
| - value = _brokenDownDateToMillisecondsSinceEpoch( |
| - years, month, day, hours, minutes, seconds, milliseconds, |
| - timeZone.isUtc) { |
| - if (value === null) throw new IllegalArgumentException(); |
| - } |
| + : this(years, month, day, hours, minutes, seconds, milliseconds, |
|
Mads Ager (google)
2012/05/22 09:11:22
four-space indent as above. Not your code, but cou
floitsch
2012/05/22 14:50:38
Done.
|
| + timeZone.isUtc); |
| DateImplementation.now() |
| - : timeZone = new TimeZone.local(), |
| + : _isUtcField = true, |
|
Mads Ager (google)
2012/05/22 09:11:22
Ditto.
floitsch
2012/05/22 14:50:38
Done.
|
| value = _getCurrentMs() { |
| } |
| @@ -91,25 +93,24 @@ class DateImplementation implements Date { |
| } |
| // TODO(floitsch): we should not need to test against the empty string. |
| bool isUtc = (match[8] !== null) && (match[8] != ""); |
| - TimeZone timezone = isUtc ? const TimeZone.utc() : new TimeZone.local(); |
| int epochValue = _brokenDownDateToMillisecondsSinceEpoch( |
| years, month, day, hours, minutes, seconds, milliseconds, isUtc); |
| if (epochValue === null) { |
| throw new IllegalArgumentException(formattedString); |
| } |
| if (addOneMillisecond) epochValue++; |
| - return new DateImplementation.fromEpoch(epochValue, timezone); |
| + return new DateImplementation.fromEpoch(epochValue, isUtc); |
| } else { |
| throw new IllegalArgumentException(formattedString); |
| } |
| } |
| - const DateImplementation.fromEpoch(int this.value, |
| - TimeZone this.timeZone); |
| + DateImplementation.fromEpoch(int this.value, [bool isUtc = false]) |
| + : _isUtcField = isUtc; |
| bool operator ==(Object other) { |
| if (!(other is DateImplementation)) return false; |
|
ngeoffray
2012/05/22 08:58:45
other is !
floitsch
2012/05/22 14:50:38
Done.
|
| - return value == other.value && timeZone == other.timeZone; |
| + return value == other.value; |
| } |
| bool operator <(Date other) => value < other.value; |
| @@ -123,11 +124,21 @@ class DateImplementation implements Date { |
| int compareTo(Date other) => value.compareTo(other.value); |
| int hashCode() => value; |
| + Date toLocal() { |
| + if (isUtc()) return new DateImplementation.fromEpoch(value, false); |
| + return this; |
| + } |
| + |
| + Date toUtc() { |
| + if (isUtc()) return this; |
| + return new DateImplementation.fromEpoch(value, true); |
| + } |
| + |
| Date changeTimeZone(TimeZone targetTimeZone) { |
| if (targetTimeZone === null) { |
| targetTimeZone = new TimeZoneImplementation.local(); |
| } |
| - return new Date.fromEpoch(value, targetTimeZone); |
| + return new Date.fromEpoch(value, targetTimeZone.isUtc); |
| } |
| String get timeZoneName() { |
| @@ -147,34 +158,34 @@ class DateImplementation implements Date { |
| // According to V8 some library calls have troubles with negative values. |
| // Therefore clamp to 0 - year 2035 (which is less than the size of 32bit). |
| if (secondsSinceEpoch >= 0 && secondsSinceEpoch < _SECONDS_YEAR_2035) { |
| - return _getYear(secondsSinceEpoch, timeZone.isUtc); |
| + return _getYear(secondsSinceEpoch, isUtc()); |
| } |
| // Approximate the result. We don't take timeZone into account. |
| int approximateYear = _yearsFromSecondsSinceEpoch(secondsSinceEpoch); |
| int equivalentYear = _equivalentYear(approximateYear); |
| - int y = _getYear(_equivalentSeconds(_secondsSinceEpoch), timeZone.isUtc); |
| + int y = _getYear(_equivalentSeconds(_secondsSinceEpoch), isUtc()); |
| return approximateYear + (y - equivalentYear); |
| } |
| int get month() { |
| - return _getMonth(_equivalentSeconds(_secondsSinceEpoch), timeZone.isUtc); |
| + return _getMonth(_equivalentSeconds(_secondsSinceEpoch), isUtc()); |
| } |
| int get day() { |
| - return _getDay(_equivalentSeconds(_secondsSinceEpoch), timeZone.isUtc); |
| + return _getDay(_equivalentSeconds(_secondsSinceEpoch), isUtc()); |
| } |
| int get hours() { |
| - return _getHours(_equivalentSeconds(_secondsSinceEpoch), timeZone.isUtc); |
| + return _getHours(_equivalentSeconds(_secondsSinceEpoch), isUtc()); |
| } |
| int get minutes() { |
| - return _getMinutes(_equivalentSeconds(_secondsSinceEpoch), timeZone.isUtc); |
| + return _getMinutes(_equivalentSeconds(_secondsSinceEpoch), isUtc()); |
| } |
| int get seconds() { |
| - return _getSeconds(_equivalentSeconds(_secondsSinceEpoch), timeZone.isUtc); |
| + return _getSeconds(_equivalentSeconds(_secondsSinceEpoch), isUtc()); |
| } |
| int get milliseconds() { |
| @@ -191,8 +202,7 @@ class DateImplementation implements Date { |
| } |
| int get weekday() { |
| - final Date unixTimeStart = |
| - new Date.withTimeZone(1970, 1, 1, 0, 0, 0, 0, timeZone); |
| + final Date unixTimeStart = new Date(1970, 1, 1, 0, 0, 0, 0, isUtc()); |
| int msSince1970 = this.difference(unixTimeStart).inMilliseconds; |
| // Adjust the milliseconds to avoid problems with summer-time. |
| if (hours < 2) { |
| @@ -210,13 +220,7 @@ class DateImplementation implements Date { |
| return ((daysSince1970 + Date.THU) % Date.DAYS_IN_WEEK); |
| } |
| - bool isLocalTime() { |
| - return !timeZone.isUtc; |
| - } |
| - |
| - bool isUtc() { |
| - return timeZone.isUtc; |
| - } |
| + bool isUtc() => _isUtcField; |
| String toString() { |
| String fourDigits(int n) { |
| @@ -244,7 +248,7 @@ class DateImplementation implements Date { |
| String min = twoDigits(minutes); |
| String sec = twoDigits(seconds); |
| String ms = threeDigits(milliseconds); |
| - if (timeZone.isUtc) { |
| + if (isUtc()) { |
| return "$y-$m-$d $h:$min:$sec.${ms}Z"; |
| } else { |
| return "$y-$m-$d $h:$min:$sec.$ms"; |
| @@ -254,13 +258,13 @@ class DateImplementation implements Date { |
| // Adds the [duration] to this Date instance. |
| Date add(Duration duration) { |
| return new DateImplementation.fromEpoch(value + duration.inMilliseconds, |
| - timeZone); |
| + isUtc()); |
| } |
| // Subtracts the [duration] from this Date instance. |
| Date subtract(Duration duration) { |
| return new DateImplementation.fromEpoch(value - duration.inMilliseconds, |
| - timeZone); |
| + isUtc()); |
| } |
| // Returns a [Duration] with the difference of [this] and [other]. |
| @@ -268,11 +272,6 @@ class DateImplementation implements Date { |
| return new DurationImplementation(milliseconds: value - other.value); |
| } |
| - final int value; |
| - final TimeZoneImplementation timeZone; |
| - |
| - static final int _SECONDS_YEAR_2035 = 2051222400; |
| - |
| // Returns the UTC year for the corresponding [secondsSinceEpoch]. |
| // It is relatively fast for values in the range 0 to year 2098. |
| // Code is adapted from V8. |