你可以从Firebase数组键获取时间戳吗? [英] Can you get the timestamp from a Firebase array key?

查看:112
本文介绍了你可以从Firebase数组键获取时间戳吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据这个博文使用时间戳创建firebase数组键:


通过根据当前时间戳分配一个永久唯一的id (偏移量匹配服务器时间)。

有没有办法恢复这个时间戳以备后用? >

解决方案

正如我在我的评论中所说的,你不应该依赖从生成的id解码时间戳。相反,您应该将其存储在Firebase中的某个媒体资源中。



这就是说,返回时间戳相当容易:



//不要使用这个代码来生产,因为它依赖于INTERNAL //实现FIREBASEvar的详细信息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); (返回时间戳);} var key = prompt(输入Firebase推送ID); if(key){var timestamp = decode(key); console.log(timestamp +\\\
+ new Date(timestamp)); alert(timestamp +\\\
+ new Date(timestamp));}

我会重复我的评论,以防万一有人认为在反向工程中使用此代码是一个好主意:


即使您知道如何从密钥中检索时间戳,在生产代码中执行此操作也不是个好主意。时间戳用于生成唯一的,按时间顺序排列的序列。如果Firebase中的某个人找出了一个更有效的方法(无论他们选择了效率的主观定义)来完成相同的目标,他们可能会改变 push 的算法。如果你的代码需要一个时间戳,你应该添加时间戳到你的数据;不要依靠它作为你的钥匙的一部分。




更新



Firebase记录了
Firebase推送ID背后的算法。但是上面的建议仍然存在:不要用这个替代存储日期。


According to this blog post, firebase array keys are created using a timestamp:

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

Is there a way to recover this timestamp for use later, given the key?

解决方案

As I said in my comment, you should not rely on decoding the timestamp from the generated id. Instead of that, you should simply store it in a property in your Firebase.

That said, it turns out to be fairly easy to get the timestamp back:

// 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));
}

I'll repeat my comment, just in case somebody thinks it is a good idea to use this code for anything else than as an exercise in reverse engineering:

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.

Update

Firebase documented the algorithm behind Firebase push IDs. But the above advice remains: don't use this as an alternative to storing the date.

这篇关于你可以从Firebase数组键获取时间戳吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆