Skip to content

Commit

Permalink
Merge pull request #2995 from akura-co:develop
Browse files Browse the repository at this point in the history
Using 1000 * 60 * 60 instead of 36e5 to avoid floating point rounding errors
  • Loading branch information
ichernev committed Apr 16, 2016
2 parents 6592ad9 + 968c44a commit 544a059
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/lib/duration/constructor.js
Expand Up @@ -17,7 +17,7 @@ export function Duration (duration) {
this._milliseconds = +milliseconds +
seconds * 1e3 + // 1000
minutes * 6e4 + // 1000 * 60
hours * 36e5; // 1000 * 60 * 60
hours * 1000 * 60 * 60; //using 1000 * 60 * 60 instead of 36e5 to avoid floating point rounding errors https://github.com/moment/moment/issues/2978
// Because of dateAddRemove treats 24 hours as different from a
// day when working around DST, we need to store them separately
this._days = +days +
Expand Down
8 changes: 7 additions & 1 deletion src/test/moment/duration.js
Expand Up @@ -548,6 +548,13 @@ test('as getters for small units', function (assert) {
assert.equal(dm.asMinutes(), 13, 'asMinutes()');
});

test('minutes getter for floating point hours', function (assert) {
// Tests for issue #2978.
// For certain floating point hours, .minutes() getter produced incorrect values due to the rounding errors
assert.equal(moment.duration(2.3, 'h').minutes(), 18, 'minutes()');
assert.equal(moment.duration(4.1, 'h').minutes(), 6, 'minutes()');
});

test('isDuration', function (assert) {
assert.ok(moment.isDuration(moment.duration(12345678)), 'correctly says true');
assert.ok(!moment.isDuration(moment()), 'moment object is not a duration');
Expand Down Expand Up @@ -636,4 +643,3 @@ test('duration plugins', function (assert) {
};
durationObject.foo(5);
});

0 comments on commit 544a059

Please sign in to comment.