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

Unified Diff: runtime/lib/date_patch.dart

Issue 830143003: Optimize DateTime constructor slightly. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix bad edit. Created 5 years, 11 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 | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/lib/date_patch.dart
diff --git a/runtime/lib/date_patch.dart b/runtime/lib/date_patch.dart
index 9411bf3f0d91a079851afad8f5675304f808ba86..0d6065d9d60aa24b583a017cf0892aa786899db2 100644
--- a/runtime/lib/date_patch.dart
+++ b/runtime/lib/date_patch.dart
@@ -200,8 +200,8 @@ patch class DateTime {
}
static bool _isLeapYear(y) {
- return (y.remainder(4) == 0) &&
- ((y.remainder(100) != 0) || (y.remainder(400) == 0));
+ // (y % 16 == 0) matches multiples of 400, and is faster than % 400.
+ return (y % 4 == 0) && ((y % 16 == 0) || (y % 100 != 0));
}
/* patch */ static int _brokenDownDateToMillisecondsSinceEpoch(
@@ -211,17 +211,24 @@ patch class DateTime {
// Simplify calculations by working with zero-based month.
--month;
// Deal with under and overflow.
- year += (month / 12).floor();
- month = month % 12;
+ if (month >= 12) {
+ year += month ~/ 12;
+ month = month % 12;
+ } else if (month < 0) {
+ int realMonth = month % 12;
+ year += (month - realMonth) ~/ 12;
+ month = realMonth;
+ }
// 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(year) ? 1 : 0][month];
days += _dayFromYear(year);
- int millisecondsSinceEpoch = days * Duration.MILLISECONDS_PER_DAY +
- hour * Duration.MILLISECONDS_PER_HOUR +
- minute * Duration.MILLISECONDS_PER_MINUTE+
+ int millisecondsSinceEpoch =
+ days * Duration.MILLISECONDS_PER_DAY +
+ hour * Duration.MILLISECONDS_PER_HOUR +
+ minute * Duration.MILLISECONDS_PER_MINUTE +
second * Duration.MILLISECONDS_PER_SECOND +
millisecond;
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698