-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathts.js
executable file
·89 lines (76 loc) · 2.98 KB
/
ts.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#! /usr/local/bin/node
let ts = process.argv[2];
if (process.argv[3]) {
ts = process.argv.slice(2).join(' ');
}
const isUnixTimestamp = input => /^\d{10,19}$/.test(input);
const formatUnixTimestamp = timestamp => {
const msTimestamp = timestamp.padEnd(13, '0').slice(0, 13); // Pad to milliseconds if needed
const date = new Date(Number(msTimestamp));
return {
localTime: date.toLocaleString('en-US', {
timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone,
year: 'numeric',
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
}),
utcISO: date.toISOString(),
};
};
const getTimeZoneOffset = (timeZone, date = new Date()) => {
const localTime = date.getTime();
const tzDate = new Date(date.toLocaleString('en-US', { timeZone }));
return (localTime - tzDate.getTime()) / 60000; // Offset in minutes
};
const parseDateString = input => {
const dateRegex = /^(\d{4})-(\d{2})-(\d{2}) (\d{1,2}):(\d{2})(am|pm)$/i;
const match = input.match(dateRegex);
if (!match) {
throw new Error('Invalid date string. Use "yyyy-MM-dd h:mma".');
}
let [_, year, month, day, hour, minute, period] = match;
year = Number(year);
month = Number(month);
day = Number(day);
hour = Number(hour);
minute = Number(minute);
// Adjust for AM/PM
if (period.toLowerCase() === 'pm' && hour !== 12) {
hour += 12;
} else if (period.toLowerCase() === 'am' && hour === 12) {
hour = 0;
}
// Create a Date object assuming America/Los_Angeles time zone
const localDateString = `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}T${hour.toString().padStart(2, '0')}:${minute.toString().padStart(2, '0')}:00`;
const localDate = new Date(localDateString + '-08:00'); // Hardcode PST offset (-08:00)
if (isNaN(localDate.getTime())) {
throw new Error('Invalid date. Please check the day, month, and year.');
}
return {
unixSeconds: Math.floor(localDate.getTime() / 1000),
unixMilliseconds: localDate.getTime(),
utcISO: localDate.toISOString(),
};
};
const parseInput = input => {
if (isUnixTimestamp(input)) {
const { localTime, utcISO } = formatUnixTimestamp(input);
return `Time in your timezone: ${localTime}\nUTC ISO timestamp: ${utcISO}`;
}
const { unixSeconds, unixMilliseconds, utcISO } = parseDateString(input);
return `Unix time (seconds): ${unixSeconds}\nUnix time (milliseconds): ${unixMilliseconds}\nUTC ISO timestamp: ${utcISO}`;
};
try {
if (!ts) {
console.log('Usage: ts <unix timestamp | "yyyy-MM-dd h:mma">');
const now = Date.now();
console.log(`Current time in seconds: ${Math.floor(now / 1000)}\nCurrent time in milliseconds: ${now}`);
} else {
console.log(parseInput(ts));
}
} catch (error) {
console.error(`Error: ${error.message}`);
}