Permalink
Browse files

Fix a couple bugs in d3.time.format.

First, we were using the wrong space-filling format for %d and equivalent.
Second, we weren't parsing numbers with optional leading spaces correctly.
Yay tests!
  • Loading branch information...
1 parent aad495a commit ff9da42269ce9ae5dadd75e1c5a373b471841514 @mbostock mbostock committed Mar 13, 2011
View
@@ -140,6 +140,8 @@ tests: \
tests/test-attr.test \
tests/test-call.test \
tests/test-format.test \
+ tests/test-time-format.test \
+ tests/test-time-parse.test \
tests/test-transition.test \
tests/test-scale-linear.test \
tests/test-scale-sqrt.test \
@@ -156,7 +158,7 @@ d3.js d3%.js: Makefile
cat $(filter %.js,$^) > $@
@chmod a-w $@
-%.test: %.js %.out d3.js
+%.test: %.js %.out all
@/bin/echo -n "test: $* "
@node $< > $*.actual
@diff -U 3 $*.out $*.actual && rm -f $*.actual \
View
@@ -63,7 +63,7 @@ function d3_time_parse(date, template, string, j) {
var d3_time_zfill2 = d3.format("02d"),
d3_time_zfill3 = d3.format("03d"),
d3_time_zfill4 = d3.format("04d"),
- d3_time_sfill2 = d3.format(" 2d");
+ d3_time_sfill2 = d3.format("2d");
var d3_time_formats = {
a: function(d) { return d3_time_weekdays[d.getDay()].substring(0, 3); },
@@ -223,14 +223,14 @@ function d3_time_parseLocaleTime(date, string, i) {
}
function d3_time_parseFullYear(date, string, i) {
- d3_time_number4Re.lastIndex = 0;
- var n = d3_time_number4Re.exec(string.substring(i, i + 4));
+ d3_time_numberRe.lastIndex = 0;
+ var n = d3_time_numberRe.exec(string.substring(i, i + 4));
return n ? (date.setFullYear(n[0]), i += n[0].length) : -1;
}
function d3_time_parseYear(date, string, i) {
- d3_time_number2Re.lastIndex = 0;
- var n = d3_time_number2Re.exec(string.substring(i, i + 2));
+ d3_time_numberRe.lastIndex = 0;
+ var n = d3_time_numberRe.exec(string.substring(i, i + 2));
return n ? (date.setFullYear(d3_time_century() + +n[0]), i += n[0].length) : -1;
}
@@ -239,21 +239,21 @@ function d3_time_century() {
}
function d3_time_parseMonthNumber(date, string, i) {
- d3_time_number2Re.lastIndex = 0;
- var n = d3_time_number2Re.exec(string.substring(i, i + 2));
+ d3_time_numberRe.lastIndex = 0;
+ var n = d3_time_numberRe.exec(string.substring(i, i + 2));
return n ? (date.setMonth(n[0] - 1), i += n[0].length) : -1;
}
function d3_time_parseDay(date, string, i) {
- d3_time_number2Re.lastIndex = 0;
- var n = d3_time_number2Re.exec(string.substring(i, i + 2));
+ d3_time_numberRe.lastIndex = 0;
+ var n = d3_time_numberRe.exec(string.substring(i, i + 2));
return n ? (date.setDate(+n[0]), i += n[0].length) : -1;
}
// Note: we don't validate that the hour is in the range [0,23].
function d3_time_parseHour24(date, string, i) {
- d3_time_number2Re.lastIndex = 0;
- var n = d3_time_number2Re.exec(string.substring(i, i + 2));
+ d3_time_numberRe.lastIndex = 0;
+ var n = d3_time_numberRe.exec(string.substring(i, i + 2));
return n ? (date.setHours(+n[0]), i += n[0].length) : -1;
}
@@ -264,20 +264,19 @@ function d3_time_parseHour12(date, string, i) {
}
function d3_time_parseMinutes(date, string, i) {
- d3_time_number2Re.lastIndex = 0;
- var n = d3_time_number2Re.exec(string.substring(i, i + 2));
+ d3_time_numberRe.lastIndex = 0;
+ var n = d3_time_numberRe.exec(string.substring(i, i + 2));
return n ? (date.setMinutes(+n[0]), i += n[0].length) : -1;
}
function d3_time_parseSeconds(date, string, i) {
- d3_time_number2Re.lastIndex = 0;
- var n = d3_time_number2Re.exec(string.substring(i, i + 2));
+ d3_time_numberRe.lastIndex = 0;
+ var n = d3_time_numberRe.exec(string.substring(i, i + 2));
return n ? (date.setSeconds(+n[0]), i += n[0].length) : -1;
}
// Note: we don't look at the next directive.
-var d3_time_number2Re = /[\s\d]{1,2}/,
- d3_time_number4Re = /[\s\d]{1,4}/;
+var d3_time_numberRe = /\s*\d+/;
function d3_time_parseAmPm(date, string, i) {
var n = d3_time_amPmLookup[string.substring(i, i += 2).toLowerCase()];
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -62,7 +62,7 @@ function d3_time_parse(date, template, string, j) {
var d3_time_zfill2 = d3.format("02d"),
d3_time_zfill3 = d3.format("03d"),
d3_time_zfill4 = d3.format("04d"),
- d3_time_sfill2 = d3.format(" 2d");
+ d3_time_sfill2 = d3.format("2d");
var d3_time_formats = {
a: function(d) { return d3_time_weekdays[d.getDay()].substring(0, 3); },
@@ -222,14 +222,14 @@ function d3_time_parseLocaleTime(date, string, i) {
}
function d3_time_parseFullYear(date, string, i) {
- d3_time_number4Re.lastIndex = 0;
- var n = d3_time_number4Re.exec(string.substring(i, i + 4));
+ d3_time_numberRe.lastIndex = 0;
+ var n = d3_time_numberRe.exec(string.substring(i, i + 4));
return n ? (date.setFullYear(n[0]), i += n[0].length) : -1;
}
function d3_time_parseYear(date, string, i) {
- d3_time_number2Re.lastIndex = 0;
- var n = d3_time_number2Re.exec(string.substring(i, i + 2));
+ d3_time_numberRe.lastIndex = 0;
+ var n = d3_time_numberRe.exec(string.substring(i, i + 2));
return n ? (date.setFullYear(d3_time_century() + +n[0]), i += n[0].length) : -1;
}
@@ -238,21 +238,21 @@ function d3_time_century() {
}
function d3_time_parseMonthNumber(date, string, i) {
- d3_time_number2Re.lastIndex = 0;
- var n = d3_time_number2Re.exec(string.substring(i, i + 2));
+ d3_time_numberRe.lastIndex = 0;
+ var n = d3_time_numberRe.exec(string.substring(i, i + 2));
return n ? (date.setMonth(n[0] - 1), i += n[0].length) : -1;
}
function d3_time_parseDay(date, string, i) {
- d3_time_number2Re.lastIndex = 0;
- var n = d3_time_number2Re.exec(string.substring(i, i + 2));
+ d3_time_numberRe.lastIndex = 0;
+ var n = d3_time_numberRe.exec(string.substring(i, i + 2));
return n ? (date.setDate(+n[0]), i += n[0].length) : -1;
}
// Note: we don't validate that the hour is in the range [0,23].
function d3_time_parseHour24(date, string, i) {
- d3_time_number2Re.lastIndex = 0;
- var n = d3_time_number2Re.exec(string.substring(i, i + 2));
+ d3_time_numberRe.lastIndex = 0;
+ var n = d3_time_numberRe.exec(string.substring(i, i + 2));
return n ? (date.setHours(+n[0]), i += n[0].length) : -1;
}
@@ -263,20 +263,19 @@ function d3_time_parseHour12(date, string, i) {
}
function d3_time_parseMinutes(date, string, i) {
- d3_time_number2Re.lastIndex = 0;
- var n = d3_time_number2Re.exec(string.substring(i, i + 2));
+ d3_time_numberRe.lastIndex = 0;
+ var n = d3_time_numberRe.exec(string.substring(i, i + 2));
return n ? (date.setMinutes(+n[0]), i += n[0].length) : -1;
}
function d3_time_parseSeconds(date, string, i) {
- d3_time_number2Re.lastIndex = 0;
- var n = d3_time_number2Re.exec(string.substring(i, i + 2));
+ d3_time_numberRe.lastIndex = 0;
+ var n = d3_time_numberRe.exec(string.substring(i, i + 2));
return n ? (date.setSeconds(+n[0]), i += n[0].length) : -1;
}
// Note: we don't look at the next directive.
-var d3_time_number2Re = /[\s\d]{1,2}/,
- d3_time_number4Re = /[\s\d]{1,4}/;
+var d3_time_numberRe = /\s*\d+/;
function d3_time_parseAmPm(date, string, i) {
var n = d3_time_amPmLookup[string.substring(i, i += 2).toLowerCase()];
@@ -1,7 +1,6 @@
require("./../lib/env-js/envjs/node");
require("./../lib/sizzle/sizzle");
require("./../d3");
-require("./../d3.time");
var f = d3.format(" .3f");
View
@@ -1,7 +1,6 @@
require("./../lib/env-js/envjs/node");
require("./../lib/sizzle/sizzle");
require("./../d3");
-require("./../d3.time");
var f = d3.format(" .3f");
View
@@ -1,7 +1,6 @@
require("./../lib/env-js/envjs/node");
require("./../lib/sizzle/sizzle");
require("./../d3");
-require("./../d3.time");
var f = d3.format(" .3f");
View
@@ -0,0 +1,22 @@
+require("./../lib/env-js/envjs/node");
+require("./../d3");
+require("./../d3.time");
+
+var formats = [
+ "a", "A", "b", "B", "c", "d", "e", "H", "I", "j", "m", "M",
+ "p", "S", "U", "w", "W", "x", "X", "y", "Y", "Z", "%"
+];
+
+var now = new Date(1990, 0, 1);
+console.log("format 01/01/1990 00:00:00:");
+formats.forEach(function(f) {
+ console.log(" " + f + ":", d3.time.format("%" + f)(now));
+});
+console.log("");
+
+var now = new Date(2011, 11, 31, 23, 59, 59);
+console.log("format 12/31/2011 23:59:59:");
+formats.forEach(function(f) {
+ console.log(" " + f + ":", d3.time.format("%" + f)(now));
+});
+console.log("");
View
@@ -0,0 +1,50 @@
+format 01/01/1990 00:00:00:
+ a: Mon
+ A: Monday
+ b: Jan
+ B: January
+ c: Mon Jan 1 00:00:00 1990
+ d: 01
+ e: 1
+ H: 00
+ I: 12
+ j: 001
+ m: 01
+ M: 00
+ p: AM
+ S: 00
+ U: 00
+ w: 1
+ W: 00
+ x: 01/01/90
+ X: 00:00:00
+ y: 90
+ Y: 1990
+ Z: -0800
+ %: %
+
+format 12/31/2011 23:59:59:
+ a: Sat
+ A: Saturday
+ b: Dec
+ B: December
+ c: Sat Dec 31 23:59:59 2011
+ d: 31
+ e: 31
+ H: 23
+ I: 11
+ j: 365
+ m: 12
+ M: 59
+ p: PM
+ S: 59
+ U: 52
+ w: 6
+ W: 52
+ x: 12/31/11
+ X: 23:59:59
+ y: 11
+ Y: 2011
+ Z: -0800
+ %: %
+
Oops, something went wrong.

0 comments on commit ff9da42

Please sign in to comment.