Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,19 @@ exports.gen = function gen(date, epoch){
* druuid.time(11142943683383068069);
* // => Sat Feb 04 2012 00:00:00 GMT-0800 (PST)
*
* @param {BigInt|Number|String} uuid
* @param {BigInt|Number|String|Buffer} uuid
* @param {Number} [epoch=druuid.epoch] offset
* @return {Date} when UUID was generated
* @public
*/

exports.time = function(uuid, epoch){
if (!epoch) epoch = exports.epoch;
var ms = bignum(uuid).shiftRight(64 - 41).toNumber();
if (typeof(Buffer) != 'undefined' && uuid instanceof Buffer) {
var bn = bignum.fromBuffer(uuid);
} else {
bn = bignum(uuid);
}
var ms = bn.shiftRight(64 - 41).toNumber();
return new Date(ms + epoch);
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "druuid",
"version": "1.3.0",
"version": "1.3.1",
"description": "Date-relative UUIDs",
"contributors": [
"Stephen Celis <[email protected]>"
Expand Down
7 changes: 7 additions & 0 deletions test/druuid.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,12 @@ describe('druuid', function(){
});
after(function(){ druuid.epoch = oldEpoch; });
});

context('with uuid as Buffer', function() {
var buffer = bignum(uuid).toBuffer();
it('determines when a UUID was generated', function(){
druuid.time(buffer).should.eql(datetime);
});
});
});
});