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.

166 lines (165 loc) • 9.49 kB
// src/internal/foreignEpochs.ts /** * The magic numbers behind `precision/convert/`'s foreign-epoch bridges, in one place. * * Every integration that talks to NTP, Windows, .NET, Excel or PostgreSQL re-derives these * constants, usually wrongly. They live here so each `to*`/`from*` pair reads from the same * declaration and `foreignEpochs.test.ts` can assert every one of them against a real * `Temporal` day count rather than against the arithmetic that consumes them. * * | Format | Epoch | Unit | Representable range | * | --- | --- | --- | --- | * | NTP | 1900-01-01 | 2^-32 s | 64 bits, wrapping every 2^32 s (~136.19 years) into a new era | * | FILETIME | 1601-01-01 | 100 ns | `0n` to 2^63 − 1, 1601-01-01 to +030828-09-14 (`FileTimeToSystemTime`'s limit) | * | .NET ticks | 0001-01-01 | 100 ns | `DateTime.MinValue` to `DateTime.MaxValue`, 0001-01-01 to 9999-12-31 | * | Excel 1900 | 1899-12-30 | 1 day | serial 1 (1900-01-01) to 2958465 (9999-12-31); 60 is the phantom 1900-02-29 | * | Excel 1904 | 1904-01-01 | 1 day | serial 0 (1904-01-01) to 2957003 (9999-12-31) | * | PostgreSQL | 2000-01-01 | 1 µs | `MIN_TIMESTAMP` (4713 BC) up; capped above by `Temporal.Instant`, not by int64 | * | UUIDv1/v6 | 1582-10-15 (proleptic Gregorian) | 100 ns | 60 bits, RFC 9562 §5.1 | * | UUIDv7 / ULID | 1970-01-01 | 1 ms | 48 bits, RFC 9562 §5.7 / ULID spec | * | KSUID | 2014-05-13T16:53:20Z | 1 s | 32 bits unsigned, `ksuid.go` | * | Julian Day | -004713-11-24 (proleptic Gregorian) | 1 day | Julian Day 0; Unix epoch is JD 2 440 587.5 | * | Arrow int64 | 1970-01-01 | s/ms/µs/ns | signed 64 bits, `Schema.fbs` | * | protobuf Timestamp | 1970-01-01 | 1 ns | 0001-01-01 to 9999-12-31T23:59:59Z, `timestamp.proto` | * * Sources: [FILETIME epoch](https://devblogs.microsoft.com/oldnewthing/20090306-00/?p=18913), * [NTP eras](https://www.eecis.udel.edu/~mills/y2k.html), * [.NET ticks](https://www.epochconverter.com/dotnet). */ /** Nanoseconds in one second. */ const NANOSECONDS_PER_SECOND = 1000000000n; /** Nanoseconds in one millisecond — the finest grid an Excel serial's `double` can carry. */ const NANOSECONDS_PER_MILLISECOND = 1000000n; /** Nanoseconds in one microsecond — PostgreSQL `timestamptz` resolution. */ const NANOSECONDS_PER_MICROSECOND = 1000n; /** Nanoseconds in one 100-nanosecond tick — the unit FILETIME and .NET ticks share. */ const NANOSECONDS_PER_TICK = 100n; /** Nanoseconds in one 24-hour day. */ const NANOSECONDS_PER_DAY = 86400000000000n; /** * Nanoseconds in one week: 604 800 s, the week every satellite week counter counts (IS-GPS-200N * §3.3.4: "The largest unit used in stating GPS time is one week defined as 604,800 seconds"). */ const NANOSECONDS_PER_WEEK = 604800000000000n; /** The same value as a `number` — an Excel serial is days in a `double`. */ const NANOSECONDS_PER_DAY_NUMBER = 864e11; /** Milliseconds in one 24-hour day. Excel serials round to this grid; see `fromExcelSerial`. */ const MILLISECONDS_PER_DAY = 864e5; /** * Fractional units in one NTP second: the low 32 bits of the timestamp count 2^-32 s, * i.e. ~232.83 picoseconds. */ const NTP_UNITS_PER_SECOND = 4294967296n; /** * Values in one NTP era: 2^64, the whole 64-bit timestamp. The seconds field is unsigned * 32-bit, so the timestamp wraps here — every 2^32 seconds, or ~136.19 years. */ const NTP_ERA_UNITS = 18446744073709551616n; /** * Nanoseconds from 1900-01-01 (the NTP epoch) to 1970-01-01: 2 208 988 800 s, the delta * RFC 5905 names for converting an NTP timestamp to a Unix one. */ const NTP_EPOCH_OFFSET_NANOSECONDS = 2208988800000000000n; /** * 100-nanosecond intervals from 1601-01-01 (the FILETIME epoch) to 1970-01-01: * 11 644 473 600 s, the constant every Win32-to-Unix conversion carries. */ const FILE_TIME_EPOCH_OFFSET_TICKS = 116444736000000000n; /** Smallest FILETIME: `FILETIME` is a pair of `DWORD`s, so the value is unsigned. */ const MIN_FILE_TIME = 0n; /** * Largest convertible FILETIME: 2^63 − 1, i.e. `+030828-09-14T02:48:05.4775807Z`. * * The struct's two `DWORD`s hold up to 2^64 − 1, but Microsoft's `FileTimeToSystemTime` * says the value "must be less than 0x8000000000000000. Otherwise, the function fails", and * `SetFileTime` reserves `0xFFFFFFFF`/`0xFFFFFFFF` (2^64 − 1) as a "do not modify" marker * rather than a time. Values from 2^63 up are therefore not dates. * Source: https://learn.microsoft.com/en-us/windows/win32/api/timezoneapi/nf-timezoneapi-filetimetosystemtime */ const MAX_FILE_TIME = 9223372036854775807n; /** * 100-nanosecond intervals from 0001-01-01 (the `DateTime` epoch, proleptic Gregorian) to * 1970-01-01. `DateTime.UnixEpoch.Ticks` in .NET. */ const DOT_NET_TICKS_EPOCH_OFFSET = 621355968000000000n; /** `DateTime.MinValue.Ticks` — 0001-01-01T00:00:00. */ const MIN_DOT_NET_TICKS = 0n; /** `DateTime.MaxValue.Ticks` — 9999-12-31T23:59:59.9999999. */ const MAX_DOT_NET_TICKS = 3155378975999999999n; /** * Nanoseconds at 1899-12-30, serial 0 of the 1900 date system. * * Serial 0 is *not* 1899-12-31, even though serial 1 is 1900-01-01: the system counts the * phantom 29 February 1900 that Lotus 1-2-3 invented and Excel keeps for compatibility, so * everything from serial 61 on sits one day later than a naive count would put it. Serials * 1–59 are offset from `EXCEL_1900_PRE_PHANTOM_EPOCH_NANOSECONDS` instead. */ const EXCEL_1900_EPOCH_NANOSECONDS = -2209161600000000000n; /** * Nanoseconds at 1899-12-31 — the effective serial 0 for the 1900 system's serials 1–59, * which precede the phantom leap day. */ const EXCEL_1900_PRE_PHANTOM_EPOCH_NANOSECONDS = -2209075200000000000n; /** Nanoseconds at 1904-01-01, serial 0 of the legacy Mac 1904 date system. */ const EXCEL_1904_EPOCH_NANOSECONDS = -2082844800000000000n; /** * Days between the two systems' serial 0. A 1900-system serial is exactly 1462 higher than * the 1904-system serial for the same date (once past the phantom day). */ const EXCEL_SYSTEM_DAY_DELTA = 1462; /** Excel's first real 1900-system serial: 1 is 1900-01-01. Serial 0 renders as "January 0". */ const MIN_EXCEL_1900_SERIAL = 1; /** Excel's first 1904-system serial: 0 is 1904-01-01. */ const MIN_EXCEL_1904_SERIAL = 0; /** * One past Excel's last 1900-system serial. 2958465 is 9999-12-31, so anything below * 2958466 is inside 9999-12-31. */ const MAX_EXCEL_1900_SERIAL_EXCLUSIVE = 2958466; /** One past Excel's last 1904-system serial. 2957003 is 9999-12-31. */ const MAX_EXCEL_1904_SERIAL_EXCLUSIVE = 2957004; /** * The 1900 system's phantom serial: 60 is 29 February 1900, a date that never existed — * 1900 was not a leap year. The whole serial day `[60, 61)` has no instant behind it. */ const EXCEL_PHANTOM_SERIAL = 60; /** * Nanoseconds at 1900-03-01, the first instant the 1900 system's serials stop needing the * phantom-day correction. */ const EXCEL_1900_PHANTOM_END_NANOSECONDS = -2203891200000000000n; /** * Microseconds from 1970-01-01 to 2000-01-01, PostgreSQL's internal `timestamptz` epoch: * 946 684 800 s. */ const PG_EPOCH_OFFSET_MICROSECONDS = 946684800000000n; /** * PostgreSQL's smallest `timestamp`/`timestamptz`: `MIN_TIMESTAMP` in * `src/include/datatype/timestamp.h`, i.e. Julian Day 0 — `-004713-11-24T00:00:00Z` in the * proleptic Gregorian calendar the ISO strings use, which is the 4713 BC the manual names in * the *Julian* calendar. * * There is no matching maximum here: PostgreSQL's `END_TIMESTAMP` (9223371331200000000, one * past 294276-12-31) is beyond what `Temporal.Instant` can represent, so the instant range * binds first and a second constant would be unreachable. */ const MIN_PG_MICROSECONDS = -211813488000000000n; /** * 100-nanosecond ticks from 1582-10-15 (the UUIDv1/v6 epoch, proleptic Gregorian) to * 1970-01-01: 141 427 days. RFC 9562 Appendix A gives 0x01B21DD213814000. */ const GREGORIAN_EPOCH_OFFSET_TICKS = 122192928000000000n; /** The KSUID epoch, 2014-05-13T16:53:20Z, in Unix seconds (`epochStamp` in `ksuid.go`). */ const KSUID_EPOCH_SECONDS = 1400000000n; /** * Julian Day Number of the civil (UTC) date 1970-01-01, counting the date -004713-11-24 as 0 * (proleptic Gregorian: Temporal's ISO calendar). */ const JULIAN_DAY_OF_UNIX_EPOCH = 2440588; /** Smallest protobuf `Timestamp` seconds: 0001-01-01T00:00:00Z (`timestamp.proto`). */ const PROTO_TIMESTAMP_MIN_SECONDS = -62135596800n; /** Largest protobuf `Timestamp` seconds: 9999-12-31T23:59:59Z (`timestamp.proto`). */ const PROTO_TIMESTAMP_MAX_SECONDS = 253402300799n; /** Largest protobuf `Duration` magnitude in seconds: ±10 000 years (`duration.proto`). */ const PROTO_DURATION_MAX_SECONDS = 315576000000n; export { DOT_NET_TICKS_EPOCH_OFFSET, EXCEL_1900_EPOCH_NANOSECONDS, EXCEL_1900_PHANTOM_END_NANOSECONDS, EXCEL_1900_PRE_PHANTOM_EPOCH_NANOSECONDS, EXCEL_1904_EPOCH_NANOSECONDS, EXCEL_PHANTOM_SERIAL, EXCEL_SYSTEM_DAY_DELTA, FILE_TIME_EPOCH_OFFSET_TICKS, GREGORIAN_EPOCH_OFFSET_TICKS, JULIAN_DAY_OF_UNIX_EPOCH, KSUID_EPOCH_SECONDS, MAX_DOT_NET_TICKS, MAX_EXCEL_1900_SERIAL_EXCLUSIVE, MAX_EXCEL_1904_SERIAL_EXCLUSIVE, MAX_FILE_TIME, MILLISECONDS_PER_DAY, MIN_DOT_NET_TICKS, MIN_EXCEL_1900_SERIAL, MIN_EXCEL_1904_SERIAL, MIN_FILE_TIME, MIN_PG_MICROSECONDS, NANOSECONDS_PER_DAY, NANOSECONDS_PER_DAY_NUMBER, NANOSECONDS_PER_MICROSECOND, NANOSECONDS_PER_MILLISECOND, NANOSECONDS_PER_SECOND, NANOSECONDS_PER_TICK, NANOSECONDS_PER_WEEK, NTP_EPOCH_OFFSET_NANOSECONDS, NTP_ERA_UNITS, NTP_UNITS_PER_SECOND, PG_EPOCH_OFFSET_MICROSECONDS, PROTO_DURATION_MAX_SECONDS, PROTO_TIMESTAMP_MAX_SECONDS, PROTO_TIMESTAMP_MIN_SECONDS };