@signumjs/util
Version:
Useful utilities and tools for building Signum Network applications
92 lines • 2.59 kB
JavaScript
;
/**
* Original work Copyright (c) 2020 Burst Apps Team
* Modfied work Copyright (c) 2021 Signum Network
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChainTime = void 0;
const GenesisBlockTime = Date.UTC(2014, 7, 11, 2, 0, 0, 0) / 1000;
/**
* A Value Object to facilitate Chain Timestamp conversions.
*
* @category value-objects
*/
class ChainTime {
_chainTimestamp;
constructor(blockTimestamp) {
this._chainTimestamp = blockTimestamp;
}
/**
* Creates a Block Time object from Chain Time Stamp
* @param timestamp The timestamp from Chain
*/
static fromChainTimestamp(timestamp) {
return new ChainTime(timestamp);
}
/**
* Creates a Block Time object from Date
* @param date Any Date object
*/
static fromDate(date) {
const blockTime = new ChainTime(0);
blockTime.setDate(date);
return blockTime;
}
/**
* @return Gets Chain Timestamp representation
*/
getChainTimestamp() {
return this._chainTimestamp;
}
/**
* Sets ChainTime using Chain Timestamp
*/
setChainTimestamp(blockTimestamp) {
this._chainTimestamp = blockTimestamp;
}
/**
* @return Time in seconds since 01.01.1970
*/
getEpoch() {
return (GenesisBlockTime + this._chainTimestamp) * 1000;
}
/**
* @return real Date representation
*/
getDate() {
return new Date(this.getEpoch());
}
/**
* Sets blockTime using native Date
* @param date Any Date object
*/
setDate(date) {
this._chainTimestamp = Math.round(date.getTime() / 1000) - GenesisBlockTime;
}
/**
* Checks for equality
* @param chainTime The other value to be compared
* @return true if equal, otherwise false
*/
equals(chainTime) {
return this._chainTimestamp === chainTime._chainTimestamp;
}
/**
* Checks if a chainTime is before a given one
* @param chainTime The other value to be compared
* @return true if _before_ a given chainTime, otherwise false
*/
before(chainTime) {
return this._chainTimestamp < chainTime._chainTimestamp;
}
/**
* Checks if a chainTime is after a given one
* @param chainTime The other value to be compared
* @return true if _after_ a given chainTime, otherwise false
*/
after(chainTime) {
return this._chainTimestamp > chainTime._chainTimestamp;
}
}
exports.ChainTime = ChainTime;
//# sourceMappingURL=chainTime.js.map