E.Akio
E.Akio

Reputation: 2388

How to retrieve a Date on Firestore without updating it with Time Zone?

I have the following list on Firestore:

enter image description here

"schedules" display to users the dates they can make an appointment. But when the user has a different Time Zone it'll automatically update the hour. For example, someone living in Germany would receive the list 4 or 5 hours forward (06:00 would go to 10:00, 09:00 to 13:00, 11:00 to 15:00, ...). So my question is how is it possible to not interfere on the Date, not changing it's time, just to receive "the original" date it was stored?

Upvotes: 1

Views: 354

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

But when the user has a different Time Zone it'll automatically update the hour. For example, someone living in Germany would receive the list 4 or 5 hours forward

Firestore timestamps stores only the offset from the Unix epoch with a nanoseconds precision. It doesn't have any timezone encoded into them, that's why you have this behavior. Not only in Firestore but in many other databases the timestamp objects don't care about the timezone, it's all about a "point in time".

To check this out, please open Firebase console and check the value of a timestamp property. What you'll see is that the time is displayed using the local timezone that your device is apart of. Please also note that it doesn't matter in which timezone the client is. A "point in time" is the same no matter where the user is around the world.

If you are converting in your Android application the timestamp to a Date object, that date object will be converted itself in the user device's local timezone, similar to what is happening in the console.

If you want to get the Date object that corresponds to a specific timezone, you should use some specialized classes such as TimeZone. Here you have a list of all available time zones.

Upvotes: 2

Related Questions