Skip to content

Commit 1d3bf80

Browse files
committed
fix date/instant construction for years 0 <= y < 100
1 parent 2ff3808 commit 1d3bf80

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

src/Data/Date.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
"use strict";
22

3+
var createDate = function (y, m, d) {
4+
var date = new Date(Date.UTC(y, m, d));
5+
if (y >= 0 && y < 100) {
6+
date.setUTCFullYear(y);
7+
}
8+
return date;
9+
}
10+
311
exports.canonicalDateImpl = function (ctor, y, m, d) {
4-
var date = new Date(Date.UTC(y, m - 1, d));
12+
var date = createDate(y, m - 1, d);
513
return ctor(date.getUTCFullYear())(date.getUTCMonth() + 1)(date.getUTCDate());
614
};
715

816
exports.calcWeekday = function (y, m, d) {
9-
return new Date(Date.UTC(y, m - 1, d)).getUTCDay();
17+
return createDate(y, m - 1, d).getUTCDay();
1018
};
1119

1220
exports.calcDiff = function (y1, m1, d1, y2, m2, d2) {
13-
var dt1 = new Date(Date.UTC(y1, m1 - 1, d1));
14-
var dt2 = new Date(Date.UTC(y2, m2 - 1, d2));
21+
var dt1 = createDate(y1, m1 - 1, d1);
22+
var dt2 = createDate(y2, m2 - 1, d2);
1523
return dt1.getTime() - dt2.getTime();
1624
};

src/Data/DateTime/Instant.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
"use strict";
22

3+
var createDateTime = function (y, m, d, h, mi, s, ms) {
4+
var dateTime = new Date(Date.UTC(y, m, d, h, mi, s, ms));
5+
if (y >= 0 && y < 100) {
6+
dateTime.setUTCFullYear(y);
7+
}
8+
return dateTime;
9+
}
10+
311
exports.fromDateTimeImpl = function (y, mo, d, h, mi, s, ms) {
4-
return new Date(Date.UTC(y, mo - 1, d, h, mi, s, ms)).getTime();
12+
return createDateTime(y, mo - 1, d, h, mi, s, ms).getTime();
513
};
614

715
exports.toDateTimeImpl = function (ctor) {

test/Test/Main.purs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ type Tests = Eff (console :: CONSOLE, assert :: ASSERT) Unit
2525
main :: Tests
2626
main = do
2727

28+
let epochDate = unsafePartial fromJust $ Date.canonicalDate
29+
<$> toEnum 1
30+
<*> pure bottom
31+
<*> pure bottom
32+
let epochDateTime = DateTime.DateTime epochDate bottom
33+
let epochMillis = -62135596800000.0
2834
-- time --------------------------------------------------------------------
2935

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

114+
log "Check that epoch is correctly constructed"
115+
assert $ Just (Date.year epochDate) == toEnum 1
116+
assert $ Date.month epochDate == bottom
117+
assert $ Date.day epochDate == bottom
118+
108119
-- datetime ----------------------------------------------------------------
109120

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

148+
log "Check that an Instant can be constructed from epoch"
149+
assert $ (Instant.unInstant $ Instant.fromDateTime epochDateTime) == Duration.Milliseconds epochMillis
150+
137151
log "Check that instant/datetime conversion is bijective"
138152
assert $ Instant.toDateTime (Instant.fromDateTime bottom) == bottom
139153
assert $ Instant.toDateTime (Instant.fromDateTime top) == top

0 commit comments

Comments
 (0)