@toreda/time
Version:
Simple, small footprint library for common time operations and converting between units of time.
23 lines (22 loc) • 859 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.timeFromDate = timeFromDate;
const make_1 = require("../make");
const parse_date_1 = require("../value/parse-date");
/**
* Create a Time instance from a date string or numeric timestamp. Strings
* are parsed with `Date.parse` (ISO 8601, RFC 2822, etc.); numbers are
* treated as already a timestamp in `units`.
*
* Bad input is sanitized to `0` and logged, matching `timeMake`'s contract.
*
* @param input Date string or numeric timestamp.
* @param units Target unit. Defaults to `'s'` (unix seconds).
* @param log
*
* @category Factories
*/
function timeFromDate(input, units = 's', log) {
const parsed = (0, parse_date_1.timeValueParseDate)(input, units);
return (0, make_1.timeMake)(units, parsed !== null && parsed !== void 0 ? parsed : 0, log);
}