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 * Represents any channel on Discord
*/ */
@@ -38,7 +40,7 @@ class Channel {
* @readonly * @readonly
*/ */
get createdTimestamp() { 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 Constants = require('../util/Constants');
const Collection = require('../util/Collection'); const Collection = require('../util/Collection');
const Snowflake = require('../util/Snowflake');
/** /**
* Represents a custom emoji * Represents a custom emoji
@@ -57,7 +58,7 @@ class Emoji {
* @readonly * @readonly
*/ */
get createdTimestamp() { 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 Constants = require('../util/Constants');
const Collection = require('../util/Collection'); const Collection = require('../util/Collection');
const Util = require('../util/Util'); const Util = require('../util/Util');
const Snowflake = require('../util/Snowflake');
/** /**
* Represents a guild (or a server) on Discord. * Represents a guild (or a server) on Discord.
@@ -216,7 +217,7 @@ class Guild {
* @readonly * @readonly
*/ */
get createdTimestamp() { 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 * Represents an OAuth2 Application
*/ */
@@ -100,7 +102,7 @@ class OAuth2Application {
* @readonly * @readonly
*/ */
get createdTimestamp() { 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'); const Permissions = require('../util/Permissions');
/** /**
@@ -78,7 +79,7 @@ class Role {
* @readonly * @readonly
*/ */
get createdTimestamp() { 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 TextBasedChannel = require('./interface/TextBasedChannel');
const Constants = require('../util/Constants'); const Constants = require('../util/Constants');
const Presence = require('./Presence').Presence; const Presence = require('./Presence').Presence;
const Snowflake = require('../util/Snowflake');
/** /**
* Represents a user on Discord. * Represents a user on Discord.
@@ -76,7 +77,7 @@ class User {
* @readonly * @readonly
*/ */
get createdTimestamp() { get createdTimestamp() {
return (this.id / 4194304) + 1420070400000; return Snowflake.deconstruct(this.id).timestamp;
} }
/** /**

View File

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