Skip to content

Commit

Permalink
fix(extensions/date): interpret low javascript dates correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce committed Sep 5, 2016
1 parent 3803dcb commit cf5e716
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/extensions/date.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var _ = require('lodash');
var assert = require('../assert');

var twoDaysInMiliseconds = 1000 * 60 * 60 * 48;
module.exports = function (construct) {
return {
types: ['date'],
Expand Down Expand Up @@ -34,7 +35,8 @@ module.exports = function (construct) {

if (_.isNaN(date.getTime())) {
return value;
} else if (_.isNaN(dateWithTz.getTime())) {
} else if (_.isNaN(dateWithTz.getTime()) ||
Math.abs(dateWithTz.getTime() - date.getTime()) > twoDaysInMiliseconds) {
return date;
} else {
return dateWithTz;
Expand Down
5 changes: 5 additions & 0 deletions test/extensions/date.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ defineTest('extensions/date.js', function (dateFactory) {
transform(86400 * 366 * 1000).should.eql(new Date('1971-01-02T00:00:00.000Z'));
});

it('should interpret ambiguous number values as javascript timestamps', function () {
transform(0).should.eql(new Date('1970-01-01T00:00:00.000Z'));
transform(2).should.eql(new Date('1970-01-01T00:00:00.002Z'));
});

it('should directly return a date value', function () {
var d1 = new Date(2016, 04, 03);
var d2 = new Date(1985, 02, 01);
Expand Down

0 comments on commit cf5e716

Please sign in to comment.