Dalon
Dalon

Reputation: 700

Is it possible to save a specific date to firestore without timezone?

In firestore dates are stored as timestamps so they are not related to a specific date (for example 03/05/2021) right? But in my App its important that the data I save is related to the specific date it was saved and not to a timestamp. So that its irrelevant in what timezone i am.

For example when I saved something on 03/05/2021 at 01:29:00 UTC+2 it should be this fixed date no matter in what timezone I am. Because when I read this date in Timezone UTC+0 its not anymore the 03/05/2021.

Is there a way to tell firestore that the specific date is relevant?

Upvotes: 0

Views: 543

Answers (1)

Guillem Puche
Guillem Puche

Reputation: 1404

See the Firestore's API docs

You can use timestamp at nanoseconds precision.

Read the Timestamp class definition:

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. It is encoded assuming all minutes are 60 seconds long, i.e. leap seconds are "smeared" so that no leap second table is needed for interpretation. Range is from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z. By restricting to that range, we ensure that we can convert to and from RFC 3339 date strings.

Then add a document to your Firestore collection using the Timestamp class:

CollectionReference users = FirebaseFirestore.instance.collection('users');

Future<void> updateUser() {
  return users
    .doc('your_collection')
    .update({'your_time': Timestamp(<put_here_your_timestamp>)})
    .then((value) => print("User Updated"))
    .catchError((error) => print("Failed to update: $error"));
}

To understand what format has to have the Timestamp of Firestore, read Google Protocol Buffer for timestamps (it's the Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data.)

To understand what format has to have the Timestamp of Firestore, read Google Protocol Buffer for timestamps (it's the Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data.)

Also, when you save a timestamp like year-month-dayT23:59:59.999999999Z, it can produce misunderstandings with timezones. A good thing to do is to use the epoch format, then in your app you will convert it to a user-friendly data format. As Google says on Protocol Buffer:

A Timestamp represents a point in time independent of any time zone or local calendar, encoded as a count of seconds and fractions of seconds at nanosecond resolution. The count is relative to an epoch at UTC midnight on January 1, 1970, in the proleptic Gregorian calendar which extends the Gregorian calendar backwards to year one.

Upvotes: 1

Related Questions