add date and time functions.

This commit is contained in:
Christopher Jeffrey 2016-04-19 20:42:46 -07:00
parent 64813d3de1
commit c95681ae3f
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
6 changed files with 34 additions and 8 deletions

View File

@ -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),

View File

@ -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;
};

View File

@ -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;
};

View File

@ -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();

View File

@ -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,

View File

@ -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