@enonic/mock-xp
Version:
Mock Enonic XP API JavaScript Library
53 lines (52 loc) • 1.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Instant = void 0;
var Instant = (function () {
function Instant(value) {
if (typeof value === 'string') {
this.date = new Date(value);
}
else {
this.date = value;
}
}
Instant.convertInstantToDate = function (instant) {
var epochSeconds = instant.getEpochSecond();
var nanoseconds = instant.getNano();
var milliseconds = (epochSeconds * 1000) + (nanoseconds / 1000000);
return new Date(milliseconds);
};
Instant.convertToInstantEpochSeconds = function (jsDate) {
var milliseconds = jsDate.getTime();
var seconds = Math.floor(milliseconds / 1000);
return seconds;
};
Instant.convertToInstantNanoseconds = function (jsDate, useMicroseconds) {
if (useMicroseconds === void 0) { useMicroseconds = false; }
var milliseconds = jsDate.getTime();
var nanoseconds = milliseconds * 1000000;
if (useMicroseconds && performance && performance.now) {
var startTime = performance.now();
var microseconds = Math.floor((performance.now() - startTime) * 1000);
nanoseconds += microseconds * 1000;
}
return nanoseconds;
};
Instant.convertToInstantMilliseconds = function (jsDate) {
return jsDate.getTime();
};
Instant.prototype.getEpochSecond = function () {
return Instant.convertToInstantEpochSeconds(this.date);
};
Instant.prototype.getNano = function () {
return Instant.convertToInstantNanoseconds(this.date);
};
Instant.prototype.toEpochMilli = function () {
return Instant.convertToInstantMilliseconds(this.date);
};
Instant.prototype.toString = function () {
return this.date.toISOString();
};
return Instant;
}());
exports.Instant = Instant;