@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.
117 lines (115 loc) • 5.25 kB
JavaScript
import { Temporal } from "./temporal.js";
import { addToZoned, subtractFromZoned } from "./zonedWallClockOperations.js";
import { durationTotal } from "./zonedWallClockDifference.js";
import { frameInstant, frameZoned, zoneFrame } from "./zoneFrame.js";
import { readDurationUnits } from "./readDurationUnits.js";
// src/internal/lengthFromNow.ts
/**
* A `units` argument as a length: `readDurationUnits`, then null if any amount is below zero.
* `-0` is not below zero, so it stays a zero length.
*
* @param units the caller's `units` argument
* @param isAllowed type guard for the units the calling function takes
* @returns the unit amounts, or null on invalid input or a negative amount
*
* @example readLengthUnits({ days: 1 }, isValidDateTimeDurationUnit) // { days: 1 }
* @example readLengthUnits({ days: -1 }, isValidDateTimeDurationUnit) // null
*/
function readLengthUnits(units, isAllowed) {
const amounts = readDurationUnits(units, isAllowed);
if (amounts === null) return null;
return Object.values(amounts).some((amount) => amount < 0) ? null : amounts;
}
/**
* The instant `amounts` after `start`, counted on `frame`'s wall clock.
*
* Pure: it reads no clock, and the caller passes its own start. A missing calendar day clamps
* (TC39 Temporal `AddZonedDateTime`, `overflow: "constrain"`); a wall time in a gap moves later
* and a repeated one takes the earlier (`"compatible"`). Throws what Temporal throws, for
* example `RangeError` past its range; the public wrapper catches.
*
* @param start the instant the length is counted from
* @param amounts the length as a unit object
* @param frame the frame whose wall clock calendar units count on
* @returns the instant the length ends
*
* @example addLengthToStart(Temporal.Instant.from("2027-06-15T12:00:00Z"), { days: 90 }, zoneFrame("UTC")).toString() // "2027-09-13T12:00:00Z"
*/
function addLengthToStart(start, amounts, frame) {
return frameInstant(addToZoned(frameZoned(start, frame), amounts, { overflow: "constrain" }), frame);
}
/**
* The length of `amounts` from `start`, in `unit`, fractional and unrounded.
*
* Pure, and it throws what Temporal throws; the public wrapper catches. Calendar units are
* measured from `start` on `frame`'s wall clock (TC39 Temporal `Duration.prototype.total` with a
* zoned `relativeTo`).
*
* @param start the instant the length is counted from
* @param amounts the length as a unit object
* @param unit the Temporal unit to total in
* @param frame the frame whose wall clock calendar units count on
* @returns the total in `unit`
*
* @example totalLengthFromStart(Temporal.Instant.from("2027-06-15T12:00:00Z"), { years: 1 }, "seconds", zoneFrame("UTC")) // 31622400
*/
function totalLengthFromStart(start, amounts, unit, frame) {
return durationTotal(Temporal.Duration.from(amounts), unit, frameZoned(start, frame));
}
/**
* Whole seconds from `now` to `target`, rounded toward positive infinity. Never `-0`.
*
* @param target the instant counted to
* @param now the instant counted from
* @returns whole seconds, negative when `target` is a second or more past
*
* @example wholeSecondsUntil(Temporal.Instant.from("2027-06-15T12:01:00Z"), Temporal.Instant.from("2027-06-15T12:00:00Z")) // 60
*/
function wholeSecondsUntil(target, now) {
const ns = target.epochNanoseconds - now.epochNanoseconds;
const whole = ns / 1000000000n;
return Number(ns % 1000000000n > 0n ? whole + 1n : whole);
}
/**
* The instant `amounts` before `start`, counted on `frame`'s wall clock.
*
* Pure, like `addLengthToStart`: it reads no clock and throws what Temporal throws. A missing
* calendar day clamps (`overflow: "constrain"`), as `subtractFromZoned` does for `subtract*`.
*
* @param start the instant the length is counted back from
* @param amounts the length as a unit object
* @param frame the frame whose wall clock calendar units count on
* @returns the instant the length begins
*
* @example subtractLengthFromStart(Temporal.Instant.from("2027-06-15T12:00:00Z"), { days: 90 }, zoneFrame("UTC")).toString() // "2027-03-17T12:00:00Z"
*/
function subtractLengthFromStart(start, amounts, frame) {
return frameInstant(subtractFromZoned(frameZoned(start, frame), amounts, { overflow: "constrain" }), frame);
}
/**
* Whole milliseconds from `now` to `target`, rounded toward positive infinity. Never `-0`.
*
* @param target the instant counted to
* @param now the instant counted from
* @returns whole milliseconds, negative when `target` is a millisecond or more past
*
* @example wholeMillisecondsUntil(Temporal.Instant.from("2027-06-15T12:00:01Z"), Temporal.Instant.from("2027-06-15T12:00:00Z")) // 1000
*/
function wholeMillisecondsUntil(target, now) {
const ns = target.epochNanoseconds - now.epochNanoseconds;
const whole = ns / 1000000n;
return Number(ns % 1000000n > 0n ? whole + 1n : whole);
}
/**
* The UTC frame that unix, UTC and from-now lengths count on.
*
* A function, not a module constant, so the file keeps no top-level work.
*
* @returns the frame for `"UTC"`
*
* @example utcLengthFrame().timeZone // "UTC"
*/
function utcLengthFrame() {
return zoneFrame("UTC");
}
export { addLengthToStart, readLengthUnits, subtractLengthFromStart, totalLengthFromStart, utcLengthFrame, wholeMillisecondsUntil, wholeSecondsUntil };