Skip to content

Commit 83b5f45

Browse files
committed
Fix d3#711 for years in the first century.
Alas, the JavaScript Date constructor is Y2K-centric.
1 parent 5c1cd6b commit 83b5f45

9 files changed

Lines changed: 31 additions & 7 deletions

File tree

d3.v2.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9162,7 +9162,9 @@ d3.time.hour = d3_time_interval(function(date) {
91629162
d3.time.hours = d3.time.hour.range;
91639163
d3.time.hours.utc = d3.time.hour.utc.range;
91649164
d3.time.day = d3_time_interval(function(date) {
9165-
return new d3_time(date.getFullYear(), date.getMonth(), date.getDate());
9165+
var day = new d3_time(0, date.getMonth(), date.getDate());
9166+
day.setFullYear(date.getFullYear());
9167+
return day;
91669168
}, function(date, offset) {
91679169
date.setDate(date.getDate() + offset);
91689170
}, function(date) {
@@ -9204,7 +9206,9 @@ d3.time.weeks = d3.time.sunday.range;
92049206
d3.time.weeks.utc = d3.time.sunday.utc.range;
92059207
d3.time.weekOfYear = d3.time.sundayOfYear;
92069208
d3.time.month = d3_time_interval(function(date) {
9207-
return new d3_time(date.getFullYear(), date.getMonth(), 1);
9209+
date = d3.time.day(date);
9210+
date.setDate(1);
9211+
return date;
92089212
}, function(date, offset) {
92099213
date.setMonth(date.getMonth() + offset);
92109214
}, function(date) {
@@ -9214,7 +9218,9 @@ d3.time.month = d3_time_interval(function(date) {
92149218
d3.time.months = d3.time.month.range;
92159219
d3.time.months.utc = d3.time.month.utc.range;
92169220
d3.time.year = d3_time_interval(function(date) {
9217-
return new d3_time(date.getFullYear(), 0, 1);
9221+
date = d3.time.day(date);
9222+
date.setMonth(0, 1);
9223+
return date;
92189224
}, function(date, offset) {
92199225
date.setFullYear(date.getFullYear() + offset);
92209226
}, function(date) {

d3.v2.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/time/day.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
d3.time.day = d3_time_interval(function(date) {
2-
return new d3_time(date.getFullYear(), date.getMonth(), date.getDate());
2+
var day = new d3_time(0, date.getMonth(), date.getDate());
3+
day.setFullYear(date.getFullYear());
4+
return day;
35
}, function(date, offset) {
46
date.setDate(date.getDate() + offset);
57
}, function(date) {

src/time/month.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
d3.time.month = d3_time_interval(function(date) {
2-
return new d3_time(date.getFullYear(), date.getMonth(), 1);
2+
date = d3.time.day(date);
3+
date.setDate(1);
4+
return date;
35
}, function(date, offset) {
46
date.setMonth(date.getMonth() + offset);
57
}, function(date) {

src/time/year.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
d3.time.year = d3_time_interval(function(date) {
2-
return new d3_time(date.getFullYear(), 0, 1);
2+
date = d3.time.day(date);
3+
date.setMonth(0, 1);
4+
return date;
35
}, function(date, offset) {
46
date.setFullYear(date.getFullYear() + offset);
57
}, function(date) {

test/time/day-test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ suite.addBatch({
3636
assert.deepEqual(floor(utc(2011, 10, 06, 08)), local(2011, 10, 06));
3737
assert.deepEqual(floor(utc(2011, 10, 06, 09)), local(2011, 10, 06));
3838
assert.deepEqual(floor(utc(2011, 10, 06, 10)), local(2011, 10, 06));
39+
},
40+
"correctly handles years in the first century": function(floor) {
41+
assert.deepEqual(floor(local(0011, 10, 06, 07)), local(0011, 10, 06));
3942
}
4043
},
4144
"ceil": {

test/time/month-test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ suite.addBatch({
3030
},
3131
"observes the end of the daylight savings time": function(floor) {
3232
assert.deepEqual(floor(local(2011, 10, 06, 01)), local(2011, 10, 01));
33+
},
34+
"correctly handles years in the first century": function(floor) {
35+
assert.deepEqual(floor(local(0011, 10, 06, 07)), local(0011, 10, 01));
3336
}
3437
},
3538
"ceil": {

test/time/week-test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ suite.addBatch({
3333
},
3434
"observes the end of the daylight savings time": function(floor) {
3535
assert.deepEqual(floor(local(2011, 10, 06, 01)), local(2011, 10, 06));
36+
},
37+
"correctly handles years in the first century": function(floor) {
38+
assert.deepEqual(floor(local(0011, 10, 06, 07)), local(0011, 10, 01));
3639
}
3740
},
3841
"ceil": {

test/time/year-test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ suite.addBatch({
2121
assert.deepEqual(floor(local(2010, 11, 31, 23, 59, 59)), local(2010, 00, 01));
2222
assert.deepEqual(floor(local(2011, 00, 01, 00, 00, 00)), local(2011, 00, 01));
2323
assert.deepEqual(floor(local(2011, 00, 01, 00, 00, 01)), local(2011, 00, 01));
24+
},
25+
"correctly handles years in the first century": function(floor) {
26+
assert.deepEqual(floor(local(0011, 10, 06, 07)), local(0011, 00, 01));
2427
}
2528
},
2629
"ceil": {

0 commit comments

Comments
 (0)