@toreda/time
Version:
Simple, small footprint library for common time operations and converting between units of time.
29 lines (28 loc) • 1.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.timeUntil = timeUntil;
const convert_1 = require("./convert");
const make_1 = require("./make");
const now_1 = require("./now");
const utils_1 = require("./utils");
/**
* Time remaining until `time`. Negative when `time` is already in the past.
* Numbers are interpreted as a unix timestamp in seconds. The returned Time
* uses `units` (defaults to seconds). Returns null when input is invalid or
* the computed result falls outside the safe integer range.
*
* @param time Target Time instance, or unix timestamp in seconds.
* @param units Units for the returned Time. Defaults to `'s'`.
*/
function timeUntil(time, units = 's') {
const now = (0, now_1.timeNow)(units);
const future = typeof time === 'number' ? (0, convert_1.timeConvert)('s', units, time) : (0, convert_1.timeConvert)(time.units(), units, time());
if (future === null) {
return null;
}
const result = future - now();
if (!isFinite(result) || !utils_1.TimeUtils.withinSafeRange(result)) {
return null;
}
return (0, make_1.timeMake)(units, result);
}