UNPKG

@northguild/gmt

Version:

Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.

65 lines (63 loc) • 4.32 kB
import { floorDivide } from "../../internal/floorDivide.js"; import { EXCEL_1900_EPOCH_NANOSECONDS, EXCEL_1900_PHANTOM_END_NANOSECONDS, EXCEL_1900_PRE_PHANTOM_EPOCH_NANOSECONDS, EXCEL_1904_EPOCH_NANOSECONDS, MAX_EXCEL_1900_SERIAL_EXCLUSIVE, MAX_EXCEL_1904_SERIAL_EXCLUSIVE, MIN_EXCEL_1900_SERIAL, MIN_EXCEL_1904_SERIAL, NANOSECONDS_PER_DAY, NANOSECONDS_PER_DAY_NUMBER } from "../../internal/foreignEpochs.js"; import { parseInstantNanoseconds } from "../../internal/instantNanoseconds.js"; import { isOptionsArgument } from "../../internal/isObject.js"; import { optionOrDefault } from "../../internal/optionOrDefault.js"; // src/precision/convert/toExcelSerial.ts /** * Convert an ISO 8601 instant string to an Excel day serial. * * - The serial is days, with the time of day as the fraction: 45361.5 is midday on * 2024-03-10. The date is the UTC wall clock — a zoned input is converted to UTC first, * so `"2024-03-10T12:00:00-05:00"` is serial 45361.708…, the 17:00 UTC it names. * - **The 1900 system's serial 60 is the phantom 29 February 1900**, a date that never * existed — 1900 was not a leap year. Lotus 1-2-3 got it wrong and Excel keeps the bug for * compatibility. No instant maps to serial 60: 1900-02-28 is 59 and 1900-03-01 is 61. * Silently "fixing" this would put GMT one day out from Excel for every date before March * 1900. * - **Instants outside Excel's own serial range are invalid input**, not an out-of-range * number: the 1900 system runs from serial 1 (1900-01-01) to 2958465 (9999-12-31), the * 1904 system from serial 0 (1904-01-01) to 2957003. Serial 0 in the 1900 system is * Excel's "January 0, 1900" placeholder, not a date, so it is rejected too. * - The serial is a `double`, as it is in a workbook. The whole-day part is exact; the * intra-day fraction is not, so precision below roughly a microsecond is not * representable, and it degrades toward year 9999. Round-tripping through * `fromExcelSerial` is exact to the millisecond, which is as fine as Excel itself goes. * - **The last ~20 µs of 9999-12-31 returns null**, from * `"9999-12-31T23:59:59.999979884Z"` on. A `double` near serial 2958465 resolves to about * 40 µs, so those instants round to exactly 2958466 — one past Excel's last serial. The * range check is deliberately made on the rounded serial rather than the exact instant, so * a returned number is always one Excel accepts; the alternative is handing back a 2958466 * that Excel rejects on the way in. * * @param isoString ISO 8601 instant string (e.g. "2024-03-10T12:00:00Z") * @param options Which Excel date system the serial is in * @returns Excel day serial as a number, or null on invalid input * * @example toExcelSerial("1900-01-01T00:00:00Z") // 1 — Excel's first serial * @example toExcelSerial("1970-01-01T00:00:00Z") // 25569 * @example toExcelSerial("2024-03-10T12:00:00Z") // 45361.5 * @example toExcelSerial("1900-02-28T00:00:00Z") // 59 — the next day is 61, not 60 * @example toExcelSerial("1900-03-01T00:00:00Z") // 61 — serial 60 is the phantom 1900-02-29 * @example toExcelSerial("2024-03-10T12:00:00Z", { system: "1904" }) // 43899.5 — exactly 1462 lower * @example toExcelSerial("1899-12-31T00:00:00Z") // null — Excel's "January 0", not a date * @example toExcelSerial("9999-12-31T23:59:59.99998Z") // null — rounds to 2958466, one past Excel's last serial * @example toExcelSerial("invalid") // null */ function toExcelSerial(isoString, options) { try { if (!isOptionsArgument(options)) return null; const system = optionOrDefault(options?.system, "1900"); if (system !== "1900" && system !== "1904") return null; const nanoseconds = parseInstantNanoseconds(isoString); if (nanoseconds === null) return null; const sinceEpoch = nanoseconds - (system === "1904" ? EXCEL_1904_EPOCH_NANOSECONDS : nanoseconds >= -2203891200000000000n ? EXCEL_1900_EPOCH_NANOSECONDS : EXCEL_1900_PRE_PHANTOM_EPOCH_NANOSECONDS); const days = floorDivide(sinceEpoch, NANOSECONDS_PER_DAY); const serial = Number(days) + Number(sinceEpoch - days * NANOSECONDS_PER_DAY) / NANOSECONDS_PER_DAY_NUMBER; if (serial < (system === "1904" ? 0 : 1) || serial >= (system === "1904" ? 2957004 : 2958466)) return null; return serial; } catch { return null; } } export { toExcelSerial };