Trying to convert a Discord snowflake (64-bit number) to a unix timestamp with Google Sheets.
Message ID | Timestamp |
---|---|
287134013666099000 | =BITRSHIFT(A2,22)+1420070400000 |
Trying to convert a Discord snowflake (64-bit number) to a unix timestamp with Google Sheets.
Message ID | Timestamp |
---|---|
287134013666099000 | =BITRSHIFT(A2,22)+1420070400000 |
Sheets, naturally, freak out because the number fed into BITRSHIFT
is above 2^48.
Function BITRSHIFT parameter 1 value is 2.87134e+17. It should be less than 281474976710656.
Any workaround?
Share Improve this question edited Feb 5 at 18:16 absolutely not the the asked Jan 31 at 15:40 absolutely not the theabsolutely not the the 6811 bronze badges 10 | Show 5 more comments2 Answers
Reset to default 4Use division by a power a two, like this:
=int(A2 / 2 ^ 22) + 1420070400000
See Arithmetic shift.
To convert a Unix timestamp to a human-readable Google Sheets datetime, use epochtodate(). To get millisecond precision, format the result as Format > Number > Custom number format > yyyy-MM-dd hh:mm:ss.000
, which gives:
2017-03-03 08:08:03.550
For additional background, see Working with date and time values in Google Sheets.
287134013666099000 in binary is (according to this website)
1111111100000110101101110100110111011111111111111100111000
If we discard the rightmost 22 bits we get
111111110000011010110111010011011101
and converting back to decimal we get
68458083549
=int(A2 / 2 ^ 22)
gives
68458083550
whereas this (which takes the original number as a string)
function myFunction(s) {
const bigNumber = BigInt(s);
//console.log(bigNumber.toString());
//console.log((bigNumber>>BigInt(22)).toString());
return (bigNumber>>BigInt(22)).toString();
}
gives
68458083549
2^22
, so try replacingbitrshift
withint(A2 / 2 ^ 22)
. – doubleunary Commented Jan 31 at 16:0722 bit positions is the same as dividing by 2^22
I believe this is only true given that the bits to be removed are all zeroes (which is insignificant for very large numbers due to rounding off). But this logic (if my assumption is correct) does not apply for small numbers especially when removing bits of 1. – PatrickdC Commented Jan 31 at 16:14int()
. Right shifting discards LSBs, so it doesn't matter whether the rightmost bits are zeroes or ones. Are you thinking of circular shift? – doubleunary Commented Jan 31 at 16:50int()
part. It does round off the decimal part when dividing by 2^n. i.e.10001
in decimal is 17. Applying it with BITRSHIFT(17,2) becomes100
which is 4 in decimal. 17/2^2 is 4.25. But with the help ofint()
, it will round down to 4. – PatrickdC Commented Jan 31 at 17:30