Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Date construction for years 0 <= y < 100 #11

Merged
merged 1 commit into from
Jun 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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