]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Better bubbling of days to years in duration
authorIskren Chernev <iskren.chernev@gmail.com>
Sat, 26 Apr 2014 19:52:01 +0000 (12:52 -0700)
committerIskren Chernev <iskren.chernev@gmail.com>
Thu, 19 Jun 2014 04:56:11 +0000 (21:56 -0700)
moment.js
test/moment/duration.js
test/moment/duration_from_moments.js

index f2604d825d532f0665fc1bfc518842b0638b8ffc..ff6641d39d33c51bc62b7781edd65b1e42edf6c8 100644 (file)
--- a/moment.js
+++ b/moment.js
                 days = this._days,
                 months = this._months,
                 data = this._data,
-                seconds, minutes, hours, years;
+                seconds, minutes, hours, years = 0;
 
             // The following code bubbles up values, see the tests for
             // examples of what that means.
             data.hours = hours % 24;
 
             days += absRound(hours / 24);
-            data.days = days % 30;
 
+            // Accurately convert days to years, assume start from year 0.
+            years = absRound(days * 400 / 146097);
+            days -= years * 365 + absRound(years / 4) -
+                absRound(years / 100) + absRound(years / 400);
+
+            // 30 days to a month
+            // TODO (iskren): Use anchor date (like 1st Jan) to compute this.
             months += absRound(days / 30);
-            data.months = months % 12;
+            days %= 30;
+
+            // 12 months -> 1 year
+            years += absRound(months / 12);
+            months %= 12;
 
-            years = absRound(months / 12);
+            data.days = days;
+            data.months = months;
             data.years = years;
         },
 
index 084a3db3ce8b70eabded715974114bdd4143f6e1..39673d8d5c705edafbb4d2327ccca540f57bb698 100644 (file)
@@ -243,24 +243,30 @@ exports.duration = {
     },
 
     "instatiation from serialized C# TimeSpan maxValue" : function (test) {
-        test.expect(6);
-        test.equal(moment.duration("10675199.02:48:05.4775807").years(), 29653, "29653 years");
-        test.equal(moment.duration("10675199.02:48:05.4775807").days(), 29, "29 day");
-        test.equal(moment.duration("10675199.02:48:05.4775807").hours(), 2, "2 hours");
-        test.equal(moment.duration("10675199.02:48:05.4775807").minutes(), 48, "48 minutes");
-        test.equal(moment.duration("10675199.02:48:05.4775807").seconds(), 5, "5 seconds");
-        test.equal(moment.duration("10675199.02:48:05.4775807").milliseconds(), 477, "477 milliseconds");
+        var d = moment.duration("10675199.02:48:05.4775807");
+
+        test.equal(d.years(), 29227, "29227 years");
+        test.equal(d.months(), 8, "8 months");
+        test.equal(d.days(), 17, "17 day");  // this should be 13
+
+        test.equal(d.hours(), 2, "2 hours");
+        test.equal(d.minutes(), 48, "48 minutes");
+        test.equal(d.seconds(), 5, "5 seconds");
+        test.equal(d.milliseconds(), 477, "477 milliseconds");
         test.done();
     },
 
     "instatiation from serialized C# TimeSpan minValue" : function (test) {
-        test.expect(6);
-        test.equal(moment.duration("-10675199.02:48:05.4775808").years(), -29653, "29653 years");
-        test.equal(moment.duration("-10675199.02:48:05.4775808").days(), -29, "29 day");
-        test.equal(moment.duration("-10675199.02:48:05.4775808").hours(), -2, "2 hours");
-        test.equal(moment.duration("-10675199.02:48:05.4775808").minutes(), -48, "48 minutes");
-        test.equal(moment.duration("-10675199.02:48:05.4775808").seconds(), -5, "5 seconds");
-        test.equal(moment.duration("-10675199.02:48:05.4775808").milliseconds(), -477, "477 milliseconds");
+        var d = moment.duration("-10675199.02:48:05.4775808");
+
+        test.equal(d.years(), -29227, "29653 years");
+        test.equal(d.months(), -8, "8 day");
+        test.equal(d.days(), -17, "17 day"); // this should be 13
+
+        test.equal(d.hours(), -2, "2 hours");
+        test.equal(d.minutes(), -48, "48 minutes");
+        test.equal(d.seconds(), -5, "5 seconds");
+        test.equal(d.milliseconds(), -477, "477 milliseconds");
         test.done();
     },
 
index 71d0f98b0045c34d6187193b1ecf91cf9e72554f..268797558ef863b2c1f183824e7e802a37652aa6 100644 (file)
@@ -1,6 +1,6 @@
 var moment = require("../../moment");
 
-exports.duration_from_moments = {
+exports.durationFromMoments = {
     setUp: function (done) {
         moment.createFromInputFallback = function () {
             throw new Error("input not handled by moment");