Repository navigation
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·281 lines (242 loc) · 8.27 KB
/
Copy pathcli.js
File metadata and controls
executable file
·281 lines (242 loc) · 8.27 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/env node
const { formatInTimeZone, getTimezoneOffset } = require('date-fns-tz');
const { parseISO, isValid } = require('date-fns');
const FORMAT = 'yyyy-MM-dd hh:mm a';
const TIME_ZONES = {
UTC: { offset: '', tz: 'UTC' },
PST: { offset: '(-8:00)', tz: 'Etc/GMT+8' },
'PDT/MST': { offset: '(-7:00)', tz: 'Etc/GMT+7' },
'CST/MDT': { offset: '(-6:00)', tz: 'Etc/GMT+6' },
'CDT/EST': { offset: '(-5:00)', tz: 'Etc/GMT+5' },
EDT: { offset: '(-4:00)', tz: 'Etc/GMT+4' },
};
const CITIES = {
'Los Angeles': 'America/Los_Angeles',
'New York': 'America/New_York',
Phoenix: 'America/Phoenix',
Chicago: 'America/Chicago',
Denver: 'America/Denver',
};
/**
* Get timezone abbreviation using Intl API
* @param {string} timeZone - IANA timezone identifier
* @param {Date} date - Date to get abbreviation for
* @returns {string} Timezone abbreviation
*/
function getTimezoneAbbreviation(timeZone, date) {
const formatter = new Intl.DateTimeFormat('en', {
timeZone,
timeZoneName: 'short',
});
return formatter.formatToParts(date).find(part => part.type === 'timeZoneName')?.value || timeZone;
}
/**
* Display help information
*/
function showHelp() {
console.log(`
timezones-cli - Convert dates and timestamps to different timezones
USAGE:
timezones-cli [date|timestamp]
timezones-cli --help
ARGUMENTS:
date|timestamp Optional. Date string or Unix timestamp to convert.
If not provided, uses current date/time.
OPTIONS:
--help, -h Show this help message
EXAMPLES:
timezones-cli # Use current date/time
timezones-cli "2024-01-15 10:30" # Convert specific date
timezones-cli "2024-01-15T10:30:00Z" # ISO format
timezones-cli 1705320600000 # Unix timestamp (milliseconds)
timezones-cli --help # Show this help
OUTPUT:
Displays the input date/time converted to various timezones:
- UTC, PST, PDT/MST, CST/MDT, CDT/EST, EDT
- Shows matching major cities for each timezone
- Marks your local timezone with (LOCAL) when it matches
- Adds a separate LOCAL row if your timezone doesn't match any listed
`);
}
/**
* Parse input date string or timestamp
* @param {string} input - Date string or timestamp
* @returns {Date} Parsed date object
*/
function parseInputDate(input) {
if (!input) {
// Allow overriding current time for testing
if (process.env.TEST_CURRENT_TIME) {
return new Date(process.env.TEST_CURRENT_TIME);
}
return new Date();
}
// Check if it's a timestamp (number)
const timestamp = Number(input);
if (!isNaN(timestamp)) {
return new Date(timestamp);
}
// Try to parse as ISO string
const parsedDate = parseISO(input);
if (isValid(parsedDate)) {
return parsedDate;
}
// Fallback to Date constructor
const fallbackDate = new Date(input);
if (isValid(fallbackDate)) {
return fallbackDate;
}
throw new Error(`Invalid date format: ${input}`);
}
/**
* Format date in specified timezone
* @param {Date} date - Date to format
* @param {string} timeZone - IANA timezone identifier
* @returns {string} Formatted date string
*/
function formatDateInTimezone(date, timeZone) {
return formatInTimeZone(date, timeZone, FORMAT);
}
/**
* Generate city dates for comparison
* @param {Date} date - Date to convert
* @returns {Object} Object mapping city names to formatted dates
*/
function generateCityDates(date) {
return Object.entries(CITIES).reduce((memo, [city, tz]) => {
memo[city] = formatDateInTimezone(date, tz);
return memo;
}, {});
}
/**
* Create a timezone entry object
* @param {string} timezone - Timezone name/abbreviation
* @param {string} offset - Timezone offset string
* @param {Date} date - Date to format
* @param {string} tz - IANA timezone identifier
* @param {Object} cityDates - Pre-computed city dates
* @param {string} localFormattedDate - Local timezone formatted date
* @returns {Object} Timezone entry object
*/
function createTimezoneEntry(timezone, offset, date, tz, cityDates, localFormattedDate) {
const formattedDate = formatDateInTimezone(date, tz);
const matchingCities = Object.entries(cityDates)
.filter(([, cityDate]) => cityDate === formattedDate)
.map(([city]) => city);
const isLocalMatch = localFormattedDate === formattedDate;
return {
timezone,
offset,
formattedDate,
cities: matchingCities,
isLocal: isLocalMatch,
};
}
/**
* Create local timezone entry when it doesn't match predefined timezones
* @param {Date} date - Date to convert
* @param {string} localTimeZone - Local timezone identifier
* @param {string} localFormattedDate - Local timezone formatted date
* @returns {Object} Local timezone entry object
*/
function createLocalTimezoneEntry(date, localTimeZone, localFormattedDate) {
// Calculate timezone offset
const offsetMs = getTimezoneOffset(localTimeZone, date);
const offsetMinutes = offsetMs / (1000 * 60);
const offsetHours = Math.floor(Math.abs(offsetMinutes) / 60);
const offsetMins = Math.abs(offsetMinutes) % 60;
const offsetSign = offsetMinutes >= 0 ? '+' : '-';
const offsetString = `(${offsetSign}${offsetHours}:${offsetMins.toString().padStart(2, '0')})`;
// Get timezone abbreviation and city name
const timezoneAbbr = getTimezoneAbbreviation(localTimeZone, date);
const cityName = localTimeZone.substring(localTimeZone.lastIndexOf('/') + 1);
return {
timezone: timezoneAbbr,
offset: offsetString,
formattedDate: localFormattedDate,
cities: [cityName],
isLocal: true,
};
}
/**
* Build timezone entries array
* @param {Date} date - Date to convert
* @returns {Array} Array of timezone entry objects
*
* @example
* // Returns an array like:
* [
* { timezone: 'UTC', offset: '', formattedDate: '2024-01-15 10:30 AM', cities: [], isLocal: false },
* { timezone: 'PST', offset: '(-8:00)', formattedDate: '2024-01-15 02:30 AM', cities: ['Los Angeles'], isLocal: false },
* { timezone: 'GMT+2', offset: '(+2:00)', formattedDate: '2024-01-15 12:30 PM', cities: ['Berlin'], isLocal: true }
* ]
*/
function buildTimezoneEntries(date) {
const localTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
const localFormattedDate = formatDateInTimezone(date, localTimeZone);
const cityDates = generateCityDates(date);
const entries = [];
let localTimezoneMatched = false;
// Build predefined timezone entries
Object.entries(TIME_ZONES).forEach(([timezone, { offset, tz }]) => {
const entry = createTimezoneEntry(timezone, offset, date, tz, cityDates, localFormattedDate);
if (entry.isLocal) {
localTimezoneMatched = true;
}
entries.push(entry);
});
// Add local timezone entry if it doesn't match any predefined timezone
if (!localTimezoneMatched) {
const localEntry = createLocalTimezoneEntry(date, localTimeZone, localFormattedDate);
entries.push(localEntry);
}
// Sort entries by datetime (ascending order)
entries.sort((a, b) => {
// Parse the formatted dates to compare them
const dateA = new Date(a.formattedDate);
const dateB = new Date(b.formattedDate);
return dateA.getTime() - dateB.getTime();
});
return entries;
}
/**
* Print timezone entries to console
* @param {Array} entries - Array of timezone entry objects
*/
function printTimezoneEntries(entries) {
entries.forEach(({ timezone, offset, formattedDate, cities, isLocal }) => {
// Ensure consistent column alignment by padding timezone to 8 characters
const paddedTimezone = timezone.padEnd(8);
// Pad offset to consistent width (8 characters) for proper alignment
const paddedOffset = offset.padEnd(8);
let message = `${paddedTimezone} ${paddedOffset} ${formattedDate}`;
if (cities.length > 0) {
message += ` ${cities.join(', ')}`;
}
if (isLocal) {
message += ' (LOCAL)';
}
console.log(message);
});
}
function main() {
try {
const [, , ...args] = process.argv;
const firstArg = args[0];
// Check for help flag
if (firstArg === '--help' || firstArg === '-h') {
showHelp();
return;
}
const date = parseInputDate(firstArg);
const entries = buildTimezoneEntries(date);
printTimezoneEntries(entries);
} catch (error) {
console.error(`Error: ${error.message}\n`);
showHelp();
process.exit(1);
}
}
if (require.main === module) {
main();
}