diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index c433c797..6d55e3a9 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -506,7 +506,7 @@ Block.prototype.inspect = function inspect() { hash: utils.revHex(this.hash('hex')), size: this.getSize(), virtualSize: this.getVirtualSize(), - date: new Date(this.ts * 1000).toISOString(), + date: utils.date(this.ts), version: this.version, prevBlock: utils.revHex(this.prevBlock), merkleRoot: utils.revHex(this.merkleRoot), diff --git a/lib/bcoin/headers.js b/lib/bcoin/headers.js index 21ec65b7..8282f61c 100644 --- a/lib/bcoin/headers.js +++ b/lib/bcoin/headers.js @@ -95,7 +95,7 @@ Headers.prototype.inspect = function inspect() { delete copy._raw; copy.hash = this.hash('hex'); copy.rhash = this.rhash; - copy.date = new Date((copy.ts || 0) * 1000).toISOString(); + copy.date = utils.date(copy.ts); return copy; }; diff --git a/lib/bcoin/merkleblock.js b/lib/bcoin/merkleblock.js index 4f5fb68d..07e6989d 100644 --- a/lib/bcoin/merkleblock.js +++ b/lib/bcoin/merkleblock.js @@ -246,7 +246,7 @@ MerkleBlock.prototype.inspect = function inspect() { delete copy._chain; copy.hash = this.hash('hex'); copy.rhash = this.rhash; - copy.date = new Date((copy.ts || 0) * 1000).toISOString(); + copy.date = utils.date(copy.ts); return copy; }; diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 81786731..ab099382 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -851,7 +851,7 @@ Pool.prototype._handleBlock = function _handleBlock(block, peer, callback) { 'Status: tip=%s ts=%s height=%d blocks=%d orphans=%d active=%d' + ' queue=%d target=%s peers=%d pending=%d highest=%d jobs=%d', block.rhash, - new Date(block.ts * 1000).toISOString().slice(0, -5) + 'Z', + utils.date(block.ts), self.chain.height, self.chain.total, self.chain.orphan.count, @@ -1477,7 +1477,7 @@ Pool.prototype.searchWallet = function(wallet, callback) { bcoin.debug( 'Reverted chain to height=%d (%s)', self.chain.height, - new Date(self.chain.tip.ts * 1000) + utils.date(self.chain.tip.ts) ); callback(); @@ -1495,11 +1495,11 @@ Pool.prototype.searchWallet = function(wallet, callback) { return callback(err); } - bcoin.debug('Wallet time: %s', new Date(ts * 1000)); + bcoin.debug('Wallet time: %s', utils.date(ts)); bcoin.debug( 'Reverted chain to height=%d (%s)', self.chain.height, - new Date(self.chain.tip.ts * 1000) + utils.date(self.chain.tip.ts) ); callback(); diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 77f335cb..0d84638e 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -1564,7 +1564,7 @@ TX.prototype.inspect = function inspect() { minFee: utils.btc(this.getMinFee()), confirmations: this.getConfirmations(), priority: this.getPriority().toString(10), - date: new Date((this.ts || this.ps) * 1000).toISOString(), + date: utils.date(this.ts || this.ps), block: this.block ? utils.revHex(this.block) : null, ts: this.ts, ps: this.ps, diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index bd05fc16..6a6d31ef 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -1226,6 +1226,32 @@ utils.now = function now() { return +new Date() / 1000 | 0; }; +/** + * Create a Date ISO string from time in unix time (seconds). + * @param {Number?} - Seconds in unix time. + * @returns {String} + */ + +utils.date = function date(ts) { + if (ts == null) + ts = utils.now(); + + return new Date(ts * 1000).toISOString().slice(0, -5) + 'Z'; +}; + +/** + * Get unix seconds from a Date string. + * @param {String} date - Date ISO String. + * @returns {Number} + */ + +utils.time = function time(date) { + if (date == null) + return utils.now(); + + return new Date(date) / 1000 | 0; +}; + /** * UINT32_MAX * @type BN