Files
TREK/client/src/utils/localDate.ts
T
Maurice 60e8f1cd34 refactor(roadtrip): read the wall clock minutes and today through localDate
useMRoadtrip and useMRtCorridor each kept a nowMinutes helper and their
own check whether the selected day is today. Both now use localMinutes
and isLocalToday from utils/localDate.ts, which hold the same rules.
2026-10-09 16:15:49 +02:00

30 lines
1.3 KiB
TypeScript

/**
* The user's LOCAL calendar date as 'YYYY-MM-DD'.
*
* Never derive "today" from `toISOString().split('T')[0]` — that is the UTC
* date, which disagrees with the wall clock between local midnight and the UTC
* rollover (for a UTC+2 user, from 00:00 to 02:00 every night). Trip and
* journey dates are wall-clock dates, so every "is this today/past/upcoming"
* comparison must use this instead.
*
* (Converting a Date you deliberately built in UTC — `new Date(s + 'T00:00:00Z')`
* round-tripped through date arithmetic — is a different, self-consistent
* pattern and fine as it is.)
*/
export function localIsoDate(d: Date = new Date()): string {
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`
}
/** Minutes since local midnight: the wall clock a day's schedule is read against. */
export function localMinutes(d: Date = new Date()): number {
return d.getHours() * 60 + d.getMinutes()
}
/**
* Whether a trip day's date (anything starting 'YYYY-MM-DD') is today on the wall
* clock. Local for the reason `localIsoDate` gives; a day without a date is never today.
*/
export function isLocalToday(date: string | null | undefined): boolean {
return !!date && date.slice(0, 10) === localIsoDate()
}