@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.
59 lines (57 loc) • 3.87 kB
JavaScript
import { EXCEL_1900_EPOCH_NANOSECONDS, EXCEL_1900_PRE_PHANTOM_EPOCH_NANOSECONDS, EXCEL_1904_EPOCH_NANOSECONDS, EXCEL_PHANTOM_SERIAL, MAX_EXCEL_1900_SERIAL_EXCLUSIVE, MAX_EXCEL_1904_SERIAL_EXCLUSIVE, MILLISECONDS_PER_DAY, MIN_EXCEL_1900_SERIAL, MIN_EXCEL_1904_SERIAL, NANOSECONDS_PER_MILLISECOND } from "../../internal/foreignEpochs.js";
import { zonedStringFromEpochNanoseconds } from "../../internal/instantStringFromEpochNanoseconds.js";
import { isOptionsArgument } from "../../internal/isObject.js";
import { optionOrDefault } from "../../internal/optionOrDefault.js";
// src/precision/convert/fromExcelSerial.ts
/**
* Convert an Excel day serial back to an ISO 8601 instant string.
*
* - The serial is days with the time of day as the fraction, read as a UTC wall clock.
* - **Serial 60 in the 1900 system returns ""** — it is the phantom 29 February 1900, a date
* that never existed. Lotus 1-2-3 treated 1900 as a leap year and Excel keeps the bug for
* compatibility, so the whole serial day `[60, 61)` is a hole: 59 is 1900-02-28 and 61 is
* 1900-03-01. Mapping 60 to either neighbour would put every earlier date one day out.
* - In the 1904 system serial 60 is a real 1904-03-01.
* - Accepted range is Excel's own: serial 1 (1900-01-01) to just under 2958466 (the end of
* 9999-12-31) in the 1900 system, serial 0 to just under 2957004 in the 1904 system.
* Serial 0 in the 1900 system is Excel's "January 0, 1900" placeholder, not a date.
* - Rounds to the nearest millisecond. A serial is a `double`, whose resolution is about a
* microsecond near 2024 and about 40 µs near 9999 — finer than a millisecond nowhere in
* the range, so rounding there is what makes the round trip through `toExcelSerial` exact.
* - Returns "" on invalid input.
*
* @param value Excel day serial (number, may carry a fractional time of day)
* @param options Which Excel date system the serial is in
* @returns ISO 8601 instant string (UTC), or "" on invalid input
*
* @example fromExcelSerial(1) // "1900-01-01T00:00:00Z" — Excel's first serial
* @example fromExcelSerial(25569) // "1970-01-01T00:00:00Z"
* @example fromExcelSerial(45361.5) // "2024-03-10T12:00:00Z"
* @example fromExcelSerial(59) // "1900-02-28T00:00:00Z"
* @example fromExcelSerial(60) // "" — the phantom 1900-02-29, a date that never existed
* @example fromExcelSerial(61) // "1900-03-01T00:00:00Z"
* @example fromExcelSerial(60, { system: "1904" }) // "1904-03-01T00:00:00Z" — no phantom day here
* @example fromExcelSerial(43899.5, { system: "1904" }) // "2024-03-10T12:00:00Z"
* @example fromExcelSerial(0) // "" — Excel's "January 0, 1900", not a date
* @example fromExcelSerial(-1) // ""
*/
function fromExcelSerial(value, options) {
try {
if (!isOptionsArgument(options)) return "";
const system = optionOrDefault(options?.system, "1900");
if (system !== "1900" && system !== "1904") return "";
if (!Number.isFinite(value)) return "";
const dayMilliseconds = Math.round(value * MILLISECONDS_PER_DAY);
const minimum = system === "1904" ? 0 : 1;
const maximumExclusive = system === "1904" ? MAX_EXCEL_1904_SERIAL_EXCLUSIVE : MAX_EXCEL_1900_SERIAL_EXCLUSIVE;
if (dayMilliseconds < minimum * 864e5 || dayMilliseconds >= maximumExclusive * 864e5) return "";
if (system === "1904") return zonedStringFromEpochNanoseconds(BigInt(dayMilliseconds) * NANOSECONDS_PER_MILLISECOND + EXCEL_1904_EPOCH_NANOSECONDS);
const phantomStart = 60 * MILLISECONDS_PER_DAY;
const phantomEnd = (60 + 1) * MILLISECONDS_PER_DAY;
if (dayMilliseconds >= phantomStart && dayMilliseconds < phantomEnd) return "";
return zonedStringFromEpochNanoseconds(BigInt(dayMilliseconds) * NANOSECONDS_PER_MILLISECOND + (dayMilliseconds >= phantomEnd ? EXCEL_1900_EPOCH_NANOSECONDS : EXCEL_1900_PRE_PHANTOM_EPOCH_NANOSECONDS));
} catch {
return "";
}
}
export { fromExcelSerial };