@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.
61 lines (59 loc) • 3.03 kB
JavaScript
import { nowInstant } from "../../internal/temporalCompat/clock.js";
import { imfFixdate } from "../../internal/englishCalendarNames.js";
import { isValidDateTimeDurationUnit } from "../../plain/validate/isValidDateTimeDurationUnit.js";
import { toUnixEpoch } from "../../internal/unixEpochValue.js";
import { addLengthToStart, readLengthUnits, utcLengthFrame, wholeMillisecondsUntil, wholeSecondsUntil } from "../../internal/lengthFromNow.js";
// src/duration/calculate/ttlFromNow.ts
/**
* Express one length of time from now in every form a receiver wants: seconds, milliseconds, a
* Unix epoch, an HTTP date and a UTC string.
*
* - Use it when one length goes to several receivers, such as a cookie's `Max-Age` and `Expires`
* beside a token's `exp`. Every field comes from one clock read; separate calls can straddle a
* second.
* - `seconds` and `milliseconds` round up, so a wait never falls short. `epochSeconds`, `httpDate`
* and `iso` floor to the same second.
* - Counts in UTC, so the result does not change with the server's zone. A missing day clamps to
* the end of the month, as `addUtc` does. No 400-day cap is applied.
* - `units` is an object of unit amounts, such as `{ days: 90 }`. A duration string returns null. A
* fractional amount returns null: write `{ hours: 36 }` for a day and a half.
* - Lengths are unsigned: a negative amount returns null.
* - `{ days: 0 }` gives `seconds: 0`. RFC 6265 section 4.1.1's server grammar has no zero
* `Max-Age` (`non-zero-digit *DIGIT`), though receivers accept one (section 5.2.2).
* - A year past 9999 returns null, because `httpDate` has no four-digit form (RFC 9110
* section 5.6.7). The whole result is null so a non-null result is usable in full.
* `getUtcIn` and `getUnixIn` reach those years.
* - Examples read the clock at 2027-06-15T12:00:00Z.
*
* @param units unit amounts, such as `{ days: 90 }`; never negative
* @returns the length in each form, or null for invalid `units`, a negative amount, a result
* outside Temporal's range or a year past 9999
*
* @example ttlFromNow({ days: 1 })?.seconds // 86400 (a cookie Max-Age for one day)
* @example ttlFromNow({ days: 1 })?.httpDate // "Wed, 16 Jun 2027 12:00:00 GMT" (the matching Expires)
* @example ttlFromNow({ days: -1 }) // null (lengths are unsigned)
* @example ttlFromNow({ years: 8000 }) // null (year 10027 has no four-digit form)
*/
function ttlFromNow(units) {
try {
const amounts = readLengthUnits(units, isValidDateTimeDurationUnit);
if (amounts === null) return null;
const start = nowInstant();
const end = addLengthToStart(start, amounts, utcLengthFrame());
const httpDate = imfFixdate(end.toZonedDateTimeISO("UTC"));
if (httpDate === "") return null;
return {
seconds: wholeSecondsUntil(end, start),
milliseconds: wholeMillisecondsUntil(end, start),
epochSeconds: toUnixEpoch(end, "seconds"),
httpDate,
iso: end.toString({
smallestUnit: "second",
roundingMode: "floor"
})
};
} catch {
return null;
}
}
export { ttlFromNow };