]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
add/subtract quarters; startOf/endOf quarter
authorAndrei Cocorean <andrei-cocorean@users.noreply.github.com>
Thu, 3 Apr 2014 14:58:27 +0000 (16:58 +0200)
committerAndrei Cocorean <andrei-cocorean@users.noreply.github.com>
Thu, 3 Apr 2014 16:02:23 +0000 (18:02 +0200)
moment.js
test/moment/add_subtract.js
test/moment/sod_eod.js

index 152d0a7b9564e9d4dca91f624d5ebdcd5b2f3b50..7c9e5abbfb2fbf28f3916c09c7ae9f593318c608 100644 (file)
--- a/moment.js
+++ b/moment.js
             w : 'week',
             W : 'isoWeek',
             M : 'month',
+            Q : 'quarter',
             y : 'year',
             DDD : 'dayOfYear',
             e : 'weekday',
     function Duration(duration) {
         var normalizedInput = normalizeObjectUnits(duration),
             years = normalizedInput.year || 0,
+            quarters = normalizedInput.quarter || 0,
             months = normalizedInput.month || 0,
             weeks = normalizedInput.week || 0,
             days = normalizedInput.day || 0,
         // which months you are are talking about, so we have to store
         // it separately.
         this._months = +months +
+            quarters * 3 +
             years * 12;
 
         this._data = {};
             case 'year':
                 this.month(0);
                 /* falls through */
+            case 'quarter':
             case 'month':
                 this.date(1);
                 /* falls through */
                 this.isoWeekday(1);
             }
 
+            // quarters are also special
+            if (units === 'quarter') {
+                this.month(Math.floor(this.month() / 3) * 3);
+            }
+
             return this;
         },
 
index 0dbd5d207b072c824d052ff20b1d34c79a0e0d65..c9ab587218a068a455bbd38032dd037e58c06ed5 100644 (file)
@@ -9,9 +9,9 @@ exports.add = {
     },
 
     "add short" : function (test) {
-        test.expect(12);
+        test.expect(16);
 
-        var a = moment(), b, c;
+        var a = moment(), b, c, d;
         a.year(2011);
         a.month(9);
         a.date(12);
@@ -28,19 +28,24 @@ exports.add = {
         test.equal(a.add({w: 1}).date(), 20, 'Add week');
         test.equal(a.add({M: 1}).month(), 10, 'Add month');
         test.equal(a.add({y: 1}).year(), 2012, 'Add year');
+        test.equal(a.add({Q: 1}).month(), 1, 'Add quarter');
 
         b = moment([2010, 0, 31]).add({M: 1});
         c = moment([2010, 1, 28]).subtract({M: 1});
+        d = moment([2010, 1, 28]).subtract({Q: 1});
 
         test.equal(b.month(), 1, 'add month, jan 31st to feb 28th');
         test.equal(b.date(), 28, 'add month, jan 31st to feb 28th');
         test.equal(c.month(), 0, 'subtract month, feb 28th to jan 28th');
         test.equal(c.date(), 28, 'subtract month, feb 28th to jan 28th');
+        test.equal(d.month(), 10, 'subtract quarter, feb 28th 2010 to nov 28th 2009');
+        test.equal(d.date(), 28, 'subtract quarter, feb 28th 2010 to nov 28th 2009');
+        test.equal(d.year(), 2009, 'subtract quarter, feb 28th 2010 to nov 28th 2009');
         test.done();
     },
 
     "add long" : function (test) {
-        test.expect(8);
+        test.expect(9);
 
         var a = moment();
         a.year(2011);
@@ -59,11 +64,12 @@ exports.add = {
         test.equal(a.add({weeks: 1}).date(), 20, 'Add week');
         test.equal(a.add({months: 1}).month(), 10, 'Add month');
         test.equal(a.add({years: 1}).year(), 2012, 'Add year');
+        test.equal(a.add({quarters: 1}).month(), 1, 'Add quarter');
         test.done();
     },
 
     "add long singular" : function (test) {
-        test.expect(8);
+        test.expect(9);
 
         var a = moment();
         a.year(2011);
@@ -82,11 +88,12 @@ exports.add = {
         test.equal(a.add({week: 1}).date(), 20, 'Add week');
         test.equal(a.add({month: 1}).month(), 10, 'Add month');
         test.equal(a.add({year: 1}).year(), 2012, 'Add year');
+        test.equal(a.add({quarter: 1}).month(), 1, 'Add quarter');
         test.done();
     },
 
     "add string long" : function (test) {
-        test.expect(9);
+        test.expect(10);
 
         var a = moment(), b;
         a.year(2011);
@@ -108,11 +115,12 @@ exports.add = {
         test.equal(a.add('month', 1).month(), 10, 'Add month');
         test.equal(a.add('year', 1).year(), 2012, 'Add year');
         test.equal(b.add('day', '01').date(), 13, 'Add date');
+        test.equal(a.add('quarter', 1).month(), 1, 'Add quarter');
         test.done();
     },
 
     "add string long singular" : function (test) {
-        test.expect(9);
+        test.expect(10);
 
         var a = moment(), b;
         a.year(2011);
@@ -134,11 +142,12 @@ exports.add = {
         test.equal(a.add('months', 1).month(), 10, 'Add month');
         test.equal(a.add('years', 1).year(), 2012, 'Add year');
         test.equal(b.add('days', '01').date(), 13, 'Add date');
+        test.equal(a.add('quarters', 1).month(), 1, 'Add quarter');
         test.done();
     },
 
     "add string short" : function (test) {
-        test.expect(8);
+        test.expect(9);
 
         var a = moment();
         a.year(2011);
@@ -157,11 +166,12 @@ exports.add = {
         test.equal(a.add('w', 1).date(), 20, 'Add week');
         test.equal(a.add('M', 1).month(), 10, 'Add month');
         test.equal(a.add('y', 1).year(), 2012, 'Add year');
+        test.equal(a.add('Q', 1).month(), 1, 'Add quarter');
         test.done();
     },
 
     "add string long reverse args" : function (test) {
-        test.expect(8);
+        test.expect(9);
 
         var a = moment();
         a.year(2011);
@@ -173,18 +183,19 @@ exports.add = {
         a.milliseconds(500);
 
         test.equal(a.add(50, 'millisecond').milliseconds(), 550, 'Add milliseconds');
-        test.equal(a.add(1, 'second', 1).seconds(), 9, 'Add seconds');
-        test.equal(a.add(1, 'minute', 1).minutes(), 8, 'Add minutes');
-        test.equal(a.add(1, 'hour', 1).hours(), 7, 'Add hours');
-        test.equal(a.add(1, 'day', 1).date(), 13, 'Add date');
-        test.equal(a.add(1, 'week', 1).date(), 20, 'Add week');
-        test.equal(a.add(1, 'month', 1).month(), 10, 'Add month');
-        test.equal(a.add(1, 'year', 1).year(), 2012, 'Add year');
+        test.equal(a.add(1, 'second').seconds(), 9, 'Add seconds');
+        test.equal(a.add(1, 'minute').minutes(), 8, 'Add minutes');
+        test.equal(a.add(1, 'hour').hours(), 7, 'Add hours');
+        test.equal(a.add(1, 'day').date(), 13, 'Add date');
+        test.equal(a.add(1, 'week').date(), 20, 'Add week');
+        test.equal(a.add(1, 'month').month(), 10, 'Add month');
+        test.equal(a.add(1, 'year').year(), 2012, 'Add year');
+        test.equal(a.add(1, 'quarter').month(), 1, 'Add quarter');
         test.done();
     },
 
     "add string long singular reverse args" : function (test) {
-        test.expect(8);
+        test.expect(9);
 
         var a = moment();
         a.year(2011);
@@ -203,11 +214,12 @@ exports.add = {
         test.equal(a.add(1, 'weeks').date(), 20, 'Add week');
         test.equal(a.add(1, 'months').month(), 10, 'Add month');
         test.equal(a.add(1, 'years').year(), 2012, 'Add year');
+        test.equal(a.add(1, 'quarters').month(), 1, 'Add quarter');
         test.done();
     },
 
     "add string short reverse args" : function (test) {
-        test.expect(8);
+        test.expect(9);
 
         var a = moment();
         a.year(2011);
@@ -226,6 +238,7 @@ exports.add = {
         test.equal(a.add(1, 'w').date(), 20, 'Add week');
         test.equal(a.add(1, 'M').month(), 10, 'Add month');
         test.equal(a.add(1, 'y').year(), 2012, 'Add year');
+        test.equal(a.add(1, 'Q').month(), 1, 'Add quarter');
         test.done();
     },
 
@@ -240,10 +253,12 @@ exports.add = {
         var a = moment(new Date(2011, 2, 12, 5, 0, 0)),
             b = moment(new Date(2011, 2, 12, 5, 0, 0)),
             c = moment(new Date(2011, 2, 12, 5, 0, 0)),
-            d = moment(new Date(2011, 2, 12, 5, 0, 0));
+            d = moment(new Date(2011, 2, 12, 5, 0, 0)),
+            e = moment(new Date(2011, 2, 12, 5, 0, 0));
         a.add('days', 1);
         b.add('hours', 24);
         c.add('months', 1);
+        e.add('quarter', 1);
         test.equal(a.hours(), 5, 'adding days over DST difference should result in the same hour');
         if (b.isDST() && !d.isDST()) {
             test.equal(b.hours(), 6, 'adding hours over DST difference should result in a different hour');
@@ -253,6 +268,7 @@ exports.add = {
             test.equal(b.hours(), 5, 'adding hours over DST difference should result in a same hour if the timezone does not have daylight savings time');
         }
         test.equal(c.hours(), 5, 'adding months over DST difference should result in the same hour');
+        test.equal(e.hours(), 5, 'adding quarters over DST difference should result in the same hour');
         test.done();
     }
 };
index 65e2b2ab11b7239a655759fa0a0ced197f3d6575..7b72ae7ab1f5cab51284b3d730d445a80febce76 100644 (file)
@@ -50,6 +50,44 @@ exports.endStartOf = {
         test.done();
     },
 
+    "start of quarter" : function (test) {
+        test.expect(10);
+
+        var m = moment(new Date(2011, 4, 2, 3, 4, 5, 6)).startOf('quarter'),
+            ms = moment(new Date(2011, 4, 2, 3, 4, 5, 6)).startOf('quarters'),
+            ma = moment(new Date(2011, 4, 2, 3, 4, 5, 6)).startOf('Q');
+        test.equal(+m, +ms, "Plural or singular should work");
+        test.equal(+m, +ma, "Full or abbreviated should work");
+        test.equal(m.year(), 2011, "keep the year");
+        test.equal(m.quarter(), 2, "keep the quarter");
+        test.equal(m.month(), 3, "strip out the month");
+        test.equal(m.date(), 1, "strip out the day");
+        test.equal(m.hours(), 0, "strip out the hours");
+        test.equal(m.minutes(), 0, "strip out the minutes");
+        test.equal(m.seconds(), 0, "strip out the seconds");
+        test.equal(m.milliseconds(), 0, "strip out the milliseconds");
+        test.done();
+    },
+
+    "end of quarter" : function (test) {
+        test.expect(10);
+
+        var m = moment(new Date(2011, 4, 2, 3, 4, 5, 6)).endOf('quarter'),
+            ms = moment(new Date(2011, 4, 2, 3, 4, 5, 6)).endOf('quarters'),
+            ma = moment(new Date(2011, 4, 2, 3, 4, 5, 6)).endOf('Q');
+        test.equal(+m, +ms, "Plural or singular should work");
+        test.equal(+m, +ma, "Full or abbreviated should work");
+        test.equal(m.year(), 2011, "keep the year");
+        test.equal(m.quarter(), 2, "keep the quarter");
+        test.equal(m.month(), 5, "set the month");
+        test.equal(m.date(), 30, "set the day");
+        test.equal(m.hours(), 23, "set the hours");
+        test.equal(m.minutes(), 59, "set the minutes");
+        test.equal(m.seconds(), 59, "set the seconds");
+        test.equal(m.milliseconds(), 999, "set the seconds");
+        test.done();
+    },
+
     "start of month" : function (test) {
         test.expect(9);