Bug 585866 - Avoid unnecessary calls to YearFromTime(). r=jwalden.

This commit is contained in:
Nicholas Nethercote 2010-08-18 19:44:28 -07:00
Родитель e994ce4ac5
Коммит 1b020d1c56
1 изменённых файлов: 22 добавлений и 14 удалений

Просмотреть файл

@ -189,8 +189,23 @@ TimeWithinDay(jsdouble t)
return result; return result;
} }
#define DaysInYear(y) ((y) % 4 == 0 && ((y) % 100 || ((y) % 400 == 0)) \ static inline bool
? 366 : 365) IsLeapYear(jsint year)
{
return year % 4 == 0 && (year % 100 || (year % 400 == 0));
}
static inline jsint
DaysInYear(jsint year)
{
return IsLeapYear(year) ? 366 : 365;
}
static inline jsint
DaysInFebruary(jsint year)
{
return IsLeapYear(year) ? 29 : 28;
}
/* math here has to be f.p, because we need /* math here has to be f.p, because we need
* floor((1968 - 1969) / 4) == -1 * floor((1968 - 1969) / 4) == -1
@ -219,8 +234,6 @@ YearFromTime(jsdouble t)
return y; return y;
} }
#define InLeapYear(t) (JSBool) (DaysInYear(YearFromTime(t)) == 366)
#define DayWithinYear(t, year) ((intN) (Day(t) - DayFromYear(year))) #define DayWithinYear(t, year) ((intN) (Day(t) - DayFromYear(year)))
/* /*
@ -237,7 +250,7 @@ static jsdouble firstDayOfMonth[2][13] = {
static intN static intN
DaysInMonth(jsint year, jsint month) DaysInMonth(jsint year, jsint month)
{ {
JSBool leap = (DaysInYear(year) == 366); JSBool leap = IsLeapYear(year);
intN result = intN(DayFromMonth(month, leap) - DayFromMonth(month-1, leap)); intN result = intN(DayFromMonth(month, leap) - DayFromMonth(month-1, leap));
return result; return result;
} }
@ -251,8 +264,7 @@ MonthFromTime(jsdouble t)
if (d < (step = 31)) if (d < (step = 31))
return 0; return 0;
step += (InLeapYear(t) ? 29 : 28); if (d < (step += DaysInFebruary(year)))
if (d < step)
return 1; return 1;
if (d < (step += 31)) if (d < (step += 31))
return 2; return 2;
@ -285,8 +297,7 @@ DateFromTime(jsdouble t)
if (d <= (next = 30)) if (d <= (next = 30))
return d + 1; return d + 1;
step = next; step = next;
next += (InLeapYear(t) ? 29 : 28); if (d <= (next += DaysInFebruary(year)))
if (d <= next)
return d - step; return d - step;
step = next; step = next;
if (d <= (next += 31)) if (d <= (next += 31))
@ -346,7 +357,7 @@ MakeDay(jsdouble year, jsdouble month, jsdouble date)
if (month < 0) if (month < 0)
month += 12; month += 12;
leap = (DaysInYear((jsint) year) == 366); leap = IsLeapYear((jsint) year);
yearday = floor(TimeFromYear(year) / msPerDay); yearday = floor(TimeFromYear(year) / msPerDay);
monthday = DayFromMonth(month, leap); monthday = DayFromMonth(month, leap);
@ -381,16 +392,13 @@ static jsint
EquivalentYearForDST(jsint year) EquivalentYearForDST(jsint year)
{ {
jsint day; jsint day;
JSBool isLeapYear;
day = (jsint) DayFromYear(year) + 4; day = (jsint) DayFromYear(year) + 4;
day = day % 7; day = day % 7;
if (day < 0) if (day < 0)
day += 7; day += 7;
isLeapYear = (DaysInYear(year) == 366); return yearStartingWith[IsLeapYear(year)][day];
return yearStartingWith[isLeapYear][day];
} }
/* LocalTZA gets set by js_InitDateClass() */ /* LocalTZA gets set by js_InitDateClass() */