Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ISO weekday number (%u) #36

Merged
merged 2 commits into from
Oct 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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].
* `%w` - Sunday-based weekday as a decimal number [0,6].
* `%W` - Monday-based week of the year as a decimal number [00,53].
Expand Down
33 changes: 26 additions & 7 deletions src/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ export default function formatLocale(locale) {
"M": formatMinutes,
"p": formatPeriod,
"S": formatSeconds,
"u": formatWeekdayNumberMonday,
"U": formatWeekNumberSunday,
"w": formatWeekdayNumber,
"w": formatWeekdayNumberSunday,
"W": formatWeekNumberMonday,
"x": null,
"X": null,
Expand All @@ -86,8 +87,9 @@ export default function formatLocale(locale) {
"M": formatUTCMinutes,
"p": formatUTCPeriod,
"S": formatUTCSeconds,
"u": formatUTCWeekdayNumberMonday,
"U": formatUTCWeekNumberSunday,
"w": formatUTCWeekdayNumber,
"w": formatUTCWeekdayNumberSunday,
"W": formatUTCWeekNumberMonday,
"x": null,
"X": null,
Expand All @@ -113,8 +115,9 @@ export default function formatLocale(locale) {
"M": parseMinutes,
"p": parsePeriod,
"S": parseSeconds,
"u": parseWeekdayNumberMonday,
"U": parseWeekNumberSunday,
"w": parseWeekdayNumber,
"w": parseWeekdayNumberSunday,
"W": parseWeekNumberMonday,
"x": parseLocaleDate,
"X": parseLocaleTime,
Expand Down Expand Up @@ -171,7 +174,7 @@ export default function formatLocale(locale) {

// Convert day-of-week and week-of-year to day-of-year.
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;
var 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 @@ -339,11 +342,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));
return n ? (d.U = +n[0], i + n[0].length) : -1;
Expand Down Expand Up @@ -441,11 +450,16 @@ 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);
}

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

Expand Down Expand Up @@ -500,11 +514,16 @@ 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);
}

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
24 changes: 22 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 @@ -66,6 +66,17 @@ tape("timeParse(\"%w %U %Y\")(date) parses numeric weekday, week number (Sunday)
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();
});

tape("timeParse(\"%W %Y\")(date) parses week number (Monday) and year", function(test) {
var p = timeFormat.timeParse("%W %Y");
test.deepEqual(p("01 1990"), date.local(1990, 0, 1));
Expand All @@ -92,7 +103,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 @@ -101,6 +112,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