Skip to content

Instantly share code, notes, and snippets.

@dotproto
Last active February 14, 2024 01:54
Show Gist options
  • Save dotproto/ae4eead8f7a97620e963 to your computer and use it in GitHub Desktop.
Save dotproto/ae4eead8f7a97620e963 to your computer and use it in GitHub Desktop.
Convert a Firebase Push ID into Unix time (https://gist.github.com/mikelehen/3596a30bd69384624c11/)
var getPushIdTimestamp = (function getPushIdTimestamp() {
var PUSH_CHARS = '-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz';
return function getTimestampFromId(id) {
var time = 0;
var data = id.substr(0, 8);
for (var i = 0; i < 8; i++) {
time = time * 64 + PUSH_CHARS.indexOf(data[i]);
}
return time;
}
})();
@Voyz
Copy link

Voyz commented Dec 13, 2016

Java port:

private final static String PUSH_CHARS = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";
public static long getTimestampFromId(String id) {
    long time = 0;

    String data = id.substring(0, 8);

    for (int i = 0; i < 8; i++) {
        time = time * 64 + PUSH_CHARS.indexOf(data.charAt(i));
    }

    return time;
}

@JuanPTM
Copy link

JuanPTM commented Dec 16, 2017

Python 2.X/3.X port:

def keyToTimeStamp(key):
        PUSH_CHARS ='-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz'
        idString = key[0:8]
        time = 0
        for letter in idString:
                time = time * 64 + PUSH_CHARS.index(letter)

        return time 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment