Module:TwitterSnowflake/gen: Difference between revisions
Appearance
Content deleted Content added
create module |
No edit summary |
||
Line 17: | Line 17: | ||
local time = os.time() |
local time = os.time() |
||
local timestamp = time - epoch |
local timestamp = time - epoch |
||
local |
local timestampbinary = numberToBinStr(timestamp * 1000) -- lua only provides time in seconds, grr |
||
local snowflakebinary = |
local snowflakebinary = timestampbinary .. shard .. sequence |
||
return tonumber(snowflakebinary,2) |
return tonumber(snowflakebinary,2) |
||
end |
end |
Revision as of 02:53, 20 January 2021
Usage
This generates a Snowflake ID of the current time, based off of Twitter's epoch.
Current ID: 1.9285232582656e+18 (
)See also
local p = {}
local function numberToBinStr(x)
ret=""
while x~=1 and x~=0 do
ret=tostring(x%2)..ret
x=math.modf(x/2)
end
ret=tostring(x)..ret
return ret
end
function p.getSnowflake()
local sequence = "000000000000" -- 12 sequence bits
local shard = "0000000000" -- 10 shard bits
local epoch = 1288834974
local time = os.time()
local timestamp = time - epoch
local timestampbinary = numberToBinStr(timestamp * 1000) -- lua only provides time in seconds, grr
local snowflakebinary = timestampbinary .. shard .. sequence
return tonumber(snowflakebinary,2)
end
return p