From 9897fa2876ea0bde66e9994dbb8cca1502d066f7 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 29 Oct 2014 12:12:12 +1100 Subject: [PATCH] block: adds getUTCDate function --- src/block.js | 15 +++++++++++---- test/block.js | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/block.js b/src/block.js index 8fcce5a..be409c0 100644 --- a/src/block.js +++ b/src/block.js @@ -132,12 +132,19 @@ Block.prototype.toHex = function(headersOnly) { return this.toBuffer(headersOnly).toString('hex') } -Block.prototype.getId = function() { - return bufferutils.reverse(this.getHash()).toString('hex') -} - Block.prototype.getHash = function() { return crypto.hash256(this.toBuffer(true)) } +Block.prototype.getId = function() { + return bufferutils.reverse(this.getHash()).toString('hex') +} + +Block.prototype.getUTCDate = function() { + var date = new Date(0) // epoch + date.setUTCSeconds(this.timestamp) + + return date +} + module.exports = Block diff --git a/test/block.js b/test/block.js index 4caeb9f..c509980 100644 --- a/test/block.js +++ b/test/block.js @@ -69,4 +69,20 @@ describe('Block', function() { }) }) }) + + describe('getUTCDate', function() { + fixtures.valid.forEach(function(f) { + var block + + beforeEach(function() { + block = Block.fromHex(f.hex) + }) + + it('returns UTC date of ' + f.id, function() { + var utcDate = block.getUTCDate().getTime() + + assert.equal(utcDate, f.timestamp * 1e3) + }) + }) + }) })