Skip to content

Commit

Permalink
Fix #24 - %f for microseconds.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Oct 9, 2017
1 parent c2eccc6 commit 66ee751
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ Returns a new formatter for the given string *specifier*. The specifier string m
* `%c` - the locale’s date and time, such as `%x, %X`.*
* `%d` - zero-padded day of the month as a decimal number [01,31].
* `%e` - space-padded day of the month as a decimal number [ 1,31]; equivalent to `%_d`.
* `%f` - microseconds as a decimal number [000000, 999999].
* `%H` - hour (24-hour clock) as a decimal number [00,23].
* `%I` - hour (12-hour clock) as a decimal number [01,12].
* `%j` - day of the year as a decimal number [001,366].
Expand Down
16 changes: 16 additions & 0 deletions src/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export default function formatLocale(locale) {
"c": null,
"d": formatDayOfMonth,
"e": formatDayOfMonth,
"f": formatMicroseconds,
"H": formatHour24,
"I": formatHour12,
"j": formatDayOfYear,
Expand Down Expand Up @@ -91,6 +92,7 @@ export default function formatLocale(locale) {
"c": null,
"d": formatUTCDayOfMonth,
"e": formatUTCDayOfMonth,
"f": formatUTCMicroseconds,
"H": formatUTCHour24,
"I": formatUTCHour12,
"j": formatUTCDayOfYear,
Expand Down Expand Up @@ -120,6 +122,7 @@ export default function formatLocale(locale) {
"c": parseLocaleDateTime,
"d": parseDayOfMonth,
"e": parseDayOfMonth,
"f": parseMicroseconds,
"H": parseHour24,
"I": parseHour24,
"j": parseDayOfYear,
Expand Down Expand Up @@ -450,6 +453,11 @@ function parseMilliseconds(d, string, i) {
return n ? (d.L = +n[0], i + n[0].length) : -1;
}

function parseMicroseconds(d, string, i) {
var n = numberRe.exec(string.slice(i, i + 6));
return n ? (d.L = Math.floor(n[0] / 1000), i + n[0].length) : -1;
}

function parseLiteralPercent(d, string, i) {
var n = percentRe.exec(string.slice(i, i + 1));
return n ? i + n[0].length : -1;
Expand All @@ -475,6 +483,10 @@ function formatMilliseconds(d, p) {
return pad(d.getMilliseconds(), p, 3);
}

function formatMicroseconds(d, p) {
return formatMilliseconds(d, p) + "000";
}

function formatMonthNumber(d, p) {
return pad(d.getMonth() + 1, p, 2);
}
Expand Down Expand Up @@ -545,6 +557,10 @@ function formatUTCMilliseconds(d, p) {
return pad(d.getUTCMilliseconds(), p, 3);
}

function formatUTCMicroseconds(d, p) {
return formatUTCMilliseconds(d, p) + "000";
}

function formatUTCMonthNumber(d, p) {
return pad(d.getUTCMonth() + 1, p, 2);
}
Expand Down
7 changes: 7 additions & 0 deletions test/format-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@ tape("timeFormat(\"%u\")(date) formats week day numbers", function(test) {
test.end();
});

tape("timeFormat(\"%f\")(date) formats zero-padded microseconds", function(test) {
var f = timeFormat.timeFormat("%f");
test.equal(f(date.local(1990, 0, 1, 0, 0, 0, 0)), "000000");
test.equal(f(date.local(1990, 0, 1, 0, 0, 0, 432)), "432000");
test.end();
});

tape("timeFormat(\"%U\")(date) formats zero-padded week numbers", function(test) {
var f = timeFormat.timeFormat("%U");
test.equal(f(date.local(1990, 0, 1, 0)), "00");
Expand Down
12 changes: 12 additions & 0 deletions test/parse-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,18 @@ tape("timeParse(\"%X\")(date) parses locale time", function(test) {
test.end();
});

tape("timeParse(\"%L\")(date) parses milliseconds", function(test) {
var p = timeFormat.timeParse("%L");
test.deepEqual(p("432"), date.local(1900, 0, 1, 0, 0, 0, 432));
test.end();
});

tape("timeParse(\"%f\")(date) parses microseconds", function(test) {
var p = timeFormat.timeParse("%f");
test.deepEqual(p("432000"), date.local(1900, 0, 1, 0, 0, 0, 432));
test.end();
});

tape("timeParse(\"%I:%M:%S %p\")(date) parses twelve hour, minute and second", function(test) {
var p = timeFormat.timeParse("%I:%M:%S %p");
test.deepEqual(p("12:00:00 am"), date.local(1900, 0, 1, 0, 0, 0));
Expand Down
7 changes: 7 additions & 0 deletions test/utcFormat-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,13 @@ tape("utcFormat(\"%u\")(date) formats week day numbers", function(test) {
test.end();
});

tape("utcFormat(\"%f\")(date) formats zero-padded microseconds", function(test) {
var f = timeFormat.utcFormat("%f");
test.equal(f(date.utc(1990, 0, 1, 0, 0, 0, 0)), "000000");
test.equal(f(date.utc(1990, 0, 1, 0, 0, 0, 432)), "432000");
test.end();
});

tape("utcFormat(\"%U\")(date) formats zero-padded week numbers", function(test) {
var f = timeFormat.utcFormat("%U");
test.equal(f(date.utc(1990, 0, 1, 0)), "00");
Expand Down
12 changes: 12 additions & 0 deletions test/utcParse-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ tape("utcParse(\"\")(date) parses locale time", function(test) {
test.end();
});

tape("utcParse(\"%L\")(date) parses milliseconds", function(test) {
var p = timeFormat.utcParse("%L");
test.deepEqual(p("432"), date.utc(1900, 0, 1, 0, 0, 0, 432));
test.end();
});

tape("utcParse(\"%f\")(date) parses microseconds", function(test) {
var p = timeFormat.utcParse("%f");
test.deepEqual(p("432000"), date.utc(1900, 0, 1, 0, 0, 0, 432));
test.end();
});

tape("utcParse(\"\")(date) parses twelve hour, minute and second", function(test) {
var p = timeFormat.utcParse("%I:%M:%S %p");
test.deepEqual(p("12:00:00 am"), date.utc(1900, 0, 1, 0, 0, 0));
Expand Down

0 comments on commit 66ee751

Please sign in to comment.