-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
timezones.ts
48 lines (36 loc) 路 1.14 KB
/
timezones.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import Fuse from "https://esm.sh/fuse.js@6.6.2/";
import { Command } from "https://deno.land/x/cliffy@v0.25.7/command/mod.ts";
import {
bgBlue,
bgGreen,
black,
} from "https://deno.land/std@0.175.0/fmt/colors.ts";
import { randomTimeZone, type TimeZone, timeZones } from "./mod.ts";
const list = new Command()
.description("List all time zones")
.action(() => timeZones.forEach(printTime));
const random = new Command()
.description("Random time zone")
.action(() => printTime(randomTimeZone()));
await new Command()
.name("timezones")
.action(main)
.version("1.2.2")
.description("Get the time in a time zone.")
.command("list", list)
.command("random", random)
.parse(Deno.args);
function main() {
const fuse = new Fuse(timeZones);
const input = prompt(bgBlue("Enter a time zone: "));
if (!input) {
return console.log("Input is required.");
}
const [{ item }] = fuse.search(input);
printTime(item);
}
function printTime(timeZone: TimeZone) {
const date = new Date();
const timeInThere = date.toLocaleString("en-US", { timeZone });
console.log(bgGreen(black(`In ${timeZone} it is ${timeInThere}.`)));
}