We have a system that serves several distribution centers. A distribution center is a physical location that can be located anywhere in the country. The same customer may have several centers. Our system can work in all these centers. As we are now expanding to more locations, we will have to face the problem of different timezones. The same client can also have centers with different timezones.
Various events can be created and stored (with date and time) using our system in a customer's centers. The ideal behavior for different timezones on the same client is as follows:
Given that an event happens in a center that is at timezone A, at noon. If a supervisor of a center that is in timezone B is looking at the date / time of this event, it should view that formatted date / time respecting timezone where the event occurred (including daylight saving time of the event location, if any). That's because what matters is whether an event was locally (at the timezone where it was made) performed at noon (or any other sample time). For the supervisor, it is not important to know that at noon when the event was done it was 2pm where he was.
We used PostgreSQL as our database and I saw that there are two types
of timestamps to save date / time. One is TIMESTAMP
and the other is TIMESTAMPTZ
.
Our entire database uses only the TIMESTAMP
type.
Another scenario that may be very rare to happen but is still possible, is the case of a center being geographically changed, thus impacting the change of your timezone.
According to my research, the best thing to do seems to be saving the timezone each center has in a column in the CENTRO_DISTRIBUICAO
table. Change all types TIMESTAMP
in our database to TIMESTAMPTZ
and at the time of each insert of something that saves date / time use the timezone of the center that is inserting the data to save together the timezone offset (since the TIMESTAMPTZ
does not save the timezone itself, but only the offset).
My question is whether this is the best way and the right way to deal with these different timezones. Since I never implemented anything that needed this support, I can not say.
If I resolve to follow this approach, I'll need to change the type of all columns in the database from TIMESTAMP
to TIMESTAMPTZ
. You'll also need to re-create all the views that depend on those columns as the types are being changed. In addition, you'll need to change all the queries that handle these columns to apply the center timezone using AT TIMEZONE
.
The bank currently has timezone America/Sao_Paulo
and my fear is that you end up doing something wrong by performing the column switching to TIMESTAMPTZ
. Can this cast end up with the consistency of timestamps already stored? What is the correct way to do this, should I first cast or first change the timezone of the bank to UTC
?
Is the solution I described the best way to deal with the problem?
Does this approach also correctly handle daylight saving time for each location?
Extra information: Our server is java (jersey) and the front is both mobile and web.