2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-06-27 07:10:45 -04:00
|
|
|
import { assert, 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-06-27 07:10:45 -04:00
|
|
|
Deno.test({
|
2020-08-15 10:37:17 -04:00
|
|
|
name: "[std/datetime] parse",
|
2020-06-27 07:10:45 -04:00
|
|
|
fn: () => {
|
|
|
|
assertEquals(
|
2020-08-15 10:37:17 -04:00
|
|
|
datetime.parse("01-03-2019 16:30", "MM-dd-yyyy hh:mm"),
|
2020-07-14 15:24:17 -04:00
|
|
|
new Date(2019, 0, 3, 16, 30),
|
2020-06-27 07:10:45 -04:00
|
|
|
);
|
|
|
|
assertEquals(
|
2020-08-15 10:37:17 -04:00
|
|
|
datetime.parse("01.03.2019 16:30", "MM.dd.yyyy hh:mm"),
|
|
|
|
new Date(2019, 0, 3, 16, 30),
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
datetime.parse("03-01-2019 16:31", "dd-MM-yyyy hh:mm"),
|
2020-07-14 15:24:17 -04:00
|
|
|
new Date(2019, 0, 3, 16, 31),
|
2020-06-27 07:10:45 -04:00
|
|
|
);
|
|
|
|
assertEquals(
|
2020-08-15 10:37:17 -04:00
|
|
|
datetime.parse("2019-01-03 16:32", "yyyy-MM-dd hh:mm"),
|
2020-07-14 15:24:17 -04:00
|
|
|
new Date(2019, 0, 3, 16, 32),
|
2020-06-27 07:10:45 -04:00
|
|
|
);
|
|
|
|
assertEquals(
|
2020-08-15 10:37:17 -04:00
|
|
|
datetime.parse("16:33 01-03-2019", "hh:mm MM-dd-yyyy"),
|
2020-07-14 15:24:17 -04:00
|
|
|
new Date(2019, 0, 3, 16, 33),
|
2020-06-27 07:10:45 -04:00
|
|
|
);
|
|
|
|
assertEquals(
|
2020-08-15 10:37:17 -04:00
|
|
|
datetime.parse("01-03-2019 16:33:23.123", "MM-dd-yyyy hh:mm:ss.SSS"),
|
|
|
|
new Date(2019, 0, 3, 16, 33, 23, 123),
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
datetime.parse("01-03-2019 09:33 PM", "MM-dd-yyyy hh:mm a"),
|
|
|
|
new Date(2019, 0, 3, 21, 33),
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
datetime.parse("16:34 03-01-2019", "hh:mm dd-MM-yyyy"),
|
2020-07-14 15:24:17 -04:00
|
|
|
new Date(2019, 0, 3, 16, 34),
|
2020-06-27 07:10:45 -04:00
|
|
|
);
|
|
|
|
assertEquals(
|
2020-08-15 10:37:17 -04:00
|
|
|
datetime.parse("16:35 2019-01-03", "hh:mm yyyy-MM-dd"),
|
2020-07-14 15:24:17 -04:00
|
|
|
new Date(2019, 0, 3, 16, 35),
|
2020-06-27 07:10:45 -04:00
|
|
|
);
|
2020-08-15 10:37:17 -04:00
|
|
|
assertEquals(
|
|
|
|
datetime.parse("01-03-2019", "MM-dd-yyyy"),
|
|
|
|
new Date(2019, 0, 3),
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
datetime.parse("03-01-2019", "dd-MM-yyyy"),
|
|
|
|
new Date(2019, 0, 3),
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
datetime.parse("2019-01-03", "yyyy-MM-dd"),
|
|
|
|
new Date(2019, 0, 3),
|
|
|
|
);
|
2020-06-27 07:10:45 -04:00
|
|
|
},
|
2019-01-16 10:25:52 -05:00
|
|
|
});
|
|
|
|
|
2020-06-27 07:10:45 -04:00
|
|
|
Deno.test({
|
|
|
|
name: "[std/datetime] invalidParseDateTimeFormatThrows",
|
|
|
|
fn: () => {
|
2020-08-15 10:37:17 -04:00
|
|
|
assertThrows((): void => {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
(datetime as any).parse("2019-01-01 00:00", "x-y-z");
|
|
|
|
}, Error);
|
|
|
|
assertThrows((): void => {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
(datetime as any).parse("2019-01-01", "x-y-z");
|
|
|
|
}, Error);
|
2020-06-27 07:10:45 -04:00
|
|
|
},
|
2019-01-03 10:19:20 -05:00
|
|
|
});
|
2019-01-16 10:25:52 -05:00
|
|
|
|
2020-06-27 07:10:45 -04:00
|
|
|
Deno.test({
|
2020-08-15 10:37:17 -04:00
|
|
|
name: "[std/datetime] format",
|
2020-06-27 07:10:45 -04:00
|
|
|
fn: () => {
|
|
|
|
assertEquals(
|
2020-08-15 10:37:17 -04:00
|
|
|
"2019-01-01",
|
|
|
|
datetime.format(new Date("2019-01-01T03:24:00"), "yyyy-MM-dd"),
|
2020-06-27 07:10:45 -04:00
|
|
|
);
|
|
|
|
assertEquals(
|
2020-08-15 10:37:17 -04:00
|
|
|
"01.01.2019",
|
|
|
|
datetime.format(new Date("2019-01-01T03:24:00"), "dd.MM.yyyy"),
|
2020-06-27 07:10:45 -04:00
|
|
|
);
|
|
|
|
assertEquals(
|
2020-08-15 10:37:17 -04:00
|
|
|
"03:24:00",
|
|
|
|
datetime.format(new Date("2019-01-01T03:24:00"), "hh:mm:ss"),
|
2020-06-27 07:10:45 -04:00
|
|
|
);
|
2020-08-15 10:37:17 -04:00
|
|
|
assertEquals(
|
|
|
|
"03:24:00.532",
|
|
|
|
datetime.format(new Date("2019-01-01T03:24:00.532"), "hh:mm:ss.SSS"),
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
"03:24:00 AM",
|
|
|
|
datetime.format(new Date("2019-01-01T03:24:00"), "hh:mm:ss a"),
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
"09:24:00 PM",
|
|
|
|
datetime.format(new Date("2019-01-01T21:24:00"), "hh:mm:ss a"),
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
datetime.format(new Date(2019, 0, 20), "'today:' yyyy-MM-dd"),
|
|
|
|
"today: 2019-01-20",
|
2020-06-27 07:10:45 -04:00
|
|
|
);
|
|
|
|
},
|
2019-01-16 10:25:52 -05:00
|
|
|
});
|
|
|
|
|
2020-06-27 07:10:45 -04:00
|
|
|
Deno.test({
|
2020-07-14 14:23:54 -04:00
|
|
|
name: "[std/datetime] dayOfYear",
|
2020-06-27 07:10:45 -04:00
|
|
|
fn: () => {
|
2020-08-27 05:12:49 -04:00
|
|
|
// from https://golang.org/src/time/time_test.go
|
|
|
|
// Test YearDay in several different scenarios
|
|
|
|
// and corner cases
|
|
|
|
// Non-leap-year tests
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("2007-01-01")), 1);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("2007-01-15")), 15);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("2007-02-01")), 32);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("2007-02-15")), 46);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("2007-03-01")), 60);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("2007-03-15")), 74);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("2007-04-01")), 91);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("2007-12-31")), 365);
|
|
|
|
|
|
|
|
// Leap-year tests
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("2008-01-01")), 1);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("2008-01-15")), 15);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("2008-02-01")), 32);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("2008-02-15")), 46);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("2008-03-01")), 61);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("2008-03-15")), 75);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("2008-04-01")), 92);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("2008-12-31")), 366);
|
|
|
|
|
|
|
|
// Looks like leap-year (but isn't) tests
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("1900-01-01")), 1);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("1900-01-15")), 15);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("1900-02-01")), 32);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("1900-02-15")), 46);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("1900-03-01")), 60);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("1900-03-15")), 74);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("1900-04-01")), 91);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("1900-12-31")), 365);
|
|
|
|
|
|
|
|
// Year one tests (non-leap)
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("0001-01-01")), 1);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("0001-01-15")), 15);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("0001-02-01")), 32);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("0001-02-15")), 46);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("0001-03-01")), 60);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("0001-03-15")), 74);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("0001-04-01")), 91);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("0001-12-31")), 365);
|
|
|
|
|
|
|
|
// Year minus one tests (non-leap)
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("-000001-01-01")), 1);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("-000001-01-15")), 15);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("-000001-02-01")), 32);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("-000001-02-15")), 46);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("-000001-03-01")), 60);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("-000001-03-15")), 74);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("-000001-04-01")), 91);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("-000001-12-31")), 365);
|
|
|
|
|
|
|
|
// // 400 BC tests (leap-year)
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("-00400-01-01")), 1);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("-00400-01-15")), 15);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("-00400-02-01")), 32);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("-00400-02-15")), 46);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("-00400-03-01")), 61);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("-00400-03-15")), 75);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("-00400-04-01")), 92);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("-00400-12-31")), 366);
|
|
|
|
|
|
|
|
// Special Cases
|
|
|
|
|
|
|
|
// Gregorian calendar change (no effect)
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("1582-10-04T03:24:00")), 277);
|
|
|
|
assertEquals(datetime.dayOfYear(new Date("1582-10-15T03:24:00")), 288);
|
2020-06-27 07:10:45 -04:00
|
|
|
},
|
2019-03-11 11:00:30 -04:00
|
|
|
});
|
|
|
|
|
2020-07-09 15:50:42 -04:00
|
|
|
Deno.test({
|
2020-07-14 14:23:54 -04:00
|
|
|
name: "[std/datetime] weekOfYear",
|
2020-07-09 15:50:42 -04:00
|
|
|
fn: () => {
|
|
|
|
assertEquals(datetime.weekOfYear(new Date("2020-01-05T03:24:00")), 1);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date("2020-12-28T03:24:00")), 53); // 53 weeks in 2020
|
|
|
|
assertEquals(datetime.weekOfYear(new Date("2020-06-28T03:24:00")), 26);
|
2020-07-14 14:23:54 -04:00
|
|
|
|
|
|
|
// iso weeks year starting sunday
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2012, 0, 1)), 52);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2012, 0, 2)), 1);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2012, 0, 8)), 1);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2012, 0, 9)), 2);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2012, 0, 15)), 2);
|
|
|
|
|
|
|
|
// iso weeks year starting monday
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2007, 0, 1)), 1);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2007, 0, 7)), 1);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2007, 0, 8)), 2);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2007, 0, 14)), 2);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2007, 0, 15)), 3);
|
|
|
|
|
|
|
|
// iso weeks year starting tuesday
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2007, 11, 31)), 1);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2008, 0, 1)), 1);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2008, 0, 6)), 1);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2008, 0, 7)), 2);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2008, 0, 13)), 2);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2008, 0, 14)), 3);
|
|
|
|
|
|
|
|
// iso weeks year starting wednesday
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2002, 11, 30)), 1);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2003, 0, 1)), 1);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2003, 0, 5)), 1);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2003, 0, 6)), 2);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2003, 0, 12)), 2);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2003, 0, 13)), 3);
|
|
|
|
|
|
|
|
// iso weeks year starting thursday
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2008, 11, 29)), 1);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2009, 0, 1)), 1);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2009, 0, 4)), 1);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2009, 0, 5)), 2);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2009, 0, 11)), 2);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2009, 0, 13)), 3);
|
|
|
|
|
|
|
|
// iso weeks year starting friday
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2009, 11, 28)), 53);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2010, 0, 1)), 53);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2010, 0, 3)), 53);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2010, 0, 4)), 1);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2010, 0, 10)), 1);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2010, 0, 11)), 2);
|
|
|
|
|
|
|
|
// iso weeks year starting saturday
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2010, 11, 27)), 52);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2011, 0, 1)), 52);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2011, 0, 2)), 52);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2011, 0, 3)), 1);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2011, 0, 9)), 1);
|
|
|
|
assertEquals(datetime.weekOfYear(new Date(2011, 0, 10)), 2);
|
2020-06-27 07:10:45 -04:00
|
|
|
},
|
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({
|
2020-06-27 07:10:45 -04:00
|
|
|
name: "[std/datetime] to IMF",
|
2019-04-27 19:07:11 -04:00
|
|
|
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({
|
2020-06-27 07:10:45 -04:00
|
|
|
name: "[std/datetime] to IMF 0",
|
2019-05-13 14:03:24 -04:00
|
|
|
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
|
|
|
});
|
2020-06-27 07:10:45 -04:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "[std/datetime] isLeap",
|
|
|
|
fn(): void {
|
|
|
|
assert(datetime.isLeap(1992));
|
|
|
|
assert(datetime.isLeap(2000));
|
|
|
|
assert(!datetime.isLeap(2003));
|
|
|
|
assert(!datetime.isLeap(2007));
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2020-08-15 10:37:17 -04:00
|
|
|
name: "[std/datetime] difference",
|
2020-06-27 07:10:45 -04:00
|
|
|
fn(): void {
|
|
|
|
const denoInit = new Date("2018/5/14");
|
|
|
|
const denoRelaseV1 = new Date("2020/5/13");
|
|
|
|
let difference = datetime.difference(denoRelaseV1, denoInit, {
|
|
|
|
units: ["days", "months", "years"],
|
|
|
|
});
|
|
|
|
assertEquals(difference.days, 730);
|
|
|
|
assertEquals(difference.months, 23);
|
|
|
|
assertEquals(difference.years, 1);
|
|
|
|
|
|
|
|
const birth = new Date("1998/2/23 10:10:10");
|
|
|
|
const old = new Date("1998/2/23 11:11:11");
|
|
|
|
difference = datetime.difference(birth, old, {
|
|
|
|
units: ["miliseconds", "minutes", "seconds", "hours"],
|
|
|
|
});
|
|
|
|
assertEquals(difference.miliseconds, 3661000);
|
|
|
|
assertEquals(difference.seconds, 3661);
|
|
|
|
assertEquals(difference.minutes, 61);
|
|
|
|
assertEquals(difference.hours, 1);
|
|
|
|
},
|
|
|
|
});
|