你能从Firebase阵列密钥中获取时间戳吗?

时间:2022-03-02 19:45:53

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

根据这篇博文,firebase数组键是使用时间戳创建的:

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

它通过根据当前时间戳(匹配服务器时间的偏移量)分配永久的唯一ID来实现此目的。

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

有没有办法恢复这个时间戳以便以后使用,给定密钥?

1 个解决方案

#1


26  

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.

正如我在评论中所说,你不应该依赖于从生成的id解码时间戳。而不是那样,您只需将其存储在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.

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

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记录了Firebase推送ID背后的算法。但上述建议仍然存在:不要将此作为存储日期的替代方法。

#1


26  

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.

正如我在评论中所说,你不应该依赖于从生成的id解码时间戳。而不是那样,您只需将其存储在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.

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

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记录了Firebase推送ID背后的算法。但上述建议仍然存在:不要将此作为存储日期的替代方法。