Skip to content

fix Date/Instant construction for years 0 <= y < 100 #53

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

Merged
merged 1 commit into from
Apr 4, 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
16 changes: 12 additions & 4 deletions src/Data/Date.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
"use strict";

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

exports.canonicalDateImpl = function (ctor, y, m, d) {
var date = new Date(Date.UTC(y, m - 1, d));
var date = createDate(y, m - 1, d);
return ctor(date.getUTCFullYear())(date.getUTCMonth() + 1)(date.getUTCDate());
};

exports.calcWeekday = function (y, m, d) {
return new Date(Date.UTC(y, m - 1, d)).getUTCDay();
return createDate(y, m - 1, d).getUTCDay();
};

exports.calcDiff = function (y1, m1, d1, y2, m2, d2) {
var dt1 = new Date(Date.UTC(y1, m1 - 1, d1));
var dt2 = new Date(Date.UTC(y2, m2 - 1, d2));
var dt1 = createDate(y1, m1 - 1, d1);
var dt2 = createDate(y2, m2 - 1, d2);
return dt1.getTime() - dt2.getTime();
};
10 changes: 9 additions & 1 deletion src/Data/DateTime/Instant.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
"use strict";

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

exports.fromDateTimeImpl = function (y, mo, d, h, mi, s, ms) {
return new Date(Date.UTC(y, mo - 1, d, h, mi, s, ms)).getTime();
return createDateTime(y, mo - 1, d, h, mi, s, ms).getTime();
};

exports.toDateTimeImpl = function (ctor) {
Expand Down
14 changes: 14 additions & 0 deletions test/Test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ type Tests = Eff (console :: CONSOLE, assert :: ASSERT) Unit
main :: Tests
main = do

let epochDate = unsafePartial fromJust $ Date.canonicalDate
<$> toEnum 1
<*> pure bottom
<*> pure bottom
let epochDateTime = DateTime.DateTime epochDate bottom
let epochMillis = -62135596800000.0
-- time --------------------------------------------------------------------

log "Check that Hour is a good BoundedEnum"
Expand Down Expand Up @@ -105,6 +111,11 @@ main = do
assert $ not $ Date.isLeapYear (unsafeYear 2017)
assert $ Date.isLeapYear (unsafeYear 2016)

log "Check that epoch is correctly constructed"
assert $ Just (Date.year epochDate) == toEnum 1
assert $ Date.month epochDate == bottom
assert $ Date.day epochDate == bottom

-- datetime ----------------------------------------------------------------

let dt1 = DateTime.DateTime d1 t1
Expand Down Expand Up @@ -134,6 +145,9 @@ main = do
let topInstant = Instant.fromDateTime top
assert $ Just topInstant == Instant.instant (Instant.unInstant topInstant)

log "Check that an Instant can be constructed from epoch"
assert $ (Instant.unInstant $ Instant.fromDateTime epochDateTime) == Duration.Milliseconds epochMillis

log "Check that instant/datetime conversion is bijective"
assert $ Instant.toDateTime (Instant.fromDateTime bottom) == bottom
assert $ Instant.toDateTime (Instant.fromDateTime top) == top
Expand Down