Skip to content

Commit 5b7fda0

Browse files
committed
refactor: enhance hackathon validation and data handling
- Updated hackathon validation logic to improve readability and maintainability. - Changed date fields in HackathonHeader type from Date to string for better compatibility with API responses. - Improved error handling in create and update hackathon functions to ensure proper validation and timezone handling. - Refactored getFilteredHackathons function to streamline filtering and pagination logic.
1 parent efd1bc6 commit 5b7fda0

File tree

3 files changed

+353
-224
lines changed

3 files changed

+353
-224
lines changed

server/services/date-parser.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// All comments in English (as requested).
2+
export function getDateWithTimezone(dateStr: string, timeZone: string): Date {
3+
const s: string = dateStr.trim();
4+
const endsWithZ: boolean = /[zZ]$/.test(s);
5+
6+
// Case 1: ends with 'Z' (UTC instant) → project to target TZ wall time
7+
if (endsWithZ) {
8+
const utcDate: Date = new Date(s);
9+
if (isNaN(utcDate.getTime())) throw new Error(`Invalid date string: "${dateStr}"`);
10+
11+
// Extract wall-clock parts in the target time zone
12+
const fmt: Intl.DateTimeFormat = new Intl.DateTimeFormat('en-US', {
13+
timeZone,
14+
year: 'numeric',
15+
month: '2-digit',
16+
day: '2-digit',
17+
hour: '2-digit',
18+
minute: '2-digit',
19+
second: '2-digit',
20+
hour12: false,
21+
});
22+
const parts: Intl.DateTimeFormatPart[] = fmt.formatToParts(utcDate);
23+
const map: Record<string, string> = Object.fromEntries(parts.map(p => [p.type, p.value]));
24+
25+
const year: number = Number(map.year);
26+
const month: number = Number(map.month);
27+
const day: number = Number(map.day);
28+
const hour: number = Number(map.hour ?? '0');
29+
const minute: number = Number(map.minute ?? '0');
30+
const second: number = Number(map.second ?? '0');
31+
const ms: number = utcDate.getUTCMilliseconds();
32+
33+
// Build a Date using those wall-clock numbers (this "freezes" the TZ wall time).
34+
return new Date(year, month - 1, day, hour, minute, second, ms);
35+
}
36+
37+
// Case 2: any other format → ignore timeZone and parse as-is (preserve the instant)
38+
const d: Date = new Date(s);
39+
if (isNaN(d.getTime())) throw new Error(`Invalid date string: "${dateStr}"`);
40+
return d;
41+
}
42+

0 commit comments

Comments
 (0)