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

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: clean up unnecessary hoop-jumping 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..18c3bef8305936b46e5ac82deff2f2bac323281e 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,
[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);
}
@@ -246,8 +246,9 @@ class DateImplementation implements Date {
/** The first list contains the days until each month in non-leap years. The
* second list contains the days in leap years. */
static final List<List<int>> _DAYS_UNTIL_MONTH =
- const [const [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334],
- const [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335]];
+ const [
+ const [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365],
floitsch 2012/08/15 08:44:26 No need for the additional days anymore.
dominich 2012/08/17 16:38:33 Done.
+ const [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366]];
// Returns the UTC year, month and day for the corresponding
// [millisecondsSinceEpoch].
@@ -340,23 +341,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.
+ --month;
+ // Deal with under and overflow.
+ year += (month / 12).floor().toInt();
+ month = month % 12;
+ int clampedMonth = month.remainder(12);
+ 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