gpt4 book ai didi

firebase - 您可以从 Firebase 实时数据库 key 获取时间戳吗?

转载 作者:行者123 更新时间:2023-12-03 00:52:09 26 4
gpt4 key购买 nike

根据这个blog post ,firebase 数组键是使用时间戳创建的:

It does this by assigning a permanent, unique id based on the current timestamp (offset to match server time).

有没有办法在给定 key 的情况下恢复此时间戳以供以后使用?

最佳答案

正如我在评论中所说,您不应依赖于从生成的 ID 中解码时间戳。相反,您应该将其存储在 Firebase 的属性中。

也就是说,事实证明恢复时间戳相当容易:

// DO NOT USE THIS CODE IN PRODUCTION AS IT DEPENDS ON AN INTERNAL 
// IMPLEMENTATION DETAIL OF FIREBASE
var PUSH_CHARS = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";
function decode(id) {
id = id.substring(0,8);
var timestamp = 0;
for (var i=0; i < id.length; i++) {
var c = id.charAt(i);
timestamp = timestamp * 64 + PUSH_CHARS.indexOf(c);
}
return timestamp;
}
var key = prompt("Enter Firebase push ID");
if (key) {
var timestamp = decode(key);
console.log(timestamp+"\n"+new Date(timestamp));
alert(timestamp+"\n"+new Date(timestamp));
}

我会重复我的评论,以防万一有人认为将此代码用于逆向工程练习以外的其他用途是个好主意:

Even if you know how to retrieve the timestamp from the key, it would be a bad idea to do this in production code. The timestamp is used to generate a unique, chronologically ordered sequence. If somebody at Firebase figures out a more efficient way (whichever subjective definition of efficiency they happen to choose) to accomplish the same goal, they might change the algorithm for push. If your code needs a timestamp, you should add the timestamp to your data; not depend on it being part of your key.

更新

Firebase 记录了 the algorithm behind Firebase push IDs 。但上述建议仍然存在:不要使用它作为存储日期的替代方法。

关于firebase - 您可以从 Firebase 实时数据库 key 获取时间戳吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27908312/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com