Josh
Josh

Reputation: 103

Use Firebase server timestamp in Firebase URL

I would like to use the current time of the Firebase servers in a Firebase URL. Below is an example of what I am trying to accomplish.

Firebase(url: "https://location.firebaseio.com/logs/<firebase_time_stamp>")
Firebase(url: "https://location.firebaseio.com/logs/" + Firebase.TIME_STAMP)

Is there anyway to do this? If so, I need it to be workable in Swift and Java. I know you can do something like this as a child but that is not what I'm looking for.

Upvotes: 4

Views: 335

Answers (2)

Joseph Francis
Joseph Francis

Reputation: 1231

Use ServerValue.timestamp() for swift 4.0.

let timeStamp = ServerValue.timestamp()

let str = "https://location.firebaseio.com/logs/\(timeStamp)"

Upvotes: 0

Frank van Puffelen
Frank van Puffelen

Reputation: 599491

The Firebase server-side timestamp (iOS, Android) can only be used in values, not in keys.

The closest approximation I can think of is to use the client-side time of the underlying platform and the offset that Firebase keeps track off. Rob gives a pretty good explanation of that here:

There is no way to get the server time synchronously from Firebase, but the local time + server offset time (provided immediately whenever you write to Firebase using the ServerValue) is a good approximation.

Also see this section of the documentation that introduces /.info/serverTimeOffset.

Upvotes: 3

Related Questions