我没有在某些值中保存时间戳,我需要从 Firebase 数据库生成的密钥中找出时间戳。如何做到这一点?
答案 0 :(得分:0)
在谷歌上搜索了一段时间并没有找到答案后,我决定查看 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
}
希望它能为您节省一些时间。
我还上传了脚本到一个要点:https://gist.github.com/Stas-Buzunko/b4f2ed1dc122cdb1867e13a731fc5dcf