2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-06-05 14:35:35 -04:00
|
|
|
import { assertEquals, assertThrows } from "../testing/asserts.ts";
|
2019-02-23 11:13:54 -05:00
|
|
|
import * as datetime from "./mod.ts";
|
2019-01-03 10:19:20 -05:00
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(function parseDateTime(): void {
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(
|
2019-03-26 08:15:16 -04:00
|
|
|
datetime.parseDateTime("01-03-2019 16:30", "mm-dd-yyyy hh:mm"),
|
|
|
|
new Date(2019, 0, 3, 16, 30)
|
2019-01-03 10:19:20 -05:00
|
|
|
);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(
|
2019-03-26 08:15:16 -04:00
|
|
|
datetime.parseDateTime("03-01-2019 16:31", "dd-mm-yyyy hh:mm"),
|
|
|
|
new Date(2019, 0, 3, 16, 31)
|
2019-01-16 10:25:52 -05:00
|
|
|
);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(
|
2019-03-26 08:15:16 -04:00
|
|
|
datetime.parseDateTime("2019-01-03 16:32", "yyyy-mm-dd hh:mm"),
|
|
|
|
new Date(2019, 0, 3, 16, 32)
|
2019-01-16 10:25:52 -05:00
|
|
|
);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(
|
2019-03-26 08:15:16 -04:00
|
|
|
datetime.parseDateTime("16:33 01-03-2019", "hh:mm mm-dd-yyyy"),
|
|
|
|
new Date(2019, 0, 3, 16, 33)
|
2019-01-16 10:25:52 -05:00
|
|
|
);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(
|
2019-01-16 10:25:52 -05:00
|
|
|
datetime.parseDateTime("16:34 03-01-2019", "hh:mm dd-mm-yyyy"),
|
2019-03-26 08:15:16 -04:00
|
|
|
new Date(2019, 0, 3, 16, 34)
|
2019-01-16 10:25:52 -05:00
|
|
|
);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(
|
2019-03-26 08:15:16 -04:00
|
|
|
datetime.parseDateTime("16:35 2019-01-03", "hh:mm yyyy-mm-dd"),
|
|
|
|
new Date(2019, 0, 3, 16, 35)
|
2019-01-16 10:25:52 -05:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(function invalidParseDateTimeFormatThrows(): void {
|
2019-06-05 14:35:35 -04:00
|
|
|
assertThrows(
|
|
|
|
(): void => {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
(datetime as any).parseDateTime("2019-01-01 00:00", "x-y-z");
|
|
|
|
},
|
|
|
|
Error,
|
|
|
|
"Invalid datetime format!"
|
|
|
|
);
|
2019-01-03 10:19:20 -05:00
|
|
|
});
|
2019-01-16 10:25:52 -05:00
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(function parseDate(): void {
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(
|
2019-01-11 00:19:42 -05:00
|
|
|
datetime.parseDate("01-03-2019", "mm-dd-yyyy"),
|
2019-03-26 08:15:16 -04:00
|
|
|
new Date(2019, 0, 3)
|
2019-01-03 10:19:20 -05:00
|
|
|
);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(
|
2019-01-16 10:25:52 -05:00
|
|
|
datetime.parseDate("03-01-2019", "dd-mm-yyyy"),
|
2019-03-26 08:15:16 -04:00
|
|
|
new Date(2019, 0, 3)
|
2019-01-16 10:25:52 -05:00
|
|
|
);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(
|
2019-01-16 10:25:52 -05:00
|
|
|
datetime.parseDate("2019-01-03", "yyyy-mm-dd"),
|
2019-03-26 08:15:16 -04:00
|
|
|
new Date(2019, 0, 3)
|
2019-01-16 10:25:52 -05:00
|
|
|
);
|
2019-01-03 10:19:20 -05:00
|
|
|
});
|
2019-01-16 10:25:52 -05:00
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(function invalidParseDateFormatThrows(): void {
|
2019-06-05 14:35:35 -04:00
|
|
|
assertThrows(
|
|
|
|
(): void => {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
(datetime as any).parseDate("2019-01-01", "x-y-z");
|
|
|
|
},
|
|
|
|
Error,
|
|
|
|
"Invalid date format!"
|
|
|
|
);
|
2019-01-16 10:25:52 -05:00
|
|
|
});
|
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(function DayOfYear(): void {
|
2019-03-11 11:00:30 -04:00
|
|
|
assertEquals(1, datetime.dayOfYear(new Date("2019-01-01T03:24:00")));
|
|
|
|
assertEquals(70, datetime.dayOfYear(new Date("2019-03-11T03:24:00")));
|
|
|
|
assertEquals(365, datetime.dayOfYear(new Date("2019-12-31T03:24:00")));
|
|
|
|
});
|
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(function currentDayOfYear(): void {
|
2019-03-11 11:00:30 -04:00
|
|
|
assertEquals(datetime.currentDayOfYear(), datetime.dayOfYear(new Date()));
|
2019-01-03 10:19:20 -05:00
|
|
|
});
|
2019-04-27 19:07:11 -04:00
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test({
|
2019-04-27 19:07:11 -04:00
|
|
|
name: "[DateTime] to IMF",
|
|
|
|
fn(): void {
|
|
|
|
const actual = datetime.toIMF(new Date(Date.UTC(1994, 3, 5, 15, 32)));
|
2019-11-16 08:24:07 -05:00
|
|
|
const expected = "Tue, 05 Apr 1994 15:32:00 GMT";
|
2019-04-27 19:07:11 -04:00
|
|
|
assertEquals(actual, expected);
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2019-04-27 19:07:11 -04:00
|
|
|
});
|
2019-05-13 14:03:24 -04:00
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test({
|
2019-05-13 14:03:24 -04:00
|
|
|
name: "[DateTime] to IMF 0",
|
|
|
|
fn(): void {
|
|
|
|
const actual = datetime.toIMF(new Date(0));
|
2019-11-16 08:24:07 -05:00
|
|
|
const expected = "Thu, 01 Jan 1970 00:00:00 GMT";
|
2019-05-13 14:03:24 -04:00
|
|
|
assertEquals(actual, expected);
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2019-05-13 14:03:24 -04:00
|
|
|
});
|