From 8db3b32ca64b0f8fb431b3e52a20243f54875264 Mon Sep 17 00:00:00 2001 From: Roger Spring Date: Sun, 31 Mar 2013 22:23:10 -0700 Subject: [PATCH] Added dot net style duration parser --- moment.js | 13 +++++++ test/moment/duration.js | 77 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/moment.js b/moment.js index 4be1d2e6e..aab814d67 100644 --- 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, @@ -991,6 +992,8 @@ var isDuration = moment.isDuration(input), isNumber = (typeof input === 'number'), duration = (isDuration ? input._data : (isNumber ? {} : input)), + matched = aspNetTimeSpanJsonRegex.exec(input), + sign, ret; if (isNumber) { @@ -999,6 +1002,16 @@ } 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); diff --git a/test/moment/duration.js b/test/moment/duration.js index 37ac4546b..30a852389 100644 --- a/test/moment/duration.js +++ b/test/moment/duration.js @@ -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); -- 2.47.2