Skip to content

Commit 9ce9ac4

Browse files
committed
Checkpoint DST tests.
1 parent 8cad720 commit 9ce9ac4

6 files changed

Lines changed: 96 additions & 19 deletions

File tree

d3.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8223,8 +8223,13 @@ d3 = function() {
82238223
return date - d0 < d1 - date ? d0 : d1;
82248224
}
82258225
function ceil(date) {
8226-
step(date = local(new d3_time(date - 1)), 1);
8227-
return date;
8226+
date = local(new d3_time(date - 1));
8227+
var t = +date, d;
8228+
do {
8229+
step(date, 1);
8230+
d = local(date);
8231+
} while (+d === t);
8232+
return d;
82288233
}
82298234
function offset(date, k) {
82308235
step(date = new d3_time(+date), k);
@@ -8235,10 +8240,10 @@ d3 = function() {
82358240
if (dt > 1) {
82368241
while (time < t1) {
82378242
if (!(number(time) % dt)) times.push(new Date(+time));
8238-
step(time, 1);
8243+
time = ceil(new Date(+time + 1));
82398244
}
82408245
} else {
8241-
while (time < t1) times.push(new Date(+time)), step(time, 1);
8246+
while (time < t1) times.push(new Date(+time)), time = ceil(new Date(+time + 1));
82428247
}
82438248
return times;
82448249
}
@@ -8698,19 +8703,17 @@ d3 = function() {
86988703
return scale.domain(d3_scale_nice(scale.domain(), m));
86998704
};
87008705
scale.ticks = function(m, k) {
8701-
var extent = d3_scaleExtent(scale.domain()), floor;
8706+
var extent = d3_scaleExtent(scale.domain());
87028707
if (typeof m !== "function") {
87038708
var span = extent[1] - extent[0], target = span / m, i = d3.bisect(d3_time_scaleSteps, target);
87048709
if (i == d3_time_scaleSteps.length) return methods.year(extent, m);
87058710
if (!i) return linear.ticks(m).map(d3_time_scaleDate);
87068711
if (target / d3_time_scaleSteps[i - 1] < d3_time_scaleSteps[i] / target) --i;
87078712
m = methods[i];
87088713
k = m[1];
8709-
floor = m[0].floor;
87108714
m = m[0].range;
87118715
}
8712-
var ticks = m(extent[0], new Date(+extent[1] + 1), k);
8713-
return floor ? ticks.map(floor) : ticks;
8716+
return m(extent[0], new Date(+extent[1] + 1), k);
87148717
};
87158718
scale.tickFormat = function() {
87168719
return format;

d3.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/time/interval.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ function d3_time_interval(local, step, number) {
88
}
99

1010
function ceil(date) {
11-
step(date = local(new d3_time(date - 1)), 1);
12-
return date;
11+
date = local(new d3_time(date - 1));
12+
var t = +date, d;
13+
do {
14+
step(date, 1);
15+
d = local(date);
16+
} while (+d === t);
17+
return d;
1318
}
1419

1520
function offset(date, k) {
@@ -22,10 +27,10 @@ function d3_time_interval(local, step, number) {
2227
if (dt > 1) {
2328
while (time < t1) {
2429
if (!(number(time) % dt)) times.push(new Date(+time));
25-
step(time, 1);
30+
time = ceil(new Date(+time + 1));
2631
}
2732
} else {
28-
while (time < t1) times.push(new Date(+time)), step(time, 1);
33+
while (time < t1) times.push(new Date(+time)), time = ceil(new Date(+time + 1));
2934
}
3035
return times;
3136
}

src/time/scale.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ function d3_time_scale(linear, methods, format) {
3434
};
3535

3636
scale.ticks = function(m, k) {
37-
var extent = d3_scaleExtent(scale.domain()),
38-
floor;
37+
var extent = d3_scaleExtent(scale.domain());
3938
if (typeof m !== "function") {
4039
var span = extent[1] - extent[0],
4140
target = span / m,
@@ -45,11 +44,9 @@ function d3_time_scale(linear, methods, format) {
4544
if (target / d3_time_scaleSteps[i - 1] < d3_time_scaleSteps[i] / target) --i;
4645
m = methods[i];
4746
k = m[1];
48-
floor = m[0].floor;
4947
m = m[0].range;
5048
}
51-
var ticks = m(extent[0], new Date(+extent[1] + 1), k); // inclusive upper bound
52-
return floor ? ticks.map(floor) : ticks;
49+
return m(extent[0], new Date(+extent[1] + 1), k); // inclusive upper bound
5350
};
5451

5552
scale.tickFormat = function() {

test/time/day-test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ suite.addBatch({
3636
},
3737
"correctly handles years in the first century": function(floor) {
3838
assert.deepEqual(floor(local(0011, 10, 06, 07)), local(0011, 10, 06));
39+
},
40+
"DST": {
41+
"00:00:00 → 01:00:00": time.dst(local(2010, 00, 01), local(2010, 00, 01, 01), function(floor) {
42+
assert.deepEqual(floor(local(2010, 00, 01, 23)), local(2010, 00, 01, 01));
43+
}),
44+
"00:00:00 → 00:30:00": time.dst(local(2010, 00, 01), local(2010, 00, 01, 00, 30), function(floor) {
45+
assert.deepEqual(floor(local(2010, 00, 01, 23)), local(2010, 00, 01, 00, 30));
46+
})
3947
}
4048
},
4149
"ceil": {
@@ -62,6 +70,14 @@ suite.addBatch({
6270
"handles midnight for leap years": function(ceil) {
6371
assert.deepEqual(ceil(utc(2012, 02, 01, 00)), local(2012, 02, 01));
6472
assert.deepEqual(ceil(utc(2012, 02, 01, 00)), local(2012, 02, 01));
73+
},
74+
"DST": {
75+
"00:00:00 → 01:00:00": time.dst(local(2010, 00, 02), local(2010, 00, 02, 01), function(ceil) {
76+
assert.deepEqual(ceil(local(2010, 00, 01, 23)), local(2010, 00, 02, 01));
77+
}),
78+
"00:00:00 → 00:30:00": time.dst(local(2010, 00, 02), local(2010, 00, 02, 00, 30), function(ceil) {
79+
assert.deepEqual(ceil(local(2010, 00, 01, 01)), local(2010, 00, 02, 00, 30));
80+
})
6581
}
6682
},
6783
"offset": {

test/time/time.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,59 @@ exports.zone = function(tzOffset, scope) {
2828
}
2929
};
3030
};
31+
32+
exports.dst = function(date0, date1, scope) {
33+
var t0 = +date0,
34+
t1 = +date1;
35+
return function() {
36+
var getHours = Date.prototype.getHours,
37+
setHours = Date.prototype.setHours,
38+
getMinutes = Date.prototype.getMinutes,
39+
getDate = Date.prototype.getDate,
40+
setDate = Date.prototype.setDate;
41+
try {
42+
Date.prototype.getDate = function() {
43+
var t = this.getTime();
44+
try {
45+
if (t0 <= t) this.setTime(t + (t1 - t0));
46+
return getDate.call(this);
47+
} finally {
48+
this.setTime(t);
49+
}
50+
};
51+
Date.prototype.getHours = function() {
52+
var t = this.getTime();
53+
try {
54+
if (t0 <= t) this.setTime(t + (t1 - t0));
55+
return getHours.call(this);
56+
} finally {
57+
this.setTime(t);
58+
}
59+
};
60+
Date.prototype.getMinutes = function() {
61+
var t = this.getTime();
62+
try {
63+
if (t0 <= t) this.setTime(t + (t1 - t0));
64+
return getMinutes.call(this);
65+
} finally {
66+
this.setTime(t);
67+
}
68+
};
69+
Date.prototype.setHours = function() {
70+
var t = setHours.apply(this, arguments);
71+
return t0 <= t ? this.setTime(t0 + (t - t1)) : t;
72+
};
73+
Date.prototype.setDate = function() {
74+
var t = setDate.apply(this, arguments);
75+
return t0 <= t ? this.setTime(t0 + (t - t1)) : t;
76+
};
77+
scope.apply(this, arguments);
78+
} finally {
79+
Date.prototype.getDate = getDate;
80+
Date.prototype.setDate = setDate;
81+
Date.prototype.getHours = getHours;
82+
Date.prototype.setHours = setHours;
83+
Date.prototype.getMinutes = getMinutes;
84+
}
85+
};
86+
};

0 commit comments

Comments
 (0)