4

This is related to this question: How can I get Timestamp from Firestore server without bothering for the case when device clock is out of sync with Firestore

I have understood FieldValue.serverTimestamp() can be used to generate a Timestamp in Firestore server while performing database operations. I want to understand how I can get the timestamp from the Firestore Database in a particular device in a Date/Timestamp format without any database related operation.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Maverick7
  • 1,087
  • 12
  • 22

3 Answers3

4

So assuming you have a database schema that looks like this:

Firestore-root
   |
   --- users (collection)
        |
        --- uidOne (document)
              |
              --- name: "UserOne"
              |
              --- creationDate: January 25, 2020 at 3:37:59 PM UTC+3 //Timestamp

To get the value of creationDate property as a Date object, please use the following lines of code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference usersRef = rootRef.collection("users");
usersRef.document(uid).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        DocumentSnapshot document = task.getResult();
        if (document.exists()) {
            Date creationDate = document.getDate("creationDate");
            //Do what you need to do with your Date object
        }
    }
});

Remember, in order to get this Date object, the date should be set as explained in my answer from the following post:

Edit:

The question emphasized to get the Server Timestamp without performing any database related operation.

There is no way you can do this since that timestamp value is generated entirely by Firebase servers. In other words, when a write operation takes place, that timestamp is generated. You cannot simulate this on the client and it makes sense since you cannot randomly generate a server timestamp yourself. Only Firebase servers can do that. If you need a particular timestamp, you should not generate it that way. Since that property is of type Date, you can set it with any Date you'll like. So instead of letting Firestore decide what timestamp to generate, set the Date yourself.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Hi, I think the question was not clear enough. I understand how to get creationDate from the database in a Date format. The question emphasized to get the Server Timestamp without performing any database related operation. (Just like Timestamp.now() gives me the timestamp with device offsets, I am searching for something similar to get the Server Timestamp) I would like to use the Server Timestamp for performing comparisons locally on the device. – Maverick7 Jan 25 '20 at 11:33
  • I understand, but this is not possible. Check my updated answer. – Alex Mamo Jan 25 '20 at 11:48
  • Thanks @Alex Mamo for your answer. But setting a date object myself, would not make it consistent in terms of relative times across devices and thus won't solve the problem I'm facing. – Maverick7 Jan 25 '20 at 13:52
  • But I just hope that Firestore could add a functionality where I could get the Server Timestamp with a simple read operation from Firestore – Maverick7 Jan 25 '20 at 13:56
  • 1
    I think [this](https://stackoverflow.com/questions/59586220/how-to-save-localdata-in-firebase-realtime-database) might help you. It's for Firebase Realtime database but the same rule applies in Cloud Firestore. To solve this, simply save the offset into a new property ;) – Alex Mamo Jan 25 '20 at 13:58
  • You can [file a feature request](https://firebase.google.com/support/troubleshooter/contact) for that ;) – Alex Mamo Jan 25 '20 at 13:59
  • Thanks @Alex Mamo, you have been very helpful! – Maverick7 Jan 27 '20 at 17:19
  • @Maverick7 Good to hear that :) And you're very welcome, cheers! – Alex Mamo Jan 27 '20 at 17:54
4

It's not possible to get the server timestamp from Firestore without using a database operation.

What you can do instead is write a Cloud Function (probably an HTTP trigger, or a callable type function) that returns the current time, and invoke that directly from the client to get that timestamp. This avoids having to work with the database, and you'll end up with the same time, since all Google service have synchronized clocks.

Bear in mind that a round trip over a network will result in the client receiving a slightly delayed view of the server time, since you don't know how long the response will take to return over the network.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
0

I find my way of solving my problem with getting the server timestamp: FireStore to Android Device(JAVA):

`//Inside serverTimeDate var: Timestamp{seconds: 1663598620 nanoseconds :  numbers}`
Timestamp serverTimeDate  = new Timestamp(new Date());
//Epoch & Unix Timestamp sample : 1663598620 = Monday, September 19, 2022 2:43:40 PM
Log.d(TAG, " Epoch & Unix Timestamp "+ currentTimeDate.getSeconds());
// Console Log the Nanoseconds
 Log.d(TAG, " Nanoseconds: "+ currentTimeDate.getNanoseconds());

I also use this converter upon checking whether it is exactly getting the right Date and Time. https://www.epochconverter.com/ & Firestore Documents for reference: https://firebase.google.com/docs/reference/js/v8/firebase.firestore.Timestamp

Ctian
  • 1
  • 1