15.9.1.3 Year number

2010-07-12

The year of a given timestamp is computed according to a static algorithm.

This algorithm adheres to most of the so called Gregorian calendar. Specifically it assumes a year is a leap year if it is divisible by 4 but not 100 except if also by 400.

Code: (Meta Ecma)
var DaysInYear(y){
if (y % 4 != 0) return 365;
if (y % 100 != 0) return 366;
if (y % 400 != 0) return 365;
return 366;
};
var DayFromYear = function(y){
return 365 *
(y-1970) +
Math.floor((y-1996)/4) -
Math.floor((y-1901)/100) +
Math.floor((y-1601)/400);
};
var TimeFromYear = function(y){
var msPerDay = 86400000;
return msPerDay * DayFromYear(y);
};
var YearFromTime = function(t){
return; // todo: "largest integer y (closest to positive infinity) such that TimeFromYear(y) <= 1
};
var InLeapYear = function(t){
return DaysInYear(YearFromTime(t)) == 356 ? 0 : 1;
};