UNPKG

@intuitionrobotics/ts-common

Version:
111 lines 4.38 kB
"use strict"; /* * 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. */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Format_YYYYMMDD_HHmmss = exports.Format_HHmmss_DDMMYYYY = exports.Week = exports.Day = exports.Hour = exports.Minute = exports.Second = void 0; exports.timeout = timeout; exports._setTimeout = _setTimeout; exports._clearTimeout = _clearTimeout; exports._setInterval = _setInterval; exports._clearInterval = _clearInterval; exports.auditBy = auditBy; exports.currentTimeMillies = currentTimeMillies; exports.currentLocalTimeMillies = currentLocalTimeMillies; exports.currentTimeMilliesWithTimeZone = currentTimeMilliesWithTimeZone; exports.createReadableTimestampObject = createReadableTimestampObject; exports.formatTimestamp = formatTimestamp; exports.parseTimeString = parseTimeString; const moment = require("moment"); exports.Second = 1000; exports.Minute = exports.Second * 60; exports.Hour = exports.Minute * 60; exports.Day = exports.Hour * 24; exports.Week = exports.Day * 7; exports.Format_HHmmss_DDMMYYYY = "HH:mm:ss_DD-MM-YYYY"; exports.Format_YYYYMMDD_HHmmss = "YYYY-MM-DD_HH:mm:ss"; function timeout(sleepMs) { return __awaiter(this, void 0, void 0, function* () { return new Promise(resolve => setTimeout(resolve, sleepMs, undefined)); }); } function _setTimeout(handler, sleepMs = 0, ...args) { return setTimeout(handler, sleepMs, ...args); } function _clearTimeout(handlerId) { if (!handlerId) return; return clearTimeout(handlerId); } function _setInterval(handler, sleepMs = 0, ...args) { return setInterval(handler, sleepMs, ...args); } function _clearInterval(handlerId) { if (!handlerId) return; return clearInterval(handlerId); } function auditBy(user, comment, timestamp = currentTimeMillies()) { const _auditBy = { auditBy: user, auditAt: createReadableTimestampObject(exports.Format_HHmmss_DDMMYYYY, timestamp) }; if (comment) _auditBy.comment = comment; return _auditBy; } function currentTimeMillies() { const date = new Date(); return date.getTime(); } function currentLocalTimeMillies() { const date = new Date(); return date.getTime(); } function currentTimeMilliesWithTimeZone() { const date = new Date(); return date.getTime() + date.getTimezoneOffset(); } function createReadableTimestampObject(pattern = exports.Format_HHmmss_DDMMYYYY, timestamp = currentTimeMillies(), timezone) { const timeObj = { timestamp: timestamp, pretty: formatTimestamp(pattern, timestamp) }; if (timezone) timeObj.timezone = timezone; return timeObj; } function formatTimestamp(pattern = exports.Format_HHmmss_DDMMYYYY, timestamp = currentTimeMillies(), timezone) { const m = moment.utc(timestamp); if (timezone) { m.utcOffset(timezone); } return m.format(pattern); } function parseTimeString(timestamp, pattern = exports.Format_HHmmss_DDMMYYYY) { return moment.utc(timestamp, pattern).valueOf(); } //# sourceMappingURL=date-time-tools.js.map