Initial commit

This commit is contained in:
Sebastian Korotkiewicz 2024-02-06 05:43:57 +01:00
commit 9bc4ca3870
Signed by: skorotkiewicz
GPG key ID: 5BDC557B496BDB0D
4 changed files with 155 additions and 0 deletions

28
decode.js Normal file
View file

@ -0,0 +1,28 @@
function decodeSnowflake(idStr) {
const id = BigInt(idStr);
const binary = id.toString(2).padStart(64, '0');
const timestampShift = 22n;
const machineIdShift = 12n;
const sequenceMask = 0xFFFn; // 12 bitów na sekwencję
const timestamp = (id >> (timestampShift + machineIdShift)) & 0x1FFFFFFFFFFn;
const machineId = (id >> machineIdShift) & 0x3FFn; // 10 bitów na ID maszyny
const sequence = id & sequenceMask;
// 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(),
};
}
// Użycie
const idStr = '7160488856901390336'; // Przykładowy identyfikator Snowflake
const decoded = decodeSnowflake(idStr);
console.log(decoded);