ravendb
Version:
RavenDB client for Node.js
101 lines • 3.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TimeUtil = void 0;
const StringUtil_js_1 = require("./StringUtil.js");
const index_js_1 = require("../Exceptions/index.js");
const StringBuilder_js_1 = require("./StringBuilder.js");
class TimeUtil {
static _parseMiddlePart(input) {
const tokens = input.split(":");
const hours = Number.parseInt(tokens[0], 10);
const minutes = Number.parseInt(tokens[1], 10);
const seconds = Number.parseInt(tokens[2], 10);
if (tokens.length !== 3) {
(0, index_js_1.throwError)("InvalidArgumentException", "Unexpected duration format: " + input);
}
const totalSeconds = seconds + minutes * 60 + hours * 3600;
return totalSeconds * 1000;
}
static MILLIS_IN_DAY = 24 * 3600 * 1000;
static durationToTimeSpan(duration) {
let time = duration;
const millis = time % 1000;
time -= millis;
time = time / 1000; // seconds
const seconds = time % 60;
time -= seconds;
time = time / 60; // in minutes
const minutes = time % 60;
time -= minutes;
time = time / 60; // in hours
const hours = time % 24;
time -= hours;
time = time / 24; // in days
const days = time;
const sb = new StringBuilder_js_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 = !!/^\d+\./.test(text);
const hasMillis = !!/.*\.\d+/.test(text);
if (hasDays && hasMillis) {
const tokens = text.split(".");
const days = Number.parseInt(tokens[0], 10);
const millis = tokens[2] ? Number.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 = Number.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 = Number.parseInt(fractionString, 10) / 10_000;
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; // seconds
const seconds = time % 60;
time = (time - seconds) / 60; // in minutes
const minutes = time % 60;
time = (time - minutes) / 60; // in hours
const hours = time % 24;
time = (time - hours) / 24;
const days = time;
let result = "";
if (days) {
result += days + ".";
}
result += StringUtil_js_1.StringUtil.leftPad(hours.toString(), 2, "0")
+ ":"
+ StringUtil_js_1.StringUtil.leftPad(minutes.toString(), 2, "0")
+ ":"
+ StringUtil_js_1.StringUtil.leftPad(seconds.toString(), 2, "0");
if (millis) {
result += "." + StringUtil_js_1.StringUtil.leftPad(millis.toString(), 3, "0") + "000";
}
return result;
}
}
exports.TimeUtil = TimeUtil;
//# sourceMappingURL=TimeUtil.js.map