Skip to content

Commit

Permalink
Merge branch 'ISO-weekday-number' of https://github.com/BrianMitchL/d…
Browse files Browse the repository at this point in the history
…3-time-format into BrianMitchL-ISO-weekday-number
  • Loading branch information
mbostock committed Oct 9, 2017
2 parents 89ca217 + 8efc194 commit ea769c0
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ Returns a new formatter for the given string *specifier*. The specifier string m
* `%L` - milliseconds as a decimal number [000, 999].
* `%p` - either AM or PM.*
* `%S` - second as a decimal number [00,61].
* `%u` - Monday-based (ISO) weekday as a decimal number [1,7].
* `%U` - Sunday-based week of the year as a decimal number [00,53].
* `%V` - ISO 8601 week number of the year as a decimal number [01, 53].
* `%w` - Sunday-based weekday as a decimal number [0,6].
Expand Down
33 changes: 26 additions & 7 deletions src/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ export default function formatLocale(locale) {
"M": formatMinutes,
"p": formatPeriod,
"S": formatSeconds,
"u": formatWeekdayNumberMonday,
"U": formatWeekNumberSunday,
"V": formatWeekNumberISO,
"w": formatWeekdayNumber,
"w": formatWeekdayNumberSunday,
"W": formatWeekNumberMonday,
"x": null,
"X": null,
Expand All @@ -98,9 +99,10 @@ export default function formatLocale(locale) {
"M": formatUTCMinutes,
"p": formatUTCPeriod,
"S": formatUTCSeconds,
"u": formatUTCWeekdayNumberMonday,
"U": formatUTCWeekNumberSunday,
"V": formatUTCWeekNumberISO,
"w": formatUTCWeekdayNumber,
"w": formatUTCWeekdayNumberSunday,
"W": formatUTCWeekNumberMonday,
"x": null,
"X": null,
Expand All @@ -126,9 +128,10 @@ export default function formatLocale(locale) {
"M": parseMinutes,
"p": parsePeriod,
"S": parseSeconds,
"u": parseWeekdayNumberMonday,
"U": parseWeekNumberSunday,
"V": parseWeekNumberISO,
"w": parseWeekdayNumber,
"w": parseWeekdayNumberSunday,
"W": parseWeekNumberMonday,
"x": parseLocaleDate,
"X": parseLocaleTime,
Expand Down Expand Up @@ -204,7 +207,7 @@ export default function formatLocale(locale) {
d.d = week.getDate() + (d.w + 6) % 7;
}
} else if ("W" in d || "U" in d) {
if (!("w" in d)) d.w = "W" in d ? 1 : 0;
if (!("w" in d)) d.w = "u" in d ? d.u % 7 : "W" in d ? 1 : 0;
day = "Z" in d ? utcDate(newYear(d.y)).getUTCDay() : newDate(newYear(d.y)).getDay();
d.m = 0;
d.d = "W" in d ? (d.w + 6) % 7 + d.W * 7 - (day + 5) % 7 : d.w + d.U * 7 - (day + 6) % 7;
Expand Down Expand Up @@ -372,11 +375,17 @@ function formatLookup(names) {
return map;
}

function parseWeekdayNumber(d, string, i) {
function parseWeekdayNumberSunday(d, string, i) {
var n = numberRe.exec(string.slice(i, i + 1));
return n ? (d.w = +n[0], i + n[0].length) : -1;
}

function parseWeekdayNumberMonday(d, string, i) {
var n = numberRe.exec(string.slice(i, i + 1));
var r = n ? (d.u = +n[0], i + n[0].length) : -1;
return r;
}

function parseWeekNumberSunday(d, string, i) {
var n = numberRe.exec(string.slice(i, i + 2));
return n ? (d.U = +n[0], i + n[0].length) : -1;
Expand Down Expand Up @@ -479,6 +488,11 @@ function formatSeconds(d, p) {
return pad(d.getSeconds(), p, 2);
}

function formatWeekdayNumberMonday(d) {
var dow = d.getDay();
return dow === 0 ? 7 : dow;
}

function formatWeekNumberSunday(d, p) {
return pad(timeSunday.count(timeYear(d), d), p, 2);
}
Expand All @@ -489,7 +503,7 @@ function formatWeekNumberISO(d, p) {
return pad(timeThursday.count(timeYear(d), d) + (timeYear(d).getDay() === 4), p, 2);
}

function formatWeekdayNumber(d) {
function formatWeekdayNumberSunday(d) {
return d.getDay();
}

Expand Down Expand Up @@ -544,6 +558,11 @@ function formatUTCSeconds(d, p) {
return pad(d.getUTCSeconds(), p, 2);
}

function formatUTCWeekdayNumberMonday(d) {
var dow = d.getUTCDay();
return dow === 0 ? 7 : dow;
}

function formatUTCWeekNumberSunday(d, p) {
return pad(utcSunday.count(utcYear(d), d), p, 2);
}
Expand All @@ -554,7 +573,7 @@ function formatUTCWeekNumberISO(d, p) {
return pad(utcThursday.count(utcYear(d), d) + (utcYear(d).getUTCDay() === 4), p, 2);
}

function formatUTCWeekdayNumber(d) {
function formatUTCWeekdayNumberSunday(d) {
return d.getUTCDay();
}

Expand Down
8 changes: 8 additions & 0 deletions test/format-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@ tape("timeFormat(\"%L\")(date) formats zero-padded milliseconds", function(test)
test.end();
});

tape("timeFormat(\"%u\")(date) formats week day numbers", function(test) {
var f = timeFormat.timeFormat("%u");
test.equal(f(date.local(1990, 0, 1, 0)), "1");
test.equal(f(date.local(1990, 0, 7, 0)), "7");
test.equal(f(date.local(2010, 2, 13, 23)), "6");
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
23 changes: 21 additions & 2 deletions test/parse-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ tape("timeParse(\"%A %U %Y\")(date) parses weekday, week number (Sunday) and yea
test.end();
});

tape("timeParse(\"%w %U %Y\")(date) parses numeric weekday, week number (Sunday) and year", function(test) {
tape("timeParse(\"%w %U %Y\")(date) parses numeric weekday (Sunday), week number (Sunday) and year", function(test) {
var p = timeFormat.timeParse("%w %U %Y");
test.deepEqual(p("1 00 1990"), date.local(1990, 0, 1));
test.deepEqual(p("0 05 1991"), date.local(1991, 1, 3));
Expand All @@ -73,6 +73,16 @@ tape("timeParse(\"%w %V %Y\")(date) parses numeric weekday, week number (ISO) an
test.deepEqual(p("4 53 1992"), date.local(1992, 11, 31));
test.deepEqual(p("0 52 1994"), date.local(1995, 0, 1));
test.deepEqual(p("0 01 1995"), date.local(1995, 0, 8));
test.end();
});

tape("timeParse(\"%u %U %Y\")(date) parses numeric weekday (Monday), week number (Monday) and year", function(test) {
var p = timeFormat.timeParse("%u %W %Y");
test.deepEqual(p("1 00 1990"), date.local(1989, 11, 25));
test.deepEqual(p("1 01 1990"), date.local(1990, 0, 1));
test.deepEqual(p("1 05 1991"), date.local(1991, 1, 4));
test.deepEqual(p("7 00 1995"), date.local(1995, 0, 1));
test.deepEqual(p("1 01 1995"), date.local(1995, 0, 2));
test.equal(p("X 03 2010"), null);
test.end();
});
Expand Down Expand Up @@ -103,7 +113,7 @@ tape("timeParse(\"%A %W %Y\")(date) parses weekday, week number (Monday) and yea
test.end();
});

tape("timeParse(\"%w %W %Y\")(date) parses numeric weekday, week number (Monday) and year", function(test) {
tape("timeParse(\"%w %W %Y\")(date) parses numeric weekday (Sunday), week number (Monday) and year", function(test) {
var p = timeFormat.timeParse("%w %W %Y");
test.deepEqual(p("1 01 1990"), date.local(1990, 0, 1));
test.deepEqual(p("0 04 1991"), date.local(1991, 1, 3));
Expand All @@ -112,6 +122,15 @@ tape("timeParse(\"%w %W %Y\")(date) parses numeric weekday, week number (Monday)
test.end();
});

tape("timeParse(\"%u %W %Y\")(date) parses numeric weekday (Monday), week number (Monday) and year", function(test) {
var p = timeFormat.timeParse("%u %W %Y");
test.deepEqual(p("1 01 1990"), date.local(1990, 0, 1));
test.deepEqual(p("7 04 1991"), date.local(1991, 1, 3));
test.deepEqual(p("7 00 1995"), date.local(1995, 0, 1));
test.equal(p("X 03 2010"), null);
test.end();
});

tape("timeParse(\"%m/%d/%y\")(date) parses month, date and two-digit year", function(test) {
var p = timeFormat.timeParse("%m/%d/%y");
test.deepEqual(p("02/03/69"), date.local(1969, 1, 3));
Expand Down
8 changes: 8 additions & 0 deletions test/utcFormat-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@ tape("utcFormat(\"%L\")(date) formats zero-padded milliseconds", function(test)
test.end();
});

tape("utcFormat(\"%u\")(date) formats week day numbers", function(test) {
var f = timeFormat.utcFormat("%u");
test.equal(f(date.utc(1990, 0, 1, 0)), "1");
test.equal(f(date.utc(1990, 0, 7, 0)), "7");
test.equal(f(date.utc(2010, 2, 13, 23)), "6");
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

0 comments on commit ea769c0

Please sign in to comment.