]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Ensure that millisecond durations are parsed from .NET timestamps
authornot-an-aardvark <not-an-aardvark@users.noreply.github.com>
Mon, 4 Jul 2016 04:01:32 +0000 (00:01 -0400)
committerIskren Chernev <iskren.chernev@gmail.com>
Thu, 1 Sep 2016 09:58:28 +0000 (02:58 -0700)
(fixes #3266)

src/lib/duration/create.js
src/test/moment/duration.js

index 24275d68896d4c08f89906d516416f9072294ecd..1324d45188f485f144ddce691835d99c3d47df42 100644 (file)
@@ -6,7 +6,7 @@ import { cloneWithOffset } from '../units/offset';
 import { createLocal } from '../create/local';
 
 // ASP.NET json date format regex
-var aspNetRegex = /^(\-)?(?:(\d*)[. ])?(\d+)\:(\d+)(?:\:(\d+)\.?(\d{3})?\d*)?$/;
+var aspNetRegex = /^(\-)?(?:(\d*)[. ])?(\d+)\:(\d+)(?:\:(\d+)(\.\d*)?)?$/;
 
 // from http://docs.closure-library.googlecode.com/git/closure_goog_date_date.js.source.html
 // somewhat more in line with 4.4.3.2 2004 spec, but allows decimal anywhere
@@ -38,11 +38,11 @@ export function createDuration (input, key) {
         sign = (match[1] === '-') ? -1 : 1;
         duration = {
             y  : 0,
-            d  : toInt(match[DATE])        * sign,
-            h  : toInt(match[HOUR])        * sign,
-            m  : toInt(match[MINUTE])      * sign,
-            s  : toInt(match[SECOND])      * sign,
-            ms : toInt(match[MILLISECOND]) * sign
+            d  : toInt(match[DATE])               * sign,
+            h  : toInt(match[HOUR])               * sign,
+            m  : toInt(match[MINUTE])             * sign,
+            s  : toInt(match[SECOND])             * sign,
+            ms : toInt(match[MILLISECOND] * 1000) * sign // the millisecond decimal point is included in the match
         };
     } else if (!!(match = isoRegex.exec(input))) {
         sign = (match[1] === '-') ? -1 : 1;
index e855f21cf6c0e1098d0c092ef7983a728f68ae8a..f1434a3eecdfe372b4d68a42b892b554b6a17699 100644 (file)
@@ -222,6 +222,19 @@ test('instatiation from serialized C# TimeSpan without milliseconds', function (
     assert.equal(moment.duration('1.02:03:04').milliseconds(), 0, '0 milliseconds');
 });
 
+test('instantiation from serialized C# TimeSpan with low millisecond precision', function (assert) {
+    assert.equal(moment.duration('00:00:15.72').years(), 0, '0 years');
+    assert.equal(moment.duration('00:00:15.72').days(), 0, '0 days');
+    assert.equal(moment.duration('00:00:15.72').hours(), 0, '0 hours');
+    assert.equal(moment.duration('00:00:15.72').minutes(), 0, '0 minutes');
+    assert.equal(moment.duration('00:00:15.72').seconds(), 15, '15 seconds');
+    assert.equal(moment.duration('00:00:15.72').milliseconds(), 720, '720 milliseconds');
+
+    assert.equal(moment.duration('00:00:15.7').milliseconds(), 700, '700 milliseconds');
+
+    assert.equal(moment.duration('00:00:15.').milliseconds(), 0, '0 milliseconds');
+});
+
 test('instatiation from serialized C# TimeSpan maxValue', function (assert) {
     var d = moment.duration('10675199.02:48:05.4775807');