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

Unified Diff: runtime/lib/date.dart

Issue 10832166: Updated VM and JS versions of Date to allow underflow and overflow. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: REmove extra days, add back throw expectation Created 8 years, 4 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: runtime/lib/date.dart
diff --git a/runtime/lib/date.dart b/runtime/lib/date.dart
index 71c079fa7b6e40c1ed5d2b10b0efce6c3a5cc51a..59bdb23a915b2dc54a304081ab118aababb97c3d 100644
--- a/runtime/lib/date.dart
+++ b/runtime/lib/date.dart
@@ -7,7 +7,7 @@
class DateImplementation implements Date {
static final int _MAX_MILLISECONDS_SINCE_EPOCH = 8640000000000000;
- DateImplementation(int years,
+ DateImplementation(int year,
Lasse Reichstein Nielsen 2012/08/24 08:39:02 Please remove "Implementation". It's never a good
[int month = 1,
int day = 1,
int hour = 0,
@@ -17,7 +17,7 @@ class DateImplementation implements Date {
bool isUtc = false])
: this.isUtc = isUtc,
this.millisecondsSinceEpoch = _brokenDownDateToMillisecondsSinceEpoch(
- years, month, day, hour, minute, second, millisecond, isUtc) {
+ year, month, day, hour, minute, second, millisecond, isUtc) {
if (millisecondsSinceEpoch === null) throw new IllegalArgumentException();
if (isUtc === null) throw new IllegalArgumentException();
}
@@ -54,7 +54,7 @@ class DateImplementation implements Date {
return Math.parseDouble(matched);
}
- int years = Math.parseInt(match[1]);
+ int year = Math.parseInt(match[1]);
int month = Math.parseInt(match[2]);
int day = Math.parseInt(match[3]);
int hour = parseIntOrZero(match[4]);
@@ -69,7 +69,7 @@ class DateImplementation implements Date {
// 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);
+ year, month, day, hour, minute, second, millisecond, isUtc);
if (millisecondsSinceEpoch === null) {
throw new IllegalArgumentException(formattedString);
}
@@ -340,23 +340,26 @@ class DateImplementation implements Date {
}
static _brokenDownDateToMillisecondsSinceEpoch(
- int years, int month, int day,
+ int year, int month, int day,
int hour, int minute, int second, int millisecond,
bool isUtc) {
- if ((month < 1) || (month > 12)) return null;
- if ((day < 1) || (day > 31)) return null;
- // Leap seconds can lead to hour == 24.
- if ((hour < 0) || (hour > 24)) return null;
- if ((hour == 24) && ((minute != 0) || (second != 0))) return null;
- if ((minute < 0) || (minute > 59)) return null;
- if ((second < 0) || (second > 59)) return null;
- if ((millisecond < 0) || (millisecond > 999)) return null;
+ // JavaScript month is zero-based while Dart is one-based. This should
+ // simplify some calculations.
Lasse Reichstein Nielsen 2012/08/24 08:39:02 Why reference JavaScript here? This is VM code, ri
floitsch 2012/08/24 22:32:49 My fault.
+ --month;
+ // Deal with under and overflow.
+ year += (month / 12).floor().toInt();
+ month = month % 12;
+ int clampedMonth = month.remainder(12);
Lasse Reichstein Nielsen 2012/08/24 08:39:02 How can this be necessary when %12 above gives a v
floitsch 2012/08/24 22:32:49 Agreed. Either use year += month ~/ 12; clampedMo
+ if (clampedMonth < 0) {
+ --year;
+ clampedMonth += 12;
+ }
// First compute the seconds in UTC, independent of the [isUtc] flag. If
// necessary we will add the time-zone offset later on.
int days = day - 1;
- days += _DAYS_UNTIL_MONTH[_isLeapYear(years) ? 1 : 0][month - 1];
- days += _dayFromYear(years);
+ days += _DAYS_UNTIL_MONTH[_isLeapYear(year) ? 1 : 0][clampedMonth];
+ days += _dayFromYear(year);
int millisecondsSinceEpoch = days * Duration.MILLISECONDS_PER_DAY +
hour * Duration.MILLISECONDS_PER_HOUR +
minute * Duration.MILLISECONDS_PER_MINUTE+

Powered by Google App Engine
This is Rietveld 408576698