Skip to content

Commit

Permalink
Merge pull request #11 from berdario/dates-pre-100
Browse files Browse the repository at this point in the history
fix Date construction for years 0 <= y < 100
  • Loading branch information
garyb authored Jun 23, 2017
2 parents 54f4b20 + 73c04f3 commit f882b5f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/Data/JSDate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
/* global exports */
"use strict";

var createDate = function (y, m, d, h, mi, s, ms) {
var date = new Date(Date.UTC(y, m, d, h, mi, s, ms));
if (y >= 0 && y < 100) {
date.setUTCFullYear(y);
}
return date;
};

var createLocalDate = function (y, m, d, h, mi, s, ms) {
var date = new Date(y, m, d, h, mi, s, ms);
if (y >= 0 && y < 100) {
date.setFullYear(y);
}
return date;
};

exports.isValid = function (date) {
return !isNaN(date.getTime());
};
Expand All @@ -15,13 +31,12 @@ exports.toInstantImpl = function (just) {
};

exports.jsdate = function (parts) {
var t = Date.UTC(parts.year, parts.month, parts.day, parts.hour, parts.minute, parts.second, parts.millisecond);
return new Date(t);
return createDate(parts.year, parts.month, parts.day, parts.hour, parts.minute, parts.second, parts.millisecond);
};

exports.jsdateLocal = function (parts) {
return function () {
return new Date(parts.year, parts.month, parts.day, parts.hour, parts.minute, parts.second, parts.millisecond);
return createLocalDate(parts.year, parts.month, parts.day, parts.hour, parts.minute, parts.second, parts.millisecond);
};
};

Expand Down
6 changes: 6 additions & 0 deletions test/Test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ main = do

log "Check that a roundtrip conversion of a dates results in the input"
assert $ JSD.toDateTime (JSD.fromDateTime dateTime) == Just dateTime
assert $ JSD.toDateTime (JSD.fromDateTime ancientDateTime) == Just ancientDateTime
assert $ JSD.toDateTime (JSD.fromDateTime bottom) == Just bottom
assert $ JSD.toDateTime (JSD.fromDateTime top) == Just top

Expand All @@ -80,3 +81,8 @@ main = do
DT.Time <$> toEnum 2 <*> toEnum 21 <*> toEnum 43 <*> toEnum 678

dateTime = DT.DateTime date time

ancientDate = unsafePartial $ fromJust $
DT.canonicalDate <$> toEnum 1 <*> pure DT.January <*> toEnum 1

ancientDateTime = DT.DateTime ancientDate time

0 comments on commit f882b5f

Please sign in to comment.