]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Added dot net style duration parser 702/head
authorRoger Spring <roger.spring@gmail.com>
Mon, 1 Apr 2013 05:23:10 +0000 (22:23 -0700)
committerRoger Spring <roger.spring@gmail.com>
Mon, 1 Apr 2013 05:23:10 +0000 (22:23 -0700)
moment.js
test/moment/duration.js

index 4be1d2e6ed630dbd9e0ed6493a42449901aab056..aab814d67eefa36deba36f37bb89724f50e0fb53 100644 (file)
--- a/moment.js
+++ b/moment.js
@@ -21,6 +21,7 @@
 
         // ASP.NET json date format regex
         aspNetJsonRegex = /^\/?Date\((\-?\d+)/i,
+        aspNetTimeSpanJsonRegex = /(\-)?(\d*)?\.?(\d+)\:(\d+)\:(\d+)\.?(\d{3})?/,
 
         // format tokens
         formattingTokens = /(\[[^\[]*\])|(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|ddd?d?|do?|w[o|w]?|W[o|W]?|YYYYY|YYYY|YY|a|A|hh?|HH?|mm?|ss?|SS?S?|X|zz?|ZZ?|.)/g,
         var isDuration = moment.isDuration(input),
             isNumber = (typeof input === 'number'),
             duration = (isDuration ? input._data : (isNumber ? {} : input)),
+            matched = aspNetTimeSpanJsonRegex.exec(input),
+            sign,
             ret;
 
         if (isNumber) {
             } else {
                 duration.milliseconds = input;
             }
+        } else if (matched) {
+            sign = (matched[1] === "-") ? -1 : 1;
+            duration = {
+                y: 0,
+                d: ~~matched[2] * sign,
+                h: ~~matched[3] * sign,
+                m: ~~matched[4] * sign,
+                s: ~~matched[5] * sign,
+                ms: ~~matched[6] * sign
+            };
         }
 
         ret = new Duration(duration);
index 37ac4546b5f597d12ecbc794f19d85ef941a2d4d..30a852389f017efc7682ca93a8011e47c0f3bf38 100644 (file)
@@ -83,6 +83,83 @@ exports.duration = {
         test.deepEqual(moment.duration(complicated), complicated, "complicated clones are equal");
         test.done();
     },
+    
+    "instatiation from serialized C# TimeSpan zero" : function(test) {
+        test.expect(6);
+        test.equal(moment.duration("00:00:00").years(), 0, "0 years");
+        test.equal(moment.duration("00:00:00").days(), 0, "0 days");
+        test.equal(moment.duration("00:00:00").hours(), 0, "0 hours");
+        test.equal(moment.duration("00:00:00").minutes(), 0, "0 minutes");
+        test.equal(moment.duration("00:00:00").seconds(), 0, "0 seconds");
+        test.equal(moment.duration("00:00:00").milliseconds(), 0, "0 milliseconds");
+        test.done();
+    },
+    
+    "instatiation from serialized C# TimeSpan with days" : function(test) {
+        test.expect(6);
+        test.equal(moment.duration("1.02:03:04.9999999").years(), 0, "0 years");
+        test.equal(moment.duration("1.02:03:04.9999999").days(), 1, "1 day");
+        test.equal(moment.duration("1.02:03:04.9999999").hours(), 2, "2 hours");
+        test.equal(moment.duration("1.02:03:04.9999999").minutes(), 3, "3 minutes");
+        test.equal(moment.duration("1.02:03:04.9999999").seconds(), 4, "4 seconds");
+        test.equal(moment.duration("1.02:03:04.9999999").milliseconds(), 999, "999 milliseconds");
+        test.done();
+    },
+    
+    "instatiation from serialized C# TimeSpan without days" : function(test) {
+        test.expect(6);
+        test.equal(moment.duration("01:02:03.9999999").years(), 0, "0 years");
+        test.equal(moment.duration("01:02:03.9999999").days(), 0, "0 days");
+        test.equal(moment.duration("01:02:03.9999999").hours(), 1, "1 hour");
+        test.equal(moment.duration("01:02:03.9999999").minutes(), 2, "2 minutes");
+        test.equal(moment.duration("01:02:03.9999999").seconds(), 3, "3 seconds");
+        test.equal(moment.duration("01:02:03.9999999").milliseconds(), 999, "999 milliseconds");
+        test.done();
+    },
+
+    "instatiation from serialized C# TimeSpan without days or milliseconds" : function(test) {
+        test.expect(6);
+        test.equal(moment.duration("01:02:03").years(), 0, "0 years");
+        test.equal(moment.duration("01:02:03").days(), 0, "0 days");
+        test.equal(moment.duration("01:02:03").hours(), 1, "1 hour");
+        test.equal(moment.duration("01:02:03").minutes(), 2, "2 minutes");
+        test.equal(moment.duration("01:02:03").seconds(), 3, "3 seconds");
+        test.equal(moment.duration("01:02:03").milliseconds(), 0, "0 milliseconds");
+        test.done();
+    },
+
+    "instatiation from serialized C# TimeSpan without milliseconds" : function(test) {
+        test.expect(6);
+        test.equal(moment.duration("1.02:03:04").years(), 0, "0 years");
+        test.equal(moment.duration("1.02:03:04").days(), 1, "1 day");
+        test.equal(moment.duration("1.02:03:04").hours(), 2, "2 hours");
+        test.equal(moment.duration("1.02:03:04").minutes(), 3, "3 minutes");
+        test.equal(moment.duration("1.02:03:04").seconds(), 4, "4 seconds");
+        test.equal(moment.duration("1.02:03:04").milliseconds(), 0, "0 milliseconds");
+        test.done();
+    },
+
+    "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");
+        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");
+        test.done();
+    },
 
     "humanize" : function(test) {
         test.expect(32);