forked from JesperDramsch/python-deadlines
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimezone-utils.js
More file actions
291 lines (251 loc) · 9.35 KB
/
Copy pathtimezone-utils.js
File metadata and controls
291 lines (251 loc) · 9.35 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
282
283
284
285
286
287
288
289
290
291
/**
* Timezone Utilities for Python Deadlines
* Provides consistent timezone handling across the site
*/
(function(window) {
'use strict';
// Default timezone for "Anywhere on Earth"
const AOE_TIMEZONE = 'UTC-12';
// Common timezone aliases that need mapping
const TIMEZONE_ALIASES = {
'AoE': AOE_TIMEZONE,
'AOE': AOE_TIMEZONE,
'Anywhere on Earth': AOE_TIMEZONE,
'EST': 'America/New_York',
'EDT': 'America/New_York',
'CST': 'America/Chicago',
'CDT': 'America/Chicago',
'MST': 'America/Denver',
'MDT': 'America/Denver',
'PST': 'America/Los_Angeles',
'PDT': 'America/Los_Angeles',
'BST': 'Europe/London',
'CET': 'Europe/Paris',
'CEST': 'Europe/Paris',
'JST': 'Asia/Tokyo',
'IST': 'Asia/Kolkata',
'AEST': 'Australia/Sydney',
'AEDT': 'Australia/Sydney'
};
/**
* Normalize timezone string to IANA format
* @param {string} timezone - Input timezone string
* @returns {string} Normalized timezone
*/
function normalizeTimezone(timezone) {
if (!timezone) {
return AOE_TIMEZONE;
}
// Check if it's an alias
if (TIMEZONE_ALIASES[timezone]) {
return TIMEZONE_ALIASES[timezone];
}
// Return as-is if it looks like a valid IANA timezone
return timezone;
}
/**
* Parse date with timezone handling and fallback
* @param {string} dateStr - Date string to parse
* @param {string} timezone - Timezone string
* @returns {Object} Object with date and warning info
*/
function parseConferenceDate(dateStr, timezone) {
const result = {
date: null,
jsDate: null,
timezone: timezone,
normalizedTimezone: null,
warning: null,
isValid: false
};
// Handle special cases
if (!dateStr || dateStr === 'TBA' || dateStr === 'Cancelled' || dateStr === 'None') {
result.warning = dateStr || 'TBA';
return result;
}
// Normalize timezone
const normalizedTz = normalizeTimezone(timezone);
result.normalizedTimezone = normalizedTz;
try {
if (typeof luxon !== 'undefined') {
const DateTime = luxon.DateTime;
// Try parsing with provided timezone
let dt = DateTime.fromSQL(dateStr, { zone: normalizedTz });
// If invalid, try without timezone
if (dt.invalid) {
dt = DateTime.fromSQL(dateStr);
if (!dt.invalid) {
result.warning = `Invalid timezone "${timezone}", using system timezone`;
console.warn(`Invalid timezone for date ${dateStr}: ${timezone}. Using system timezone.`);
}
}
// If still invalid, try ISO format
if (dt.invalid) {
dt = DateTime.fromISO(dateStr, { zone: normalizedTz });
}
// If still invalid, try JS Date parsing as last resort
if (dt.invalid) {
const jsDate = new Date(dateStr);
if (!isNaN(jsDate)) {
dt = DateTime.fromJSDate(jsDate);
result.warning = `Parsed using fallback method`;
}
}
if (!dt.invalid) {
result.date = dt;
result.jsDate = dt.toJSDate();
result.isValid = true;
} else {
result.warning = `Could not parse date: ${dateStr}`;
console.error(`Failed to parse date: ${dateStr} with timezone: ${timezone}`);
}
} else {
// Fallback when Luxon is not available
const jsDate = new Date(dateStr);
if (!isNaN(jsDate)) {
result.jsDate = jsDate;
result.isValid = true;
result.warning = 'Using basic Date parsing (Luxon not available)';
} else {
result.warning = `Invalid date: ${dateStr}`;
}
}
} catch (error) {
result.warning = `Error parsing date: ${error.message}`;
console.error('Date parsing error:', error);
}
return result;
}
/**
* Format date for display with proper timezone
* @param {Object} dateInfo - Result from parseConferenceDate
* @param {string} format - Display format (short, long, huge)
* @returns {string} Formatted date string
*/
function formatConferenceDate(dateInfo, format = 'long') {
if (!dateInfo.isValid) {
return dateInfo.warning || 'Invalid date';
}
try {
if (dateInfo.date && typeof luxon !== 'undefined') {
const DateTime = luxon.DateTime;
switch(format) {
case 'short':
return dateInfo.date.toLocaleString(DateTime.DATE_SHORT);
case 'long':
return dateInfo.date.toLocaleString(DateTime.DATETIME_FULL);
case 'huge':
return dateInfo.date.toLocaleString(DateTime.DATETIME_HUGE_WITH_SECONDS);
case 'relative':
return dateInfo.date.toRelative();
default:
return dateInfo.date.toLocaleString(DateTime.DATETIME_FULL);
}
} else if (dateInfo.jsDate) {
// Fallback formatting
return dateInfo.jsDate.toLocaleString();
}
} catch (error) {
console.error('Date formatting error:', error);
}
return dateInfo.warning || 'Format error';
}
/**
* Get timezone display string with location
* @param {string} timezone - Timezone identifier
* @returns {string} Display string like "EST (New York)"
*/
function getTimezoneDisplay(timezone) {
const normalized = normalizeTimezone(timezone);
if (typeof luxon !== 'undefined') {
try {
const DateTime = luxon.DateTime;
const dt = DateTime.now().setZone(normalized);
if (!dt.invalid) {
const offsetStr = dt.toFormat('ZZZ');
const abbr = dt.toFormat('ZZZZ');
return `${abbr} (${offsetStr})`;
}
} catch (error) {
console.error('Timezone display error:', error);
}
}
return timezone || AOE_TIMEZONE;
}
/**
* Calculate time remaining until deadline
* @param {Object} dateInfo - Result from parseConferenceDate
* @returns {Object} Time remaining breakdown
*/
function getTimeRemaining(dateInfo) {
if (!dateInfo.isValid) {
return { expired: true, message: dateInfo.warning };
}
const now = new Date();
const deadline = dateInfo.jsDate;
const diff = deadline - now;
if (diff <= 0) {
return { expired: true, message: 'Deadline passed' };
}
const days = Math.floor(diff / (1000 * 60 * 60 * 24));
const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((diff % (1000 * 60)) / 1000);
return {
expired: false,
days: days,
hours: hours,
minutes: minutes,
seconds: seconds,
totalMs: diff,
display: `${days}d ${hours}h ${minutes}m ${seconds}s`
};
}
/**
* Validate if a timezone string is valid
* @param {string} timezone - Timezone to validate
* @returns {boolean} True if valid
*/
function isValidTimezone(timezone) {
if (!timezone) return false;
const normalized = normalizeTimezone(timezone);
if (typeof luxon !== 'undefined') {
try {
const DateTime = luxon.DateTime;
const dt = DateTime.now().setZone(normalized);
return !dt.invalid;
} catch (error) {
return false;
}
}
// Basic validation without Luxon
return /^[A-Za-z]+\/[A-Za-z_]+$/.test(normalized) || normalized === AOE_TIMEZONE;
}
/**
* Get user's local timezone
* @returns {string} IANA timezone string
*/
function getUserTimezone() {
if (typeof luxon !== 'undefined') {
return luxon.DateTime.now().zoneName;
}
// Fallback method
try {
return Intl.DateTimeFormat().resolvedOptions().timeZone;
} catch (error) {
return 'UTC';
}
}
// Export utilities
window.TimezoneUtils = {
normalizeTimezone: normalizeTimezone,
parseConferenceDate: parseConferenceDate,
formatConferenceDate: formatConferenceDate,
getTimezoneDisplay: getTimezoneDisplay,
getTimeRemaining: getTimeRemaining,
isValidTimezone: isValidTimezone,
getUserTimezone: getUserTimezone,
AOE_TIMEZONE: AOE_TIMEZONE,
TIMEZONE_ALIASES: TIMEZONE_ALIASES
};
})(window);