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

@@ -1,3 +1,5 @@
const Snowflake = require('../util/Snowflake');
/**
* Represents any channel on Discord
*/
@@ -38,7 +40,7 @@ class Channel {
* @readonly
*/
get createdTimestamp() {
return (this.id / 4194304) + 1420070400000;
return Snowflake.deconstruct(this.id).timestamp;
}
/**

View File

@@ -1,5 +1,6 @@
const Constants = require('../util/Constants');
const Collection = require('../util/Collection');
const Snowflake = require('../util/Snowflake');
/**
* Represents a custom emoji
@@ -57,7 +58,7 @@ class Emoji {
* @readonly
*/
get createdTimestamp() {
return (this.id / 4194304) + 1420070400000;
return Snowflake.deconstruct(this.id).timestamp;
}
/**

View File

@@ -6,6 +6,7 @@ const GuildMember = require('./GuildMember');
const Constants = require('../util/Constants');
const Collection = require('../util/Collection');
const Util = require('../util/Util');
const Snowflake = require('../util/Snowflake');
/**
* Represents a guild (or a server) on Discord.
@@ -216,7 +217,7 @@ class Guild {
* @readonly
*/
get createdTimestamp() {
return (this.id / 4194304) + 1420070400000;
return Snowflake.deconstruct(this.id).timestamp;
}
/**

View File

@@ -1,3 +1,5 @@
const Snowflake = require('../util/Snowflake');
/**
* Represents an OAuth2 Application
*/
@@ -100,7 +102,7 @@ class OAuth2Application {
* @readonly
*/
get createdTimestamp() {
return (this.id / 4194304) + 1420070400000;
return Snowflake.deconstruct(this.id).timestamp;
}
/**

View File

@@ -1,3 +1,4 @@
const Snowflake = require('../util/Snowflake');
const Permissions = require('../util/Permissions');
/**
@@ -78,7 +79,7 @@ class Role {
* @readonly
*/
get createdTimestamp() {
return (this.id / 4194304) + 1420070400000;
return Snowflake.deconstruct(this.id).timestamp;
}
/**

View File

@@ -1,6 +1,7 @@
const TextBasedChannel = require('./interface/TextBasedChannel');
const Constants = require('../util/Constants');
const Presence = require('./Presence').Presence;
const Snowflake = require('../util/Snowflake');
/**
* Represents a user on Discord.
@@ -76,7 +77,7 @@ class User {
* @readonly
*/
get createdTimestamp() {
return (this.id / 4194304) + 1420070400000;
return Snowflake.deconstruct(this.id).timestamp;
}
/**

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