From: not-an-aardvark Date: Mon, 4 Jul 2016 04:01:32 +0000 (-0400) Subject: Ensure that millisecond durations are parsed from .NET timestamps X-Git-Tag: 2.15.0~26^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0d9b7af6c2b8ac0e9875f513fdbb22f458b887a4;p=thirdparty%2Fmoment.git Ensure that millisecond durations are parsed from .NET timestamps (fixes #3266) --- diff --git a/src/lib/duration/create.js b/src/lib/duration/create.js index 24275d688..1324d4518 100644 --- a/src/lib/duration/create.js +++ b/src/lib/duration/create.js @@ -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; diff --git a/src/test/moment/duration.js b/src/test/moment/duration.js index e855f21cf..f1434a3ee 100644 --- a/src/test/moment/duration.js +++ b/src/test/moment/duration.js @@ -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');