我正在尝试解码 mysql 中的 uuid 以确定哪些数字有助于准备 date 部分。例如uuid ==> b54adc00-67f9-11d9-9669-0800200c9a66
上面 uuid 中的 date 部分是什么。
回答1
我想也许你正在寻找这样的东西:
select uuid AS uid
, from_unixtime(
(conv(
concat(
substring(uuid, 16, 3)
, substring(uuid, 10, 4)
, substring(uuid, 1, 8)
), 16, 10)
div 10 div 1000 div 1000
) - (141427 * 24 * 60 * 60)
) AS uuid_to_timestamp
, current_timestamp() AS timestamp
from test ;
https://www.db-fiddle.com/f/4uegQFAyd1NyuSme7F2JwX/1
参考:https://rpbouman.blogspot.com/2014/06/mysql-extracting-timstamp-and-mac.html?m=0
回答2
这仅适用于变体 1 类型 uuids。对于过时的变体0或很少使用的变体2,需要修改,其他变体不包括dates;该变体存储在 uuid 中(除非它是完全随机的),但我没有检查它。
假设 uuid 是字符串形式的 uuid :
select '1582-10-15'
+ interval floor(
conv(
concat(
substr(uuid,16,3),
substr(uuid,10,4),
substr(uuid,1,8)
),
16,
10
)
/1e7
) second
这会产生 UTC 的日期时间;如果您在当地时区需要它,请将其转换为:
convert_tz(
'1582-10-15' + interval floor(conv(concat(substr(uuid,16,3),substr(uuid,10,4),substr(uuid,1,8)),16,10)/1e7) second,
'+00:00',
'America/Los_Angeles'
)
如果您只想要 date,请将上述之一包装在 DATE( ... )
中。