-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdatetime.ts
More file actions
60 lines (56 loc) · 2.15 KB
/
Copy pathdatetime.ts
File metadata and controls
60 lines (56 loc) · 2.15 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
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
/**
* Timezone-aware calendar utilities (ADR-0053 Phase 2).
*
* The one primitive everything else builds on is {@link calendarPartsInTz}:
* the year/month/day an instant falls on *as seen in a reference timezone*.
* It uses `Intl.DateTimeFormat().formatToParts()` so DST transitions are
* handled by the platform's tz database — never hand-rolled offset math, which
* is the classic source of off-by-one-hour bucket errors.
*
* This lives in `@objectstack/core` (not `@objectstack/formula`) because both
* the ObjectQL aggregation engine and the analytics service need it and both
* already depend on core, whereas neither depends on formula's public surface.
* (`@objectstack/formula` keeps its own private copy for `today()`/`daysFromNow`
* to avoid a layering dependency on core.)
*/
/** Calendar-day parts in a reference timezone. `month` is 1-12. */
export interface CalendarParts {
year: number;
month: number;
day: number;
}
/**
* The year/month/day an instant falls on in `tz`. Throws if `tz` is not a
* valid IANA zone (callers treat that as a fall-through to UTC).
*/
export function calendarPartsInTz(d: Date, tz: string): CalendarParts {
const parts = new Intl.DateTimeFormat('en-US', {
timeZone: tz,
year: 'numeric',
month: '2-digit',
day: '2-digit',
}).formatToParts(d);
const get = (t: string) => Number(parts.find((p) => p.type === t)?.value);
return { year: get('year'), month: get('month'), day: get('day') };
}
/**
* The calendar-day parts of an instant, in `tz` when it's a real non-UTC zone,
* otherwise in UTC. Never throws: an unset, `'UTC'`, or invalid zone falls back
* to the UTC calendar day. This is the safe entry point for bucketing code that
* must degrade to the historical UTC behavior rather than error.
*/
export function calendarPartsInTzOrUtc(d: Date, tz?: string): CalendarParts {
if (tz && tz !== 'UTC') {
try {
return calendarPartsInTz(d, tz);
} catch {
// unknown zone → fall through to UTC
}
}
return {
year: d.getUTCFullYear(),
month: d.getUTCMonth() + 1,
day: d.getUTCDate(),
};
}