]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
adding duration toIsoString, tests
authorNicholas Bollweg <nicholas.bollweg@gtri.gatech.edu>
Wed, 14 Aug 2013 18:01:50 +0000 (14:01 -0400)
committerNicholas Bollweg <nicholas.bollweg@gtri.gatech.edu>
Wed, 14 Aug 2013 18:01:50 +0000 (14:01 -0400)
moment.js
test/moment/duration.js

index 63c9252ccf02abe74ca8cd4928ffdfc1cd9f41cb..7dd6ddcaa97fdbc66f0cbed8581498bc846da9a4 100644 (file)
--- a/moment.js
+++ b/moment.js
         this._bubble();
     }
 
-
     /************************************
         Helpers
     ************************************/
             return this['as' + units.charAt(0).toUpperCase() + units.slice(1) + 's']();
         },
 
-        lang : moment.fn.lang
+        lang : moment.fn.lang,
+        
+        toIsoString : function () {
+            // inspired by https://github.com/dordille/moment-isoduration/blob/master/moment.isoduration.js
+            return (this.asSeconds() < 0 ? '-' : '') 
+                + 'P'
+                + (this.years() ? Math.abs(this.years()) + 'Y' : '')
+                + (this.months() ? Math.abs(this.months()) + 'M' : '')
+                + (this.days() ? Math.abs(this.days()) + 'D' : '')
+                + ((this.hours() || this.minutes() || this.seconds()) ? 'T' : '')
+                + (this.hours() ? Math.abs(this.hours()) + 'H' : '')
+                + (this.minutes() ? Math.abs(this.minutes()) + 'M' : '')
+                + ((this.seconds() || this.milliseconds()) ?
+                    (Math.abs(this.seconds() + this.milliseconds() / 1000.0)) + 'S' : '');
+        }
     };
 
     function makeDurationGetter(name) {
index 5d2953d9527b81c935d7ed6b2c22e91e5becce8b..1cee4fc6f73a7648d986f0a0e9962b2818c2b0ee 100644 (file)
@@ -198,17 +198,26 @@ exports.duration = {
         test.done();
     },
 
-    "instatiation from iso 8601 duration" : function (test) {
-        test.expect(7);
+    "instantiation from ISO 8601 duration" : function (test) {
+        test.expect(6);
         test.equal(moment.duration("P1Y2M3DT4H5M6S").asSeconds(), moment.duration({y: 1, M: 2, d: 3, h: 4, m: 5, s: 6}).asSeconds(), "all fields");
-        test.equal(moment.duration("P1W").asSeconds(), moment.duration({d: 7}).asSeconds(), "week field");
         test.equal(moment.duration("P1M").asSeconds(), moment.duration({M: 1}).asSeconds(), "single month field");
         test.equal(moment.duration("PT1M").asSeconds(), moment.duration({m: 1}).asSeconds(), "single minute field");
         test.equal(moment.duration("P1MT2H").asSeconds(), moment.duration({M: 1, h: 2}).asSeconds(), "random fields missing");
-        test.equal(moment.duration("PY1MDT2HMS").asSeconds(), moment.duration({M: 1, h: 2}).asSeconds(), "random values missing");
         test.equal(moment.duration("-P60D").asSeconds(), moment.duration({d: -60}).asSeconds(), "negative days");
+        test.equal(moment.duration("PT0.5S").asSeconds(), moment.duration({y: 1, M: 2, d: 3, h: 4, m: 5, s: 6}).asSeconds(), "fractional seconds");
         test.done();
     },
+    
+    "serialization to ISO 8601 duration strings" : function (test) {
+      test.expect(4);
+      test.equal(moment.duration({y: 1, M: 2, d: 3, h: 4, m: 5, s: 6}).toIsoString() , "P1Y2M3DT4H5M6S", "all fields");
+      test.equal(moment.duration({M: -1}).toIsoString() , "-P1M", "one month ago");
+      test.equal(moment.duration({m: -1}).toIsoString() , "-PT1M", "one minute ago");
+      test.equal(moment.duration({s: -1}).toIsoString() , "-PT0.5S", "one half second ago");
+      test.equal(moment.duration({y: -0.5, M: 1}).toIsoString() , "-P5M", "a month after half a year ago");
+      test.done();
+    },
 
     "humanize" : function (test) {
         test.expect(32);