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

fix(datetime): handle am / pm variants #5406

Merged
merged 18 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 14 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
35 changes: 33 additions & 2 deletions datetime/_date_time_formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,21 @@
break;
}
case "dayPeriod": {
value = /^(A|P)M/.exec(string)?.[0] as string;
value = /^[AP](?:\.M\.|M\.?)/i.exec(string)?.[0] as string;
switch (value.toUpperCase()) {
case "AM":
case "AM.":
case "A.M.":
value = "AM";
break;

Check warning on line 471 in datetime/_date_time_formatter.ts

View check run for this annotation

Codecov / codecov/patch

datetime/_date_time_formatter.ts#L470-L471

Added lines #L470 - L471 were not covered by tests
case "PM":
case "PM.":
case "P.M.":
value = "PM";
break;
default:
throw new Error(`dayPeriod '${value}' is not supported.`);

Check warning on line 478 in datetime/_date_time_formatter.ts

View check run for this annotation

Codecov / codecov/patch

datetime/_date_time_formatter.ts#L478

Added line #L478 was not covered by tests
}
break;
}
case "literal": {
Expand Down Expand Up @@ -542,7 +556,24 @@
const dayPeriod = parts.find(
(part: DateTimeFormatPart) => part.type === "dayPeriod",
);
if (dayPeriod?.value === "PM") value += 12;
if (dayPeriod) {
switch (dayPeriod.value.toUpperCase()) {
case "AM":
case "AM.":
case "A.M.":
// ignore
break;
case "PM":
case "PM.":
case "P.M.":
value += 12;
break;
default:
throw new Error(
`dayPeriod '${dayPeriod.value}' is not supported.`,
);
}
}
utc ? date.setUTCHours(value) : date.setHours(value);
break;
}
Expand Down
144 changes: 144 additions & 0 deletions datetime/_date_time_formatter_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ Deno.test("dateTimeFormatter.format()", () => {
new Date(2020, 0, 1, 23, 59, 59),
"2020-01-01 23:59:59 PM",
],
[
"yyyy-MM-dd hh:mm:ss a",
new Date(2020, 0, 1, 23, 59, 59),
"2020-01-01 11:59:59 PM",
],
["yyyy-MM-dd a", new Date(2020, 0, 1), "2020-01-01 AM"],
["yyyy-MM-dd HH:mm:ss a", new Date(2020, 0, 1), "2020-01-01 00:00:00 AM"],
["yyyy-MM-dd hh:mm:ss a", new Date(2020, 0, 1), "2020-01-01 12:00:00 AM"],
["yyyy", new Date(2020, 0, 1), "2020"],
["MM", new Date(2020, 0, 1), "01"],
] as const;
Expand Down Expand Up @@ -87,6 +94,143 @@ Deno.test("dateTimeFormatter.partsToDate()", () => {
]),
+date,
);
assertEquals(
+formatter.partsToDate([
{ type: "year", value: "2020" },
{ type: "month", value: "01" },
{ type: "day", value: "01" },
{ type: "hour", value: "00" },
{ type: "minute", value: "00" },
{ type: "second", value: "00" },
{ type: "fractionalSecond", value: "000" },
{ type: "dayPeriod", value: "am" },
{ type: "timeZoneName", value: "UTC" },
]),
+date,
);
assertEquals(
+formatter.partsToDate([
{ type: "year", value: "2020" },
{ type: "month", value: "01" },
{ type: "day", value: "01" },
{ type: "hour", value: "00" },
{ type: "minute", value: "00" },
{ type: "second", value: "00" },
{ type: "fractionalSecond", value: "000" },
{ type: "dayPeriod", value: "a.m." },
{ type: "timeZoneName", value: "UTC" },
]),
+date,
);
assertEquals(
+formatter.partsToDate([
{ type: "year", value: "2020" },
{ type: "month", value: "01" },
{ type: "day", value: "01" },
{ type: "hour", value: "00" },
{ type: "minute", value: "00" },
{ type: "second", value: "00" },
{ type: "fractionalSecond", value: "000" },
{ type: "dayPeriod", value: "am." },
{ type: "timeZoneName", value: "UTC" },
]),
+date,
);
});
Deno.test("dateTimeFormatter.partsToDate() works with dayPeriod", () => {
timreichen marked this conversation as resolved.
Show resolved Hide resolved
const date = new Date("2020-01-01T00:00:00.000Z");
using _time = new FakeTime(date);
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
const format = "HH a";
const formatter = new DateTimeFormatter(format);
assertEquals(
+formatter.partsToDate([
{ type: "hour", value: "00" },
{ type: "dayPeriod", value: "AM" },
{ type: "timeZoneName", value: "UTC" },
]),
+date,
);
assertEquals(
+formatter.partsToDate([
{ type: "hour", value: "00" },
{ type: "dayPeriod", value: "AM." },
{ type: "timeZoneName", value: "UTC" },
]),
+date,
);
assertEquals(
+formatter.partsToDate([
{ type: "hour", value: "00" },
{ type: "dayPeriod", value: "A.M." },
{ type: "timeZoneName", value: "UTC" },
]),
+date,
);
assertEquals(
+formatter.partsToDate([
{ type: "hour", value: "00" },
{ type: "dayPeriod", value: "am" },
{ type: "timeZoneName", value: "UTC" },
]),
+date,
);
assertEquals(
+formatter.partsToDate([
{ type: "hour", value: "00" },
{ type: "dayPeriod", value: "am." },
{ type: "timeZoneName", value: "UTC" },
]),
+date,
);
assertEquals(
+formatter.partsToDate([
{ type: "hour", value: "00" },
{ type: "dayPeriod", value: "a.m." },
{ type: "timeZoneName", value: "UTC" },
]),
+date,
);
});
Deno.test("dateTimeFormatter.partsToDate() throws with invalid dayPeriods", () => {
const date = new Date("2020-01-01T00:00:00.000Z");
using _time = new FakeTime(date);
timreichen marked this conversation as resolved.
Show resolved Hide resolved
const format = "HH a";
const formatter = new DateTimeFormatter(format);
assertThrows(() =>
timreichen marked this conversation as resolved.
Show resolved Hide resolved
formatter.partsToDate([
{ type: "hour", value: "00" },
{ type: "dayPeriod", value: "A.M" },
{ type: "timeZoneName", value: "UTC" },
])
);
assertThrows(() =>
formatter.partsToDate([
{ type: "hour", value: "00" },
{ type: "dayPeriod", value: "a.m" },
{ type: "timeZoneName", value: "UTC" },
])
);
assertThrows(() =>
formatter.partsToDate([
{ type: "hour", value: "00" },
{ type: "dayPeriod", value: "P.M" },
{ type: "timeZoneName", value: "UTC" },
])
);
assertThrows(() =>
formatter.partsToDate([
{ type: "hour", value: "00" },
{ type: "dayPeriod", value: "p.m" },
{ type: "timeZoneName", value: "UTC" },
])
);
assertThrows(() =>
formatter.partsToDate([
{ type: "hour", value: "00" },
{ type: "dayPeriod", value: "noon" },
{ type: "timeZoneName", value: "UTC" },
])
);
});

Deno.test("dateTimeFormatter.partsToDate() sets utc", () => {
Expand Down
Loading