2019-02-07 11:45:47 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-03-06 16:39:50 -05:00
|
|
|
import { test } from "../testing/mod.ts";
|
|
|
|
import { assert, assertEq } 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
|
|
|
|
|
|
|
test(function parseDateTime() {
|
2019-03-06 16:39:50 -05:00
|
|
|
assertEq(
|
2019-01-03 10:19:20 -05:00
|
|
|
datetime.parseDateTime("01-03-2019 16:34", "mm-dd-yyyy hh:mm"),
|
|
|
|
new Date(2019, 1, 3, 16, 34)
|
|
|
|
);
|
2019-03-06 16:39:50 -05:00
|
|
|
assertEq(
|
2019-01-16 10:25:52 -05:00
|
|
|
datetime.parseDateTime("03-01-2019 16:34", "dd-mm-yyyy hh:mm"),
|
|
|
|
new Date(2019, 1, 3, 16, 34)
|
|
|
|
);
|
2019-03-06 16:39:50 -05:00
|
|
|
assertEq(
|
2019-01-16 10:25:52 -05:00
|
|
|
datetime.parseDateTime("2019-01-03 16:34", "yyyy-mm-dd hh:mm"),
|
|
|
|
new Date(2019, 1, 3, 16, 34)
|
|
|
|
);
|
2019-03-06 16:39:50 -05:00
|
|
|
assertEq(
|
2019-01-16 10:25:52 -05:00
|
|
|
datetime.parseDateTime("16:34 01-03-2019", "hh:mm mm-dd-yyyy"),
|
|
|
|
new Date(2019, 1, 3, 16, 34)
|
|
|
|
);
|
2019-03-06 16:39:50 -05:00
|
|
|
assertEq(
|
2019-01-16 10:25:52 -05:00
|
|
|
datetime.parseDateTime("16:34 03-01-2019", "hh:mm dd-mm-yyyy"),
|
|
|
|
new Date(2019, 1, 3, 16, 34)
|
|
|
|
);
|
2019-03-06 16:39:50 -05:00
|
|
|
assertEq(
|
2019-01-16 10:25:52 -05:00
|
|
|
datetime.parseDateTime("16:34 2019-01-03", "hh:mm yyyy-mm-dd"),
|
|
|
|
new Date(2019, 1, 3, 16, 34)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test(function invalidParseDateTimeFormatThrows() {
|
|
|
|
try {
|
2019-03-04 19:53:35 -05:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2019-01-16 10:25:52 -05:00
|
|
|
(datetime as any).parseDateTime("2019-01-01 00:00", "x-y-z");
|
2019-01-21 10:35:07 -05:00
|
|
|
assert(false, "no exception was thrown");
|
2019-01-16 10:25:52 -05:00
|
|
|
} catch (e) {
|
2019-03-06 16:39:50 -05:00
|
|
|
assertEq(e.message, "Invalid datetime format!");
|
2019-01-16 10:25:52 -05:00
|
|
|
}
|
2019-01-03 10:19:20 -05:00
|
|
|
});
|
2019-01-16 10:25:52 -05:00
|
|
|
|
2019-01-03 10:19:20 -05:00
|
|
|
test(function parseDate() {
|
2019-03-06 16:39:50 -05:00
|
|
|
assertEq(
|
2019-01-11 00:19:42 -05:00
|
|
|
datetime.parseDate("01-03-2019", "mm-dd-yyyy"),
|
2019-01-03 10:19:20 -05:00
|
|
|
new Date(2019, 1, 3)
|
|
|
|
);
|
2019-03-06 16:39:50 -05:00
|
|
|
assertEq(
|
2019-01-16 10:25:52 -05:00
|
|
|
datetime.parseDate("03-01-2019", "dd-mm-yyyy"),
|
|
|
|
new Date(2019, 1, 3)
|
|
|
|
);
|
2019-03-06 16:39:50 -05:00
|
|
|
assertEq(
|
2019-01-16 10:25:52 -05:00
|
|
|
datetime.parseDate("2019-01-03", "yyyy-mm-dd"),
|
|
|
|
new Date(2019, 1, 3)
|
|
|
|
);
|
2019-01-03 10:19:20 -05:00
|
|
|
});
|
2019-01-16 10:25:52 -05:00
|
|
|
|
|
|
|
test(function invalidParseDateFormatThrows() {
|
|
|
|
try {
|
2019-03-04 19:53:35 -05:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2019-01-16 10:25:52 -05:00
|
|
|
(datetime as any).parseDate("2019-01-01", "x-y-z");
|
2019-01-21 10:35:07 -05:00
|
|
|
assert(false, "no exception was thrown");
|
2019-01-16 10:25:52 -05:00
|
|
|
} catch (e) {
|
2019-03-06 16:39:50 -05:00
|
|
|
assertEq(e.message, "Invalid date format!");
|
2019-01-16 10:25:52 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-01-03 10:19:20 -05:00
|
|
|
test(function currentDayOfYear() {
|
2019-03-06 16:39:50 -05:00
|
|
|
assertEq(
|
2019-01-03 10:19:20 -05:00
|
|
|
datetime.currentDayOfYear(),
|
|
|
|
Math.ceil(new Date().getTime() / 86400000) -
|
|
|
|
Math.floor(
|
|
|
|
new Date().setFullYear(new Date().getFullYear(), 0, 1) / 86400000
|
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|