【发布时间】:2021-01-12 23:15:31
【问题描述】:
我没有在某些值中保存时间戳,我需要从 firebase 数据库生成的密钥中找出时间戳。如何做到这一点?
【问题讨论】:
标签: firebase-realtime-database
我没有在某些值中保存时间戳,我需要从 firebase 数据库生成的密钥中找出时间戳。如何做到这一点?
【问题讨论】:
标签: firebase-realtime-database
在谷歌上搜索了一段时间但没有找到答案后,我决定看看 firebase 的源代码。
一旦我发现密钥是如何生成的 (https://github.com/firebase/firebase-js-sdk/blob/master/packages/database/src/core/util/NextPushId.ts)
我发现时间戳在键的开头被转换为 8 符号字符串 + 之后添加了一些随机符号。
所以我写了一个小脚本来解码时间戳。
const decodeFirebaseKey = key => {
// chars firebase use for generating keys
const CHARS = '-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz';
let timestamp = 0
// we only need to decode first 8 symbols
for (let i = 0; i <= 7; i++) {
const index = CHARS.indexOf(key[i])
timestamp = timestamp * 64 + index
}
return timestamp
}
希望它能为您节省一些时间。
我还将脚本上传到了 gist:https://gist.github.com/Stas-Buzunko/b4f2ed1dc122cdb1867e13a731fc5dcf
【讨论】: