-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·66 lines (54 loc) · 1.89 KB
/
index.js
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#! /usr/bin/env node
const { program } = require("commander");
const chalk = require("chalk");
const {
getTimeZoneDifferenceWithGMT,
isTimestampFormatInMilliSeconds,
} = require("./core/util");
program
.command("dt <timestamp>")
.description("Convert an timestamp to human-readable datetime")
.action((timestamp) => {
const isMilliSeconds = isTimestampFormatInMilliSeconds(timestamp);
const ts = isMilliSeconds ? Number(timestamp) : Number(timestamp) * 1000;
const date = new Date(ts);
const formattedOptions = {
weekday: "short",
day: "2-digit",
month: "short",
year: "numeric",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: false, // Use 24-hour format
};
if (isMilliSeconds)
console.log(chalk.bold.red("\nAssuming the timestamp in milliseconds"));
console.log(chalk.bold.greenBright("\nLocal Time:"));
console.log(
chalk.bold.greenBright(
` - ${date.toLocaleString("en-US", formattedOptions)} ${getTimeZoneDifferenceWithGMT()}\n`,
),
);
console.log(chalk.bold.greenBright("GMT Time:"));
console.log(chalk.bold.greenBright(` - ${date.toGMTString()}\n`));
});
program
.command("ts <datetime>")
.description(
`Convert human-readable datetime (Default Local Timezone) to timestamp:
\t Example Format(<datetime>)
\t\tLocal Time:\t${chalk.greenBright.bold('"Jan 1, 1970 22:30:00"')}
\t\tGMT Time:\t${chalk.greenBright.bold('"Jan 1, 1970 22:30:00 GMT"')}
`,
)
.action((datetime) => {
const date = new Date(datetime);
const localTimestamp = date.getTime();
const formattedText = `
${chalk.bold.greenBright(" - Epoch timestamp:")} ${chalk.yellowBright(localTimestamp / 1000)}
${chalk.bold.greenBright(" - Timestamp in milliseconds:")} ${chalk.yellowBright(localTimestamp)}
`;
console.log(formattedText);
});
program.parse(process.argv);