Does anyone know if `getTickcount()` is reliable f...
# cfml-general
a
Does anyone know if
getTickcount()
is reliable for getting the milliseconds from 1970? Seems to be the same as Java, so may just use java to be sure if doesn't do strange things with timezones or something! https://trycf.com/gist/a3aa5914b8f561acdcd49ad565885b65/acf2023?theme=monokai
I'm looking to generate snowflake ids and haven't spotted a CFML version
d
Read this.
🤔 1
I'm also interested in where the machine ID portion of the snowflake comes from. That's not an easy problem, as software copy protection attests.
a
I've seen it done using the mac address, or you can manually set it in most implementations
d
In a past life we looked into using the mac address, but it gets dodgy. Machines can have multiple network cards, some virtual and/or transient. Depending on the deployment model, configuring IDs manually per machine may be reasonable.
a
Yeah. snowflake ids should still be unique even if all machines (up to a point) are using the same id, it's just another nit in the encoding.
d
I don't understand what keeps IDs unique if multiple machines have the same machine ID. The rest is just a timestamp and a machine-specific sequence number. The machine ID is all that prevents multiple machines from creating the same ID. Am I misunderstanding the concept?
a
Well, true, you could get a collision within the same millisecond. The implementation I looked at used a rand if no machine id is passed to it. So for my use case the risk is low.
A GUID is safer but for my use case then I want something that is fast to sort in a database so integers work well but I still need it to be unique across databases so snowflake seems like a good fit.
b
I think
getTickCount()
is reliable for getting the milliseconds from the Unix epoch (January 1, 1970, 00:00 GMT). You can verify it yourself. Here goes:
ticks=getTickCount();
secs=ticks/1000;
epoch=dateAdd("s",-secs,now());
epochToUTC=dateConvert("local2Utc",epoch);
writeoutput("Epoch to UTC: " & epochToUTC);