Home > Enterprise >  Can you use firebase-admin.firebase.Timestamp instead of FieldValue.serverTimestamp() in Firebase fu
Can you use firebase-admin.firebase.Timestamp instead of FieldValue.serverTimestamp() in Firebase fu

Time:12-10

For generating server side timestamps in Google Cloud functions it seems like the documentation tends to point to using admin.firestore.FieldValue.serverTimestamp().

But there is also an option to use import {Timestamp} from "firebase-admin/firestore" which has a function Timestamp.now(). Seems a much easier way to get at the current time on the server than using admin.firestore.FieldValue.serverTimestamp(). Do you end up with the same time either way?

CodePudding user response:

Firestore timestamps are generated when the document is modified/created on the database server. Other timestamps can be used for logic, but those are independent of Firestore timestamps.

ref.set({
    createdAt: firebase.firestore.FieldValue.serverTimestamp()
})

CodePudding user response:

There is no guarantee that the timestamp on the server that executes the Cloud Function will be exactly the same as the time when the data is written to the database.

The two operations happen on different machines, so even if the clocks were complete in sync, there's time that passes between executing the operations on those different machines. When the machines are in different zones/regions, that time difference may well be non-trivial.

This level of difference may of course not be important for your application, but that's something only you can determine. I recommend measuring the difference between the two values in your project before deciding.

  • Related