2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-04-29 02:57:30 +00:00
|
|
|
import { assertEquals } from "@std/assert";
|
2022-12-19 10:24:48 +00:00
|
|
|
import { format } from "./format.ts";
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-19 00:57:06 +00:00
|
|
|
name: "format()",
|
2022-12-19 10:24:48 +00:00
|
|
|
fn: () => {
|
|
|
|
// 00 hours
|
2023-09-22 05:41:22 +00:00
|
|
|
assertEquals(
|
|
|
|
"00:00:00",
|
|
|
|
format(new Date("2019-01-01T00:00:00"), "HH:mm:ss"),
|
|
|
|
);
|
2022-12-19 10:24:48 +00:00
|
|
|
assertEquals(
|
|
|
|
"01:00:00",
|
|
|
|
format(new Date("2019-01-01T01:00:00"), "HH:mm:ss"),
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
"13:00:00",
|
|
|
|
format(new Date("2019-01-01T13:00:00"), "HH:mm:ss"),
|
|
|
|
);
|
|
|
|
|
|
|
|
// 12 hours
|
2023-09-22 05:41:22 +00:00
|
|
|
assertEquals(
|
|
|
|
"12:00:00",
|
|
|
|
format(new Date("2019-01-01T00:00:00"), "hh:mm:ss"),
|
|
|
|
);
|
2022-12-19 10:24:48 +00:00
|
|
|
assertEquals(
|
|
|
|
"01:00:00",
|
|
|
|
format(new Date("2019-01-01T01:00:00"), "hh:mm:ss"),
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
"01:00:00",
|
|
|
|
format(new Date("2019-01-01T13:00:00"), "hh:mm:ss"),
|
|
|
|
);
|
|
|
|
|
|
|
|
// milliseconds
|
|
|
|
assertEquals(
|
|
|
|
"13:00:00.000",
|
|
|
|
format(new Date("2019-01-01T13:00:00"), "HH:mm:ss.SSS"),
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
"13:00:00.000",
|
|
|
|
format(new Date("2019-01-01T13:00:00.000"), "HH:mm:ss.SSS"),
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
"13:00:00.123",
|
|
|
|
format(new Date("2019-01-01T13:00:00.123"), "HH:mm:ss.SSS"),
|
|
|
|
);
|
|
|
|
|
|
|
|
// day period
|
2023-09-22 05:41:22 +00:00
|
|
|
assertEquals(
|
|
|
|
"00:00:00 AM",
|
|
|
|
format(new Date("2019-01-01T00:00:00"), "HH:mm:ss a"),
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
"12:00:00 AM",
|
|
|
|
format(new Date("2019-01-01T00:00:00"), "hh:mm:ss a"),
|
|
|
|
);
|
2022-12-19 10:24:48 +00:00
|
|
|
assertEquals(
|
|
|
|
"01:00:00 AM",
|
|
|
|
format(new Date("2019-01-01T01:00:00"), "HH:mm:ss a"),
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
"01:00:00 AM",
|
|
|
|
format(new Date("2019-01-01T01:00:00"), "hh:mm:ss a"),
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
"01:00:00 PM",
|
|
|
|
format(new Date("2019-01-01T13:00:00"), "hh:mm:ss a"),
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
"21:00:00 PM",
|
|
|
|
format(new Date("2019-01-01T21:00:00"), "HH:mm:ss a"),
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
"09:00:00 PM",
|
|
|
|
format(new Date("2019-01-01T21:00:00"), "hh:mm:ss a"),
|
|
|
|
);
|
|
|
|
|
|
|
|
// quoted literal
|
|
|
|
assertEquals(
|
|
|
|
format(new Date(2019, 0, 20), "'today:' yyyy-MM-dd"),
|
|
|
|
"today: 2019-01-20",
|
|
|
|
);
|
2023-02-20 06:05:32 +00:00
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
format(new Date("2019-01-09T21:09:09"), "H:m:s yy-M-d"),
|
|
|
|
"21:9:9 19-1-9",
|
|
|
|
);
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
"13:00:00.00",
|
|
|
|
format(new Date("2019-01-01T13:00:00.000"), "HH:mm:ss.SS"),
|
|
|
|
);
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
"13:00:00.0",
|
|
|
|
format(new Date("2019-01-01T13:00:00.000"), "HH:mm:ss.S"),
|
|
|
|
);
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
"1",
|
|
|
|
format(new Date("2019-01-01T13:00:00.000"), "h"),
|
|
|
|
);
|
2024-02-09 10:35:14 +00:00
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
"2019-01-01 04:00:00.000",
|
|
|
|
format(
|
|
|
|
new Date("2019-01-01T13:00:00.000+09:00"),
|
|
|
|
"yyyy-MM-dd HH:mm:ss.SSS",
|
2024-08-08 15:55:15 +00:00
|
|
|
{ timeZone: "UTC" },
|
2024-02-09 10:35:14 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
"2019-01-01 18:00:00.000",
|
|
|
|
format(
|
|
|
|
new Date("2019-01-01T13:00:00.000-05:00"),
|
|
|
|
"yyyy-MM-dd HH:mm:ss.SSS",
|
2024-08-08 15:55:15 +00:00
|
|
|
{ timeZone: "UTC" },
|
2024-02-09 10:35:14 +00:00
|
|
|
),
|
|
|
|
);
|
2022-12-19 10:24:48 +00:00
|
|
|
},
|
|
|
|
});
|