@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.
72 lines (70 loc) • 2.87 kB
JavaScript
import { Temporal } from "../temporal.js";
// src/internal/temporalCompat/clock.ts
const NANOSECONDS_PER_MILLISECOND = 1000000n;
const INSTANT_LIMIT = 8640000000000000n * NANOSECONDS_PER_MILLISECOND;
/**
* The epoch nanoseconds one millisecond later (clamped to the Instant range) when a reading lies in
* the millisecond before `dateNow`, else `undefined`.
*/
function corrected(epochMilliseconds, epochNanoseconds, dateNow) {
if (epochMilliseconds !== dateNow - 1) return void 0;
const later = epochNanoseconds + NANOSECONDS_PER_MILLISECOND;
return later > INSTANT_LIMIT ? INSTANT_LIMIT : later;
}
/**
* `Temporal.Now.instant()`, never earlier than a `Date.now()` read just before it.
*
* @returns the current instant
* @throws whatever `Temporal.Now.instant` throws
*/
function nowInstant() {
const dateNow = Date.now();
const instant = Temporal.Now.instant();
const later = corrected(instant.epochMilliseconds, instant.epochNanoseconds, dateNow);
return later === void 0 ? instant : Temporal.Instant.fromEpochNanoseconds(later);
}
/**
* `Temporal.Now.zonedDateTimeISO(timeZone)`, never earlier than a `Date.now()` read just before it.
* A bad zone argument throws the polyfill's own error.
*
* @param timeZone a time zone identifier; omitted for the system zone
* @returns the current date and time in the zone
* @throws whatever `Temporal.Now.zonedDateTimeISO` throws
*/
function nowZonedDateTimeISO(timeZone) {
const dateNow = Date.now();
const zoned = Temporal.Now.zonedDateTimeISO(timeZone);
const later = corrected(zoned.epochMilliseconds, zoned.epochNanoseconds, dateNow);
return later === void 0 ? zoned : new Temporal.ZonedDateTime(later, zoned.timeZoneId);
}
/**
* The current date and time in the zone as a `PlainDateTime`, read from {@link nowZonedDateTimeISO}.
*
* @param timeZone a time zone identifier; omitted for the system zone
* @returns the current wall-clock date and time
* @throws whatever `Temporal.Now.zonedDateTimeISO` throws
*/
function nowPlainDateTimeISO(timeZone) {
return nowZonedDateTimeISO(timeZone).toPlainDateTime();
}
/**
* The current date in the zone as a `PlainDate`, read from {@link nowZonedDateTimeISO}.
*
* @param timeZone a time zone identifier; omitted for the system zone
* @returns the current date
* @throws whatever `Temporal.Now.zonedDateTimeISO` throws
*/
function nowPlainDateISO(timeZone) {
return nowZonedDateTimeISO(timeZone).toPlainDate();
}
/**
* The current time in the zone as a `PlainTime`, read from {@link nowZonedDateTimeISO}.
*
* @param timeZone a time zone identifier; omitted for the system zone
* @returns the current wall-clock time
* @throws whatever `Temporal.Now.zonedDateTimeISO` throws
*/
function nowPlainTimeISO(timeZone) {
return nowZonedDateTimeISO(timeZone).toPlainTime();
}
export { nowInstant, nowPlainDateISO, nowPlainDateTimeISO, nowPlainTimeISO, nowZonedDateTimeISO };