UNPKG

@intuitionrobotics/ts-common

Version:
99 lines 3.73 kB
/* * ts-common is the basic building blocks of our typescript projects * * Copyright (C) 2020 Intuition Robotics * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import dayjs from "dayjs"; import utc from "dayjs/plugin/utc.js"; import customParseFormat from "dayjs/plugin/customParseFormat.js"; import {} from "./types.js"; // Plugin extends are global + idempotent. Running them at module load means // any consumer that imports ts-common's date-time-tools gets a dayjs with // .utc() and string-with-format parsing already wired up — same surface the // previous moment-backed implementation exposed. dayjs.extend(utc); dayjs.extend(customParseFormat); export const Second = 1000; export const Minute = Second * 60; export const Hour = Minute * 60; export const Day = Hour * 24; export const Week = Day * 7; export const Format_HHmmss_DDMMYYYY = "HH:mm:ss_DD-MM-YYYY"; export const Format_YYYYMMDD_HHmmss = "YYYY-MM-DD_HH:mm:ss"; export async function timeout(sleepMs) { return new Promise(resolve => setTimeout(resolve, sleepMs, undefined)); } export function _setTimeout(handler, sleepMs = 0, ...args) { return setTimeout(handler, sleepMs, ...args); } export function _clearTimeout(handlerId) { if (!handlerId) return; return clearTimeout(handlerId); } export function _setInterval(handler, sleepMs = 0, ...args) { return setInterval(handler, sleepMs, ...args); } export function _clearInterval(handlerId) { if (!handlerId) return; return clearInterval(handlerId); } export function auditBy(user, comment, timestamp = currentTimeMillies()) { const _auditBy = { auditBy: user, auditAt: createReadableTimestampObject(Format_HHmmss_DDMMYYYY, timestamp) }; if (comment) _auditBy.comment = comment; return _auditBy; } export function currentTimeMillies() { const date = new Date(); return date.getTime(); } export function currentLocalTimeMillies() { const date = new Date(); return date.getTime(); } export function currentTimeMilliesWithTimeZone() { const date = new Date(); return date.getTime() + date.getTimezoneOffset(); } export function createReadableTimestampObject(pattern = Format_HHmmss_DDMMYYYY, timestamp = currentTimeMillies(), timezone) { const timeObj = { timestamp: timestamp, pretty: formatTimestamp(pattern, timestamp) }; if (timezone) timeObj.timezone = timezone; return timeObj; } export function formatTimestamp(pattern = Format_HHmmss_DDMMYYYY, timestamp = currentTimeMillies(), timezone) { // dayjs is immutable (unlike moment), so utcOffset(...) returns a new // instance — we must reassign rather than relying on in-place mutation. // `timezone` here is misnamed: utcOffset accepts a numeric minute offset // or a "+HH:MM" string, NOT an IANA zone name. Behaviour matches the // previous moment-backed implementation. let m = dayjs.utc(timestamp); if (timezone) { m = m.utcOffset(timezone); } return m.format(pattern); } export function parseTimeString(timestamp, pattern = Format_HHmmss_DDMMYYYY) { return dayjs.utc(timestamp, pattern).valueOf(); } //# sourceMappingURL=date-time-tools.js.map