Permalink
Browse files

Merge pull request #72 from millermedeiros/date-leap

Date isLeapYear + totalDaysInMonth
  • Loading branch information...
2 parents 43d2eaa + 325e9a6 commit dd32131c24f06e5bcfffb7beb17cd3e229b4cc55 @millermedeiros millermedeiros committed Apr 4, 2013
View
@@ -10,6 +10,8 @@ Next
- normalize behavior in `array/*` methods to treat `null` values as empty
arrays when reading from array
- add `date/parseIso`
+ - add `date/isLeapYear`
+ - add `date/totalDaysInMonth`
- add `object/deepMatches`
- change `function/makeIterator_` to use `deepMatches` (affects nearly all
iteration methods)
View
@@ -4,6 +4,20 @@ Date utilities.
+## isLeapYear(fullYear|date):Boolean
+
+Checks if it's a [leap year](http://en.wikipedia.org/wiki/Leap_year) according
+to the Gregorian calendar.
+
+see: [`totalDaysInMonth()`](#totalDaysInMonth)
+
+```js
+isLeapYear(2012); // true
+isLeapYear(2013); // false
+isLeapYear(new Date(2012, 2, 28)); // true
+```
+
+
## parseIso(str):Number
@@ -31,6 +45,20 @@ parseIso('2000-01-02T20:10+04:00') // 946829400000
```
+## totalDaysInMonth(fullYear, monthIndex):Number
+
+Returns the amount of days in the month taking into consideration leap years
+(following Gregorian calendar).
+
+see: [`isLeapYear()`](#isLeapYear)
+
+```js
+totalDaysInMonth(2008, 1); // 29 (leap year)
+totalDaysInMonth(2009, 1); // 28
+
+// you can also pass a Date object as single argument
+totalDaysInMonth( new Date(2013, 0, 1) ); // 31
+```
-------------------------------------------------------------------------------
View
@@ -3,7 +3,9 @@ define(function(require){
//automatically generated, do not edit!
//run `node build` instead
return {
- 'parseIso' : require('./date/parseIso')
+ 'isLeapYear' : require('./date/isLeapYear'),
+ 'parseIso' : require('./date/parseIso'),
+ 'totalDaysInMonth' : require('./date/totalDaysInMonth')
};
});
View
@@ -0,0 +1,15 @@
+define(['../lang/isDate'], function (isDate) {
+
+ /**
+ * checks if it's a leap year
+ */
+ function isLeapYear(fullYear){
+ if (isDate(fullYear)) {
+ fullYear = fullYear.getFullYear();
+ }
+ return fullYear % 400 === 0 || (fullYear % 100 !== 0 && fullYear % 4 === 0);
+ }
+
+ return isLeapYear;
+
+});
@@ -0,0 +1,24 @@
+define(['../lang/isDate', './isLeapYear'], function (isDate, isLeapYear) {
+
+ var DAYS_IN_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
+
+ /**
+ * returns the total amount of days in the month (considering leap years)
+ */
+ function totalDaysInMonth(fullYear, monthIndex){
+ if (isDate(fullYear)) {
+ var date = fullYear;
+ year = date.getFullYear();
+ monthIndex = date.getMonth();
+ }
+
+ if (monthIndex === 1 && isLeapYear(fullYear)) {
+ return 29;
+ } else {
+ return DAYS_IN_MONTH[monthIndex];
+ }
+ }
+
+ return totalDaysInMonth;
+
+});
@@ -0,0 +1,37 @@
+define(['mout/date/isLeapYear'], function(isLeapYear){
+
+ describe('date/isLeapYear', function(){
+
+ it('should check if it is a leap year', function(){
+ expect( isLeapYear(2000) ).toBe( true );
+ // gregorian calendar skips 3 leap years each 400 years
+ expect( isLeapYear(2100) ).toBe( false );
+ expect( isLeapYear(2200) ).toBe( false );
+ expect( isLeapYear(2300) ).toBe( false );
+
+ expect( isLeapYear(2008) ).toBe( true );
+ expect( isLeapYear(2009) ).toBe( false );
+ expect( isLeapYear(2010) ).toBe( false );
+ expect( isLeapYear(2011) ).toBe( false );
+ expect( isLeapYear(2012) ).toBe( true );
+ expect( isLeapYear(2013) ).toBe( false );
+ });
+
+ it('should work with Date objects', function () {
+ expect( isLeapYear(new Date(2000, 2, 28)) ).toBe( true );
+ // gregorian calendar skips 3 leap years each 400 years
+ expect( isLeapYear(new Date(2100, 2, 28)) ).toBe( false );
+ expect( isLeapYear(new Date(2200, 2, 28)) ).toBe( false );
+ expect( isLeapYear(new Date(2300, 2, 28)) ).toBe( false );
+
+ expect( isLeapYear(new Date(2008, 2, 28)) ).toBe( true );
+ expect( isLeapYear(new Date(2009, 2, 28)) ).toBe( false );
+ expect( isLeapYear(new Date(2010, 2, 28)) ).toBe( false );
+ expect( isLeapYear(new Date(2011, 2, 28)) ).toBe( false );
+ expect( isLeapYear(new Date(2012, 2, 28)) ).toBe( true );
+ expect( isLeapYear(new Date(2013, 2, 28)) ).toBe( false );
+ });
+
+ });
+
+});
@@ -0,0 +1,42 @@
+define(['mout/date/totalDaysInMonth'], function(totalDaysInMonth){
+
+ describe('date/totalDaysInMonth', function(){
+
+ it('should return number of days in the month', function () {
+ expect( totalDaysInMonth(2013, 0) ).toEqual( 31 );
+ expect( totalDaysInMonth(2013, 1) ).toEqual( 28 );
+ expect( totalDaysInMonth(2013, 2) ).toEqual( 31 );
+ expect( totalDaysInMonth(2013, 3) ).toEqual( 30 );
+ expect( totalDaysInMonth(2013, 4) ).toEqual( 31 );
+ expect( totalDaysInMonth(2013, 5) ).toEqual( 30 );
+ expect( totalDaysInMonth(2013, 6) ).toEqual( 31 );
+ expect( totalDaysInMonth(2013, 7) ).toEqual( 31 );
+ expect( totalDaysInMonth(2013, 8) ).toEqual( 30 );
+ expect( totalDaysInMonth(2013, 9) ).toEqual( 31 );
+ expect( totalDaysInMonth(2013, 10) ).toEqual( 30 );
+ expect( totalDaysInMonth(2013, 11) ).toEqual( 31 );
+ });
+
+ it('should work with leap years', function () {
+ expect( totalDaysInMonth(2000, 1) ).toEqual( 29 );
+ // gregorian calendar skips 3 leap years each 400 years
+ expect( totalDaysInMonth(2100, 1) ).toEqual( 28 );
+ expect( totalDaysInMonth(2200, 1) ).toEqual( 28 );
+ expect( totalDaysInMonth(2300, 1) ).toEqual( 28 );
+
+ expect( totalDaysInMonth(2008, 1) ).toEqual( 29 );
+ expect( totalDaysInMonth(2009, 1) ).toEqual( 28 );
+ expect( totalDaysInMonth(2010, 1) ).toEqual( 28 );
+ expect( totalDaysInMonth(2011, 1) ).toEqual( 28 );
+ expect( totalDaysInMonth(2012, 1) ).toEqual( 29 );
+ expect( totalDaysInMonth(2013, 1) ).toEqual( 28 );
+ });
+
+ it('should work with Date objects', function () {
+ expect( totalDaysInMonth(new Date(2000, 1, 23)) ).toEqual( 29 );
+ expect( totalDaysInMonth(new Date(2200, 1, 23)) ).toEqual( 28 );
+ });
+
+ });
+
+});
View
@@ -1,7 +1,9 @@
//automatically generated, do not edit!
//run `node build` instead
define([
- './date/spec-parseIso'
+ './date/spec-isLeapYear',
+ './date/spec-parseIso',
+ './date/spec-totalDaysInMonth'
], function(){
//noop
});

0 comments on commit dd32131

Please sign in to comment.