tai64
Version:
TypeScript implementation of TAI64 timestamps for Node.js and the browser
168 lines (167 loc) • 5.95 kB
JavaScript
;
/**
* Copyright (c) 2018 hl2, https://hl2.com
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const long_1 = __importDefault(require("long"));
const leapSeconds_1 = require("./leapSeconds");
class TAI64 {
/**
* Construct an instance of TAI64.
*
* @param label - The TAI64 label between 0 and 2^63-1, inclusive
* @returns An instance of TAI64
* @throws RangeError if the given label is not between 0 and 2^63-1, inclusive
*/
constructor(label) {
this.label = label;
if (label.lt(long_1.default.ZERO) || label.gte(long_1.default.MAX_VALUE)) {
throw new RangeError("Label must be an integer between 0 and 2^63-1, inclusive");
}
}
/**
* Return a TAI64 the current number of seconds elapsed since 1970 TAI.
*
* @returns An instance of TAI64
*/
static now() {
const unixTimestamp = Math.floor(Date.now() / 1000);
return TAI64.fromUnix(unixTimestamp);
}
/**
* Return a TAI64 corresponding to the given UNIX timestamp.
*
* @param timestamp - The UNIX timestamp in seconds
* @returns An instance of TAI64
*/
static fromUnix(timestamp) {
const seconds = leapSeconds_1.addLeapSeconds(timestamp);
const label = TAI64.EPOCH.label.add(seconds);
return new TAI64(label);
}
/**
* Return a TAI64 corresponding to the given hexadecimal string representing a TAI64. This method
* is an alias for `TAI64#fromString()` method.
*
* @param hexString - The hexadecimal string
* @returns An instance of TAI64
*/
static fromHexString(hexString) {
return TAI64.fromString(hexString);
}
/**
* Return a TAI64 corresponding to the given string representing a TAI64 in the given radix.
*
* @param str - The string
* @param radix - An integer that represents the radix (the base in mathematical numeral systems), defaults to `16`
* @returns An instance of TAI64
*/
static fromString(str, radix = 16) {
const label = long_1.default.fromString(str, false, radix);
return new TAI64(label);
}
/**
* Return a TAI64 corresponding to the given byte array representing a TAI64.
*
* @param bytes - The byte array
* @returns An instance of TAI64
*/
static fromByteArray(bytes) {
const label = long_1.default.fromBytes(bytes, false);
return new TAI64(label);
}
/**
* Return if this TAI64 is after the given TAI64.
*
* @param other - The other TAI64 to compare
* @returns `true` if this TAI64 is after the given TAI64, `false` otherwise
*/
isAfter(other) {
return this.compareTo(other) > 0;
}
/**
* Return if this TAI64 is before the given TAI64.
*
* @param other - The other TAI64 to compare
* @returns `true` if this TAI64 is before the given TAI64, `false` otherwise
*/
isBefore(other) {
return this.compareTo(other) < 0;
}
/**
* Return if this TAI64 is equal to the given TAI64.
*
* @param other - The other TAI64 to compare
* @returns `true` if this TAI64 is equal to the given TAI64, `false` otherwise
*/
isEqual(other) {
return this.compareTo(other) === 0;
}
/**
* Compare this TAI64 to the given TAI64.
*
* @param other - The other TAI64 to compare
* @returns
* - `1` if this TAI64 is before the given TAI64
* - `-1` if this TAI64 is before the given TAI64
* - `0` if this TAI64 is equal to the given TAI64
*/
compareTo(other) {
return this.label.compare(other.label);
}
/**
* Return a byte array representation of this TAI64.
*/
toByteArray() {
return this.label.toBytes();
}
/**
* Return an hexadecimal string representation of this TAI64. This method
* is an alias for `TAI64#toString()` method.
*/
toHexString() {
return this.toString();
}
/**
* Return a string representation of this TAI64.
*
* @param radix - An integer that represents the radix (the base in mathematical numeral systems), defaults to `16`
*/
toString(radix = 16) {
return this.label.toString(radix);
}
/**
* Return a UNIX timestamp corresponding to this TAI64.
*/
toUnix() {
const elapsedSeconds = this.label.sub(TAI64.EPOCH.label);
return leapSeconds_1.removeLeapSeconds(elapsedSeconds.toNumber());
}
}
/**
* Instance of TAI64 representing the second that began 1970 TAI.
*/
TAI64.EPOCH = new TAI64(long_1.default.MAX_VALUE.shiftRight(1).add(1));
exports.TAI64 = TAI64;