Spaces:
Running
Running
Refactor date utilities
Browse files- src/utils/dateUtils.ts +24 -80
src/utils/dateUtils.ts
CHANGED
@@ -3,124 +3,68 @@ import { zonedTimeToUtc, utcToZonedTime } from 'date-fns-tz';
|
|
3 |
|
4 |
export const getDeadlineInLocalTime = (deadline: string | undefined, timezone: string | undefined): Date | null => {
|
5 |
if (!deadline || deadline === 'TBD') {
|
6 |
-
console.log('Early return - deadline is null or TBD:', { deadline, timezone });
|
7 |
return null;
|
8 |
}
|
9 |
|
10 |
try {
|
11 |
-
console.log('Processing conference deadline:', {
|
12 |
-
conferenceName: 'Unknown', // We could pass this as a parameter if helpful
|
13 |
-
deadline,
|
14 |
-
timezone,
|
15 |
-
deadlineType: typeof deadline,
|
16 |
-
timezoneType: typeof timezone
|
17 |
-
});
|
18 |
-
|
19 |
// Parse the deadline string to a Date object
|
20 |
-
const
|
21 |
-
|
22 |
-
|
23 |
-
parsed: deadlineDate,
|
24 |
-
isValid: isValid(deadlineDate),
|
25 |
-
timestamp: deadlineDate.getTime(),
|
26 |
-
toISOString: deadlineDate.toISOString()
|
27 |
-
});
|
28 |
-
|
29 |
-
if (!isValid(deadlineDate)) {
|
30 |
console.error('Invalid date parsed from deadline:', deadline);
|
31 |
return null;
|
32 |
}
|
33 |
|
34 |
-
// Handle
|
35 |
-
if (timezone === 'AoE') {
|
36 |
-
console.log('Converting AoE to UTC-12');
|
37 |
-
return new Date(deadlineDate.getTime() - 12 * 60 * 60 * 1000);
|
38 |
-
}
|
39 |
-
|
40 |
-
// Handle UTC offset timezones (e.g., "UTC-12", "UTC+01", "UTC+0")
|
41 |
const normalizeTimezone = (tz: string | undefined): string => {
|
42 |
-
if (!tz)
|
43 |
-
console.log('No timezone provided, using UTC');
|
44 |
-
return 'UTC';
|
45 |
-
}
|
46 |
|
47 |
-
|
|
|
48 |
|
49 |
// If it's already an IANA timezone, return as is
|
50 |
-
if (!tz.toUpperCase().startsWith('UTC'))
|
51 |
-
console.log('Using IANA timezone:', tz);
|
52 |
-
return tz;
|
53 |
-
}
|
54 |
|
55 |
// Convert UTC±XX to proper format
|
56 |
const match = tz.match(/^UTC([+-])(\d+)$/);
|
57 |
if (match) {
|
58 |
const [, sign, hours] = match;
|
59 |
const paddedHours = hours.padStart(2, '0');
|
60 |
-
|
61 |
-
console.log('Normalized UTC offset:', { original: tz, normalized });
|
62 |
-
return normalized;
|
63 |
}
|
64 |
|
65 |
// Handle special case of UTC+0/UTC-0
|
66 |
if (tz === 'UTC+0' || tz === 'UTC-0' || tz === 'UTC+00' || tz === 'UTC-00') {
|
67 |
-
console.log('Handling UTC+0/-0 case:', tz);
|
68 |
return 'UTC';
|
69 |
}
|
70 |
|
71 |
-
console.log('Falling back to UTC for timezone:', tz);
|
72 |
return 'UTC';
|
73 |
};
|
74 |
|
75 |
const normalizedTimezone = normalizeTimezone(timezone);
|
76 |
-
|
77 |
-
|
78 |
try {
|
79 |
-
// Create date in the conference's timezone
|
80 |
-
const dateInConfTimezone = utcToZonedTime(deadlineDate, normalizedTimezone);
|
81 |
-
console.log('Conference timezone date:', {
|
82 |
-
date: dateInConfTimezone,
|
83 |
-
isValid: isValid(dateInConfTimezone),
|
84 |
-
timezone: normalizedTimezone
|
85 |
-
});
|
86 |
-
|
87 |
// Get user's local timezone
|
88 |
const userTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
89 |
|
90 |
-
//
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
isValid: isValid(localDate),
|
95 |
-
timezone: userTimezone
|
96 |
-
});
|
97 |
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
localDate
|
104 |
-
});
|
105 |
-
return null;
|
106 |
-
}
|
107 |
|
108 |
-
return localDate;
|
109 |
-
} catch (
|
110 |
-
console.error('Timezone conversion error:',
|
111 |
-
|
112 |
-
deadline,
|
113 |
-
timezone,
|
114 |
-
normalizedTimezone
|
115 |
-
});
|
116 |
-
return deadlineDate;
|
117 |
}
|
118 |
} catch (error) {
|
119 |
-
console.error('Error
|
120 |
-
error,
|
121 |
-
deadline,
|
122 |
-
timezone
|
123 |
-
});
|
124 |
return null;
|
125 |
}
|
126 |
};
|
|
|
3 |
|
4 |
export const getDeadlineInLocalTime = (deadline: string | undefined, timezone: string | undefined): Date | null => {
|
5 |
if (!deadline || deadline === 'TBD') {
|
|
|
6 |
return null;
|
7 |
}
|
8 |
|
9 |
try {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
// Parse the deadline string to a Date object
|
11 |
+
const parsedDate = parseISO(deadline);
|
12 |
+
|
13 |
+
if (!isValid(parsedDate)) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
console.error('Invalid date parsed from deadline:', deadline);
|
15 |
return null;
|
16 |
}
|
17 |
|
18 |
+
// Handle timezone normalization
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
const normalizeTimezone = (tz: string | undefined): string => {
|
20 |
+
if (!tz) return 'UTC';
|
|
|
|
|
|
|
21 |
|
22 |
+
// Handle AoE (Anywhere on Earth) timezone
|
23 |
+
if (tz === 'AoE') return '-12:00';
|
24 |
|
25 |
// If it's already an IANA timezone, return as is
|
26 |
+
if (!tz.toUpperCase().startsWith('UTC')) return tz;
|
|
|
|
|
|
|
27 |
|
28 |
// Convert UTC±XX to proper format
|
29 |
const match = tz.match(/^UTC([+-])(\d+)$/);
|
30 |
if (match) {
|
31 |
const [, sign, hours] = match;
|
32 |
const paddedHours = hours.padStart(2, '0');
|
33 |
+
return `${sign}${paddedHours}:00`;
|
|
|
|
|
34 |
}
|
35 |
|
36 |
// Handle special case of UTC+0/UTC-0
|
37 |
if (tz === 'UTC+0' || tz === 'UTC-0' || tz === 'UTC+00' || tz === 'UTC-00') {
|
|
|
38 |
return 'UTC';
|
39 |
}
|
40 |
|
|
|
41 |
return 'UTC';
|
42 |
};
|
43 |
|
44 |
const normalizedTimezone = normalizeTimezone(timezone);
|
45 |
+
|
|
|
46 |
try {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
// Get user's local timezone
|
48 |
const userTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
49 |
|
50 |
+
// We need to:
|
51 |
+
// 1. Treat the parsed date as being in the conference's timezone
|
52 |
+
// 2. Convert it to UTC
|
53 |
+
// 3. Then convert to the user's local timezone
|
|
|
|
|
|
|
54 |
|
55 |
+
// Convert from conference timezone to UTC
|
56 |
+
const utcDate = zonedTimeToUtc(parsedDate, normalizedTimezone);
|
57 |
+
|
58 |
+
// Convert from UTC to user's local timezone
|
59 |
+
const localDate = utcToZonedTime(utcDate, userTimezone);
|
|
|
|
|
|
|
|
|
60 |
|
61 |
+
return isValid(localDate) ? localDate : null;
|
62 |
+
} catch (error) {
|
63 |
+
console.error('Timezone conversion error:', error);
|
64 |
+
return parsedDate; // Fall back to the parsed date if conversion fails
|
|
|
|
|
|
|
|
|
|
|
65 |
}
|
66 |
} catch (error) {
|
67 |
+
console.error('Error processing deadline:', error);
|
|
|
|
|
|
|
|
|
68 |
return null;
|
69 |
}
|
70 |
};
|