UNPKG

@configurator/ravendb

Version:
101 lines 3.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TimeUtil = void 0; const StringUtil_1 = require("./StringUtil"); const Exceptions_1 = require("../Exceptions"); const StringBuilder_1 = require("./StringBuilder"); class TimeUtil { static _parseMiddlePart(input) { const tokens = input.split(":"); const hours = parseInt(tokens[0], 10); const minutes = parseInt(tokens[1], 10); const seconds = parseInt(tokens[2], 10); if (tokens.length !== 3) { (0, Exceptions_1.throwError)("InvalidArgumentException", "Unexpected duration format: " + input); } const totalSeconds = seconds + minutes * 60 + hours * 3600; return totalSeconds * 1000; } static durationToTimeSpan(duration) { let time = duration; const millis = time % 1000; time -= millis; time = time / 1000; const seconds = time % 60; time -= seconds; time = time / 60; const minutes = time % 60; time -= minutes; time = time / 60; const hours = time % 24; time -= hours; time = time / 24; const days = time; const sb = new StringBuilder_1.StringBuilder(); if (days) { sb.append(days).append("."); } sb.append(hours.toString().padStart(2, "0")).append(":"); sb.append(minutes.toString().padStart(2, "0")).append(":"); sb.append(seconds.toString().padStart(2, "0")); if (millis) { sb.append("."); sb.append(millis.toString().padStart(3, "0")) .append("0000"); } return sb.toString(); } static timeSpanToDuration(text) { const hasDays = !!text.match(/^\d+\./); const hasMillis = !!text.match(/.*\.\d+/); if (hasDays && hasMillis) { const tokens = text.split("."); const days = parseInt(tokens[0], 10); const millis = tokens[2] ? parseInt(tokens[2], 10) : 0; return this._parseMiddlePart(tokens[1]) + millis + days * TimeUtil.MILLIS_IN_DAY; } else if (hasDays) { const tokens = text.split("."); const days = parseInt(tokens[0], 10); return this._parseMiddlePart(tokens[1]) + days * TimeUtil.MILLIS_IN_DAY; } else if (hasMillis) { const tokens = text.split("."); let fractionString = tokens[1]; fractionString = fractionString.padEnd(7, "0"); const value = parseInt(fractionString, 10) / 10000; return this._parseMiddlePart(tokens[0]) + value; } else { return this._parseMiddlePart(text); } } static millisToTimeSpan(value) { let time = value; const millis = time % 1000; time = (time - millis) / 1000; const seconds = time % 60; time = (time - seconds) / 60; const minutes = time % 60; time = (time - minutes) / 60; const hours = time % 24; time = (time - hours) / 24; const days = time; let result = ""; if (days) { result += days + "."; } result += StringUtil_1.StringUtil.leftPad(hours.toString(), 2, "0") + ":" + StringUtil_1.StringUtil.leftPad(minutes.toString(), 2, "0") + ":" + StringUtil_1.StringUtil.leftPad(seconds.toString(), 2, "0"); if (millis) { result += "." + StringUtil_1.StringUtil.leftPad(millis.toString(), 3, "0") + "000"; } return result; } } exports.TimeUtil = TimeUtil; TimeUtil.MILLIS_IN_DAY = 24 * 3600 * 1000; //# sourceMappingURL=TimeUtil.js.map