UNPKG

@gpa-gemstone/common-pages

Version:
114 lines (113 loc) 4.67 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.dateTimeFormat = exports.units = void 0; exports.findAppropriateUnit = findAppropriateUnit; exports.getStartEndTime = getStartEndTime; exports.addDuration = addDuration; exports.getMoment = getMoment; exports.readableUnit = readableUnit; //****************************************************************************************************** // TimeWindowUtils.tsx - Gbtc // // Copyright © 2023, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this // file except in compliance with the License. You may obtain a copy of the License at: // // http://opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 07/11/2023 - C. Lackner // Generated original version of source code. // 06/20/2024 - Ali Karrar // Moved TimeWindowUtil from SEBrowser to gemstone //****************************************************************************************************** var moment_1 = __importDefault(require("moment")); exports.units = ['ms', 's', 'm', 'h', 'd', 'w', 'M', 'y']; exports.dateTimeFormat = 'DD MM YYYY hh:mm:ss.SSS'; /** * A Function to determine the most appropriate unit for a window of time specified by start and end time */ function findAppropriateUnit(startTime, endTime) { var unitIndex = 7; var diff = endTime.diff(startTime, exports.units[unitIndex], true); for (var i = unitIndex; i >= 1; i--) { if (Number.isInteger(diff)) { return exports.units[i]; } var nextI = i - 1; diff = endTime.diff(startTime, exports.units[nextI], true); if (diff > 65000) { diff = endTime.diff(startTime, exports.units[i], true); return exports.units[i]; } } return exports.units[0]; } /** * Function to handle adding or subtracting duration */ function getStartEndTime(center, duration, unit) { var start = addDuration(center, -duration, unit); var end = addDuration(center, duration, unit); return [start, end]; } /* * Function to handle adding or subtracting duration */ function addDuration(start, duration, unit) { var t1 = start.clone(); var floor = duration > 0 ? Math.floor(duration) : Math.ceil(duration); // if duration is negative, use Math.ceil() to get the floor var ceil = duration > 0 ? Math.ceil(duration) : Math.floor(duration); // if duration is negative, use Math.floor() to get the ceil if (floor == ceil && exports.units.findIndex(function (u) { return u == unit; }) >= 4) // if duration is integer, add duration without modifying return t1.add(duration, unit); t1.add(floor, unit); var t2 = t1.clone().add(Math.sign(duration), unit); // Adds a duration of 1 or -1 depending on the sign of input duration var hours = t2.diff(t1, 'h', true) * Math.abs(duration - floor); // Calculates the difference in hours between t2 and t1 and adds to t1 return t1.add(hours, 'h'); } /* * Returns a formatted version of date and time provided */ function getMoment(date, format, time) { if (time === undefined) return (0, moment_1.default)(date, format !== null && format !== void 0 ? format : 'MM/DD/YYYY HH:mm:ss.SSS'); return (0, moment_1.default)(date + ' ' + time, 'MM/DD/YYYY HH:mm:ss.SSS'); } /* * Returns a unit string based on unit char input */ function readableUnit(unit) { if (unit == 'y') { return 'Year(s)'; } else if (unit == 'M') { return 'Month(s)'; } else if (unit == 'w') { return 'Week(s)'; } else if (unit == 'd') { return 'Day(s)'; } else if (unit == 'h') { return 'Hour(s)'; } else if (unit == 'm') { return 'Minute(s)'; } else if (unit == 's') { return 'Second(s)'; } return 'Millisecond(s)'; }