Fixes time zone based stuff
This commit is contained in:
23
src/lib/utils.ts
Normal file
23
src/lib/utils.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import type { HuntResponse } from './api/types'
|
||||
|
||||
// Ensures API datetime strings are always parsed as UTC, even if the Z suffix is missing.
|
||||
export function parseUTC(iso: string): Date {
|
||||
const normalized = /Z$|[+-]\d{2}:?\d{2}$/.test(iso) ? iso : iso + 'Z'
|
||||
return new Date(normalized)
|
||||
}
|
||||
|
||||
export function huntStatus(hunt: HuntResponse): 'ONGOING' | 'UNSTARTED' | 'CLOSED' {
|
||||
if (hunt.isTerminated) return 'CLOSED'
|
||||
const now = Date.now()
|
||||
if (now < parseUTC(hunt.startDateTime).getTime()) return 'UNSTARTED'
|
||||
if (now > parseUTC(hunt.endDateTime).getTime()) return 'CLOSED'
|
||||
return 'ONGOING'
|
||||
}
|
||||
|
||||
export function formatDateTime(iso: string): string {
|
||||
return parseUTC(iso).toLocaleDateString(undefined, { month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' })
|
||||
}
|
||||
|
||||
export function formatDate(iso: string): string {
|
||||
return parseUTC(iso).toLocaleDateString(undefined, { month: 'short', day: 'numeric', year: 'numeric' })
|
||||
}
|
||||
Reference in New Issue
Block a user