Maverick7
Maverick7

Reputation: 1097

How to get server Timestamp from Firestore in an android device?

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.

Upvotes: 4

Views: 6187

Answers (3)

Ctian
Ctian

Reputation: 1

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

Upvotes: 0

Doug Stevenson
Doug Stevenson

Reputation: 317808

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.

Upvotes: 4

Alex Mamo
Alex Mamo

Reputation: 139019

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.

Upvotes: 5

Related Questions