Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the Firebase server timestamp to generate date created?

Tags:

Currently, the Google's version of ServerValue.TIMESTAMP returns {".sv":"timestamp"} which is used as a directive for Firebase to fill that field with the server timestamp once you save the data to the Firebase server.

When you create your data on the client side however, you don't have the actual timestamp to play with yet (ie. use as the creation date). You only will have an access to the timestamp after the initial save and consequent retrieval, which - I imagine - is sometimes too late and not very elegant.


Before Google:

Update: Ignore this section as it is incorrect - I misunderstood the examples. ServerValue.TIMESTAMP always returned the {".sv":"timestamp"}.

As far as I understand in pre-google Firebase there seemed to be a server-generated timestamp available that allowed you to acquire the actual timestamp:

import com.firebase.client.ServerValue;
ServerValue.TIMESTAMP // eg. 1466094046

(ref 1, ref 2)


Questions:

  1. Is such save/retrieval the only way to get the server-generated creation date on my model instances?
  2. If yes can you propose a method of implementing such pattern?
  3. Am I understanding correctly ServerValue.TIMESTAMP has changed with Google's acquisition of Firebase? Update: No, @FrankvanPuffelen replied that nothing's changed during acquisition.

Note:

I'm not considering using new Date() on client side as I've been reading it's not safe, though please share your thoughts if you think different.

like image 773
Voy Avatar asked Jun 16 '16 16:06

Voy


People also ask

How do I convert a firestore date timestamp to a JS Date ()?

To convert a Firestore date or timestamp to a JavaScript Date, we use firebase. firestore. Timestamp. fromDate to convert the a date to a Firestore timestamp.

What is Firebase timestamp?

firestore. Timestamp. A Timestamp represents a point in time independent of any time zone or calendar, represented as seconds and fractions of seconds at nanosecond resolution in UTC Epoch time. It is encoded using the Proleptic Gregorian Calendar which extends the Gregorian calendar backwards to year one.

How do I get Firebase time?

To reach the timestamp of firebase server on client, you first need to write the value to the server then read the value. firebase. database(). ref('currentTime/').

How does Firebase timestamp compare?

As of SDK Version 7.10. 0, you can directly compare Timestamp objects with the JavaScript arithmetic inequality comparison operators ( < , <= , > , and >= ). To check if two Timestamp objects are temporally identical you must use the isEqual property (docs).


1 Answers

When you use the ServerValue.TIMESTAMP constant in a write operation, you're saying that the Firebase Database server should determine the correct timestamp when it executes the write operation.

Let's say we run this code:

ref.addValueEventListener(new ValueEventListener() {
    public void onDataChange(DataSnapshot dataSnapshot) {
        System.out.println(dataSnapshot.getValue()); 
    }

    public void onCancelled(DatabaseError databaseError) { }
});
ref.setValue(ServerValue.TIMESTAMP);

This will execute as follows:

  1. you attach a listener
  2. you write a value with ServerValue.TIMESTAMP
  3. the Firebase client immediate fires a value event with an approximation of the timestamp it will write on the server
  4. your code prints that value
  5. the write operation gets sent to the Firebase servers
  6. the Firebase servers determine the actual timestamp and write the value to the database (assuming no security rules fail)
  7. the Firebase server send the actual timestamp back to the client
  8. the Firebase client raises a value event for the actual value
  9. your code prints that value

If you're using ChildEventListener instead of a ValueEventListener, then the client will call onChildAdded in step 3 and onChildChanged in step 8.

Nothing changed in the way we generate the ServerValue.TIMESTAMP since Firebase joined Google. Code that worked before, will continue to work. That also means that the first answer you linked is a valid way to handle it.

like image 62
Frank van Puffelen Avatar answered Sep 19 '22 20:09

Frank van Puffelen