From 6d78355cec22d629d7446836d4760f5d76f7a245 Mon Sep 17 00:00:00 2001 From: Sebastian Korotkiewicz Date: Tue, 6 Feb 2024 06:10:46 +0100 Subject: [PATCH] fix date --- decode.js | 35 ++++++++++++---------------- encode.js | 68 +++++++++++++++++++++++++++---------------------------- index.js | 37 +++++++++++++----------------- 3 files changed, 65 insertions(+), 75 deletions(-) diff --git a/decode.js b/decode.js index fb9395d..41b0524 100644 --- a/decode.js +++ b/decode.js @@ -1,28 +1,23 @@ function decodeSnowflake(idStr) { - const id = BigInt(idStr); - const binary = id.toString(2).padStart(64, '0'); + const id = BigInt(idStr); + const timestampShift = 22n; + const machineIdShift = 12n; + const sequenceMask = 0xfffn; - const timestampShift = 22n; - const machineIdShift = 12n; - const sequenceMask = 0xFFFn; // 12 bitów na sekwencję + const timestamp = (id >> timestampShift) & 0x1ffffffffffn; // Adjusting the shift + const machineId = (id >> machineIdShift) & 0x3ffn; + const sequence = id & sequenceMask; - const timestamp = (id >> (timestampShift + machineIdShift)) & 0x1FFFFFFFFFFn; - const machineId = (id >> machineIdShift) & 0x3FFn; // 10 bitów na ID maszyny - const sequence = id & sequenceMask; + const date = new Date(Number(timestamp)); // Converting BigInt to Number, then to Date - // Upewnij się, że epoka jest taka sama, jak użyta do generowania Snowflake IDs - const epoch = BigInt(1288834974657); // Powinna być taka sama, jak w generatorze - const date = new Date(Number(epoch + (timestamp * 1000n))); - - return { - timestamp: date, - machineId: machineId.toString(), - sequence: sequence.toString(), - }; + return { + timestamp: date.toISOString(), + machineId: machineId.toString(), + sequence: sequence.toString(), + }; } -// Użycie -const idStr = '7160488856901390336'; // Przykładowy identyfikator Snowflake +// Use +const idStr = "7160488856901390336"; const decoded = decodeSnowflake(idStr); console.log(decoded); - diff --git a/encode.js b/encode.js index 0013b90..6baf508 100644 --- a/encode.js +++ b/encode.js @@ -1,46 +1,46 @@ class Snowflake { - constructor(machineId) { - if (machineId < 0 || machineId >= 1024) { - throw new Error('Machine ID must be in the range 0-1023.'); - } - - this.machineId = BigInt(machineId); - this.sequence = BigInt(0); - this.lastTimestamp = BigInt(-1); - - // Konfiguracja bitów: 41 bitów na czas, 10 bitów na ID maszyny, 12 bitów na sekwencję - this.timestampShift = 22n; - this.machineIdShift = 12n; - this.sequenceMask = 0xFFFn; // 4095 w dziesiętnym, BigInt + constructor(machineId) { + if (machineId < 0 || machineId >= 1024) { + throw new Error("Machine ID must be in the range 0-1023."); } - generate() { - let timestamp = BigInt(Date.now()); + this.machineId = BigInt(machineId); + this.sequence = BigInt(0); + this.lastTimestamp = BigInt(-1); - if (timestamp === this.lastTimestamp) { - this.sequence = (this.sequence + 1n) & this.sequenceMask; + this.timestampShift = 22n; + this.machineIdShift = 12n; + this.sequenceMask = 0xfffn; + } - if (this.sequence === 0n) { - while (timestamp <= this.lastTimestamp) { - timestamp = BigInt(Date.now()); - } - } - } else { - this.sequence = 0n; + async generate() { + let timestamp = BigInt(Date.now()); + + if (timestamp === this.lastTimestamp) { + this.sequence = (this.sequence + 1n) & this.sequenceMask; + + if (this.sequence === 0n) { + while (timestamp <= this.lastTimestamp) { + await new Promise((resolve) => setTimeout(resolve, 1)); + timestamp = BigInt(Date.now()); } - - this.lastTimestamp = timestamp; - - const id = ((timestamp & 0x1FFFFFFFFFFn) << this.timestampShift) | - (this.machineId << this.machineIdShift) | - this.sequence; - - return id.toString(); // Zwraca string, ponieważ BigInt może być zbyt duży dla niektórych zastosowań + } + } else { + this.sequence = 0n; } + + this.lastTimestamp = timestamp; + + const id = + ((timestamp & 0x1ffffffffffn) << this.timestampShift) | + (this.machineId << this.machineIdShift) | + this.sequence; + + return id.toString(); + } } -// Użycie -const machineId = 1; // ID maszyny (0-1023) +const machineId = 1; // machine ID (0-1023) const snowflake = new Snowflake(machineId); const id1 = snowflake.generate(); diff --git a/index.js b/index.js index b958cad..5cc34c0 100644 --- a/index.js +++ b/index.js @@ -1,30 +1,28 @@ class Snowflake { - constructor(machineId, epoch) { + constructor(machineId) { if (machineId < 0 || machineId >= 1024) { throw new Error("Machine ID must be in the range 0-1023."); } - this.epoch = BigInt(epoch); // Ustawienie epoki this.machineId = BigInt(machineId); this.sequence = BigInt(0); this.lastTimestamp = BigInt(-1); - // Konfiguracja bitów: 41 bitów na czas, 10 bitów na ID maszyny, 12 bitów na sekwencję this.timestampShift = 22n; this.machineIdShift = 12n; - this.sequenceMask = 0xfffn; // 4095 w dziesiętnym, BigInt + this.sequenceMask = 0xfffn; } async generate() { - let timestamp = BigInt(Date.now()) - this.epoch; // Odejmowanie epoki + let timestamp = BigInt(Date.now()); if (timestamp === this.lastTimestamp) { this.sequence = (this.sequence + 1n) & this.sequenceMask; if (this.sequence === 0n) { while (timestamp <= this.lastTimestamp) { - await new Promise((resolve) => setTimeout(resolve, 1)); // Opóźnienie 1 ms - timestamp = BigInt(Date.now()) - this.epoch; + await new Promise((resolve) => setTimeout(resolve, 1)); + timestamp = BigInt(Date.now()); } } } else { @@ -38,40 +36,37 @@ class Snowflake { (this.machineId << this.machineIdShift) | this.sequence; - return id.toString(); // Zwraca string, ponieważ BigInt może być zbyt duży dla niektórych zastosowań + return id.toString(); } } -function decodeSnowflake(idStr, epoch) { +function decodeSnowflake(idStr) { const id = BigInt(idStr); const timestampShift = 22n; const machineIdShift = 12n; - const sequenceMask = 0xfffn; // 12 bitów na sekwencję + const sequenceMask = 0xfffn; - const timestamp = (id >> (timestampShift + machineIdShift)) & 0x1ffffffffffn; - const machineId = (id >> machineIdShift) & 0x3ffn; // 10 bitów na ID maszyny + const timestamp = (id >> timestampShift) & 0x1ffffffffffn; // Adjusting the shift + const machineId = (id >> machineIdShift) & 0x3ffn; const sequence = id & sequenceMask; - const epochBigInt = BigInt(epoch); // Upewnij się, że epoka jest BigInt - const date = new Date(Number(epochBigInt + timestamp * 1000n)); // Dodaj epokę do czasu + const date = new Date(Number(timestamp)); // Converting BigInt to Number, then to Date return { - timestamp: date, + timestamp: date.toISOString(), machineId: machineId.toString(), sequence: sequence.toString(), }; } -// Użycie +// Use (async () => { - const machineId = 1; // ID maszyny (0-1023) - const epoch = 1288834974657; // Ustawienie epoki - const snowflake = new Snowflake(machineId, epoch); + const machineId = 1; // machine ID (0-1023) + const snowflake = new Snowflake(machineId); const id1 = await snowflake.generate(); console.log("encodeID", id1); - const idStr = id1; // Przykładowy identyfikator Snowflake - const decoded = decodeSnowflake(idStr, epoch); + const decoded = decodeSnowflake(id1); console.log("decodeID", decoded); })();