@azure/cosmos
Version:
Microsoft Azure Cosmos DB Service Node.js SDK for NOSQL API
210 lines (209 loc) • 6.85 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var timeSpan_exports = {};
__export(timeSpan_exports, {
TimeSpan: () => TimeSpan
});
module.exports = __toCommonJS(timeSpan_exports);
const ticksPerMillisecond = 1e4;
const millisecondsPerTick = 1 / ticksPerMillisecond;
const ticksPerSecond = ticksPerMillisecond * 1e3;
const secondsPerTick = 1 / ticksPerSecond;
const ticksPerMinute = ticksPerSecond * 60;
const minutesPerTick = 1 / ticksPerMinute;
const ticksPerHour = ticksPerMinute * 60;
const hoursPerTick = 1 / ticksPerHour;
const ticksPerDay = ticksPerHour * 24;
const daysPerTick = 1 / ticksPerDay;
const millisPerSecond = 1e3;
const millisPerMinute = millisPerSecond * 60;
const millisPerHour = millisPerMinute * 60;
const millisPerDay = millisPerHour * 24;
const maxMilliSeconds = Number.MAX_SAFE_INTEGER / ticksPerMillisecond;
const minMilliSeconds = Number.MIN_SAFE_INTEGER / ticksPerMillisecond;
class TimeSpan {
_ticks;
constructor(days, hours, minutes, seconds, milliseconds) {
if (!Number.isInteger(days)) {
throw new Error("days is not an integer");
}
if (!Number.isInteger(hours)) {
throw new Error("hours is not an integer");
}
if (!Number.isInteger(minutes)) {
throw new Error("minutes is not an integer");
}
if (!Number.isInteger(seconds)) {
throw new Error("seconds is not an integer");
}
if (!Number.isInteger(milliseconds)) {
throw new Error("milliseconds is not an integer");
}
const totalMilliSeconds = (days * 3600 * 24 + hours * 3600 + minutes * 60 + seconds) * 1e3 + milliseconds;
if (totalMilliSeconds > maxMilliSeconds || totalMilliSeconds < minMilliSeconds) {
throw new Error("Total number of milliseconds was either too large or too small");
}
this._ticks = totalMilliSeconds * ticksPerMillisecond;
}
/**
* Returns a new TimeSpan object whose value is the sum of the specified TimeSpan object and this instance.
* @param ts - The time interval to add.
*/
add(ts) {
if (TimeSpan.additionDoesOverflow(this._ticks, ts._ticks)) {
throw new Error("Adding the two timestamps causes an overflow.");
}
const results = this._ticks + ts._ticks;
return TimeSpan.fromTicks(results);
}
/**
* Returns a new TimeSpan object whose value is the difference of the specified TimeSpan object and this instance.
* @param ts - The time interval to subtract.
*/
subtract(ts) {
if (TimeSpan.subtractionDoesUnderflow(this._ticks, ts._ticks)) {
throw new Error("Subtracting the two timestamps causes an underflow.");
}
const results = this._ticks - ts._ticks;
return TimeSpan.fromTicks(results);
}
/**
* Compares this instance to a specified object and returns an integer that indicates whether this
* instance is shorter than, equal to, or longer than the specified object.
* @param value - The time interval to add.
*/
compareTo(value) {
if (value == null) {
return 1;
}
if (!TimeSpan.isTimeSpan(value)) {
throw new Error("Argument must be a TimeSpan object");
}
return TimeSpan.compare(this, value);
}
/**
* Returns a new TimeSpan object whose value is the absolute value of the current TimeSpan object.
*/
duration() {
return TimeSpan.fromTicks(this._ticks >= 0 ? this._ticks : -this._ticks);
}
/**
* Returns a value indicating whether this instance is equal to a specified object.
* @param value - The time interval to check for equality.
*/
equals(value) {
if (TimeSpan.isTimeSpan(value)) {
return this._ticks === value._ticks;
}
return false;
}
/**
* Returns a new TimeSpan object whose value is the negated value of this instance.
* @param value - The time interval to check for equality.
*/
negate() {
return TimeSpan.fromTicks(-this._ticks);
}
days() {
return Math.floor(this._ticks / ticksPerDay);
}
hours() {
return Math.floor(this._ticks / ticksPerHour);
}
milliseconds() {
return Math.floor(this._ticks / ticksPerMillisecond);
}
seconds() {
return Math.floor(this._ticks / ticksPerSecond);
}
ticks() {
return this._ticks;
}
totalDays() {
return this._ticks * daysPerTick;
}
totalHours() {
return this._ticks * hoursPerTick;
}
totalMilliseconds() {
return this._ticks * millisecondsPerTick;
}
totalMinutes() {
return this._ticks * minutesPerTick;
}
totalSeconds() {
return this._ticks * secondsPerTick;
}
static fromTicks(value) {
const timeSpan = new TimeSpan(0, 0, 0, 0, 0);
timeSpan._ticks = value;
return timeSpan;
}
static zero = new TimeSpan(0, 0, 0, 0, 0);
static maxValue = TimeSpan.fromTicks(Number.MAX_SAFE_INTEGER);
static minValue = TimeSpan.fromTicks(Number.MIN_SAFE_INTEGER);
static isTimeSpan(timespan) {
return timespan._ticks;
}
static additionDoesOverflow(a, b) {
const c = a + b;
return a !== c - b || b !== c - a;
}
static subtractionDoesUnderflow(a, b) {
const c = a - b;
return a !== c + b || b !== a - c;
}
static compare(t1, t2) {
if (t1._ticks > t2._ticks) {
return 1;
}
if (t1._ticks < t2._ticks) {
return -1;
}
return 0;
}
static interval(value, scale) {
if (isNaN(value)) {
throw new Error("value must be a number");
}
const milliseconds = value * scale;
if (milliseconds > maxMilliSeconds || milliseconds < minMilliSeconds) {
throw new Error("timespan too long");
}
return TimeSpan.fromTicks(Math.floor(milliseconds * ticksPerMillisecond));
}
static fromMilliseconds(value) {
return TimeSpan.interval(value, 1);
}
static fromSeconds(value) {
return TimeSpan.interval(value, millisPerSecond);
}
static fromMinutes(value) {
return TimeSpan.interval(value, millisPerMinute);
}
static fromHours(value) {
return TimeSpan.interval(value, millisPerHour);
}
static fromDays(value) {
return TimeSpan.interval(value, millisPerDay);
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
TimeSpan
});