Index: src/runtime.cc |
=================================================================== |
--- src/runtime.cc (revision 4006) |
+++ src/runtime.cc (working copy) |
@@ -4969,9 +4969,25 @@ |
ASSERT(month >= 0); |
ASSERT(month < 12); |
- static const int base_day = 365*1969 + 1969/4 - 1969/100 + 1969/400; |
- int year1 = year - 1; |
- int day_from_year = 365 * year1 + year1 / 4 - year1 / 100 + year1 / 400 - |
+ // year_delta is an arbitrary number such that: |
+ // a) year_delta = -1 (mod 400) |
+ // b) year + year_delta > 0 for years in the range defined by |
+ // ECMA 262 - 15.9.1.1, i.e. upto 100,000,000 days on either side of |
+ // Jan 1 1970. This is required so that we don't run into integer |
+ // division of negative numbers. |
+ // c) there shouldn't be overflow for 32-bit integers in the following |
+ // operations. |
+ static const int year_delta = 399999; |
+ static const int base_day = 365 * (1970 + year_delta) + |
+ (1970 + year_delta) / 4 - |
+ (1970 + year_delta) / 100 + |
+ (1970 + year_delta) / 400; |
+ |
+ int year1 = year + year_delta; |
+ int day_from_year = 365 * year1 + |
+ year1 / 4 - |
+ year1 / 100 + |
+ year1 / 400 - |
base_day; |
if (year % 4 || (year % 100 == 0 && year % 400 != 0)) { |