ndfdTime.ts
Purpose
This module computes the inclusive local-time begin/end window sent to the NDFD XML API. NDFD requires local (non-UTC) datetime strings, and the window must be aligned to the anchor hour to produce consistent cache-compatible results.
Its responsibilities include:
- Validating the
hoursargument as a positive integer - Validating the
anchordate input - Flooring the anchor to the nearest local hour boundary
- Computing the end time as
begin + (hours − 1)hours - Formatting both bounds as NDFD-compatible local datetime strings without timezone offset
Invariants
Hour Alignment
The begin time is always the anchor date floored to the nearest local hour (setMinutes(0, 0, 0)).
This guarantees that two calls with the same hour produce the same window regardless of the minute or second within that hour.
Inclusive End Bound
NDFD’s end parameter is inclusive. The end time is computed as:
end = begin + (hours − 1) × 3,600,000 ms
A request for hours = 24 produces a window spanning hour 0 through hour 23 inclusive (24 entries).
NDFD Local Format
Output strings use the format YYYY-MM-DDTHH:MM:SS with no timezone indicator, matching the NDFD API’s expected local-time format.
Input Validation
hoursmust be a finite positive integer; otherwise throwsError("ndfdLocalWindow: hours must be a positive integer")anchormust parse to a finite timestamp; otherwise throwsError("ndfdLocalWindow: anchor time is invalid")
Exports
NdfdTimeWindow
type NdfdTimeWindow = {
begin: string;
end: string;
beginDate: Date;
endDate: Date;
};
| Field | Description |
|---|---|
begin | NDFD local-format start string |
end | NDFD local-format end string (inclusive) |
beginDate | Date object for the floored anchor hour |
endDate | Date object for the computed end hour |
ndfdLocalWindow(...)
function ndfdLocalWindow(
hours: number,
anchor?: Date | string,
): NdfdTimeWindow
Builds an inclusive local-time NDFD request window aligned to the anchor hour.
Parameters
| Parameter | Description |
|---|---|
hours | Number of forecast hours to request (must be a positive integer) |
anchor | Window start reference time (default: new Date()) |
Throws
Throws when hours is not a positive integer or anchor is not a valid date.