fix created timestamp precision (#1241)

* fix created timestamp precision

* perf

* Update Snowflake.js

* gawdl3y was butthurt

* Update Snowflake.js
This commit is contained in:
Gus Caplan
2017-03-09 17:31:31 -06:00
committed by Schuyler Cebulskie
parent 21babf8859
commit 2897692cf1
7 changed files with 23 additions and 9 deletions

View File

@@ -34,7 +34,8 @@ class SnowflakeUtil {
/**
* A deconstructed snowflake
* @typedef {Object} DeconstructedSnowflake
* @property {Date} date Date in the snowflake
* @property {number} timestamp Timestamp the snowflake was created
* @property {Date} date Date the snowflake was created
* @property {number} workerID Worker ID in the snowflake
* @property {number} processID Process ID in the snowflake
* @property {number} increment Increment in the snowflake
@@ -48,13 +49,18 @@ class SnowflakeUtil {
*/
static deconstruct(snowflake) {
const BINARY = pad(Long.fromString(snowflake).toString(2), 64);
return {
date: new Date(parseInt(BINARY.substring(0, 42), 2) + EPOCH),
const res = {
timestamp: parseInt(BINARY.substring(0, 42), 2) + EPOCH,
workerID: parseInt(BINARY.substring(42, 47), 2),
processID: parseInt(BINARY.substring(47, 52), 2),
increment: parseInt(BINARY.substring(52, 64), 2),
binary: BINARY,
};
Object.defineProperty(res, 'date', {
get: function get() { return new Date(this.timestamp); },
enumerable: true,
});
return res;
}
}