From dc1ec954fb8d8282dc2f7a38dbe388facd155f2a Mon Sep 17 00:00:00 2001 From: rolux Date: Sun, 24 Apr 2011 00:42:10 +0200 Subject: [PATCH] cleanup --- source/js/Ox.js | 67 ++++++++++++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 28 deletions(-) diff --git a/source/js/Ox.js b/source/js/Ox.js index 42667cb1..c202322e 100644 --- a/source/js/Ox.js +++ b/source/js/Ox.js @@ -940,25 +940,33 @@ Ox.getDayOfTheYear = function(date, utc) { })) + Ox.getDate(date, utc); }; -Ox.getDaysInMonth = function(year, month) { +Ox.getDaysInMonth = function(year, month, utc) { /* >>> Ox.getDaysInMonth(2000, 2) 29 >>> Ox.getDaysInMonth("2002", "Feb") 28 - >>> Ox.getDaysInMonth("2004", "February") + >>> Ox.getDaysInMonth(new Date('01/01/2004'), "February") 29 */ - var year = parseInt(year), - month = Ox.isNumber(month) ? month : - Ox.map(Ox.MONTHS, function(v, i) { - return v.substr(0, 3) == month.substr(0, 3) ? i + 1 : null; - })[0]; + year = Ox.makeYear(year); + month = Ox.isNumber(month) ? month : + Ox.map(Ox.MONTHS, function(v, i) { + return v.substr(0, 3) == month.substr(0, 3) ? i + 1 : null; + })[0]; return new Date(year, month, 0).getDate(); } -Ox.getDaysInYear = function(year) { - return 365 + Ox.isLeapYear(year); +Ox.getDaysInYear = function(year, utc) { + /* + >>> Ox.getDaysInYear(1900) + 365 + >>> Ox.getDaysInYear('2000') + 366 + >>> Ox.getDaysInYear(new Date('01/01/2004')) + 366 + */ + return 365 + Ox.isLeapYear(Ox.makeYear(year, utc)); }; Ox.getFirstDayOfTheYear = function(date, utc) { @@ -1060,15 +1068,16 @@ Ox.getWeek = function(date, utc) { Ox.getFirstDayOfTheYear(date, utc) - 1) / 7); }; -Ox.isLeapYear = function(year) { +Ox.isLeapYear = function(year, utc) { /* >>> Ox.isLeapYear(1900) false - >>> Ox.isLeapYear(2000) + >>> Ox.isLeapYear('2000') true - >>> Ox.isLeapYear(2004) + >>> Ox.isLeapYear(new Date('01/01/2004')) true */ + year = Ox.makeYear(year, utc); return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); }; @@ -1077,23 +1086,25 @@ Ox.makeDate = function(date) { Ox.isUndefined(date) ? new Date() : new Date(date); }; -['get', 'set'].forEach(function(verb) { - [ - 'FullYear', 'Month', 'Date', 'Day', 'Hours', 'Minutes', 'Seconds' - ].forEach(function(noun) { - Ox['get' + noun] = function(date, utc) { - return Ox.makeDate(date)['get' + (utc ? 'UTC' : '') + noun]() - } - Ox['set' + noun] = function(date, num, utc) { - // new Date(date) makes a clone, so that - // setSomething() doesn't have side effects - return new Date( - Ox.makeDate(date) - )['set' + (utc ? 'UTC' : '') + noun](num); - } - }); - +Ox.makeYear = function(date, utc) { + return Ox.isDate(date) ? Ox.getFullYear(date, utc) : parseInt(date); +}; + +[ + 'FullYear', 'Month', 'Date', 'Day', 'Hours', 'Minutes', 'Seconds' +].forEach(function(noun) { + Ox['get' + noun] = function(date, utc) { + return Ox.makeDate(date)['get' + (utc ? 'UTC' : '') + noun]() + } + Ox['set' + noun] = function(date, num, utc) { + // new Date(date) makes a clone, so that + // setSomething() doesn't have side effects + return new Date( + Ox.makeDate(date) + )['set' + (utc ? 'UTC' : '') + noun](num); + } }); + /* ================================================================================