8
We have a system that serves several distribution centers. A distribution center is a physical location that can be located anywhere in the country. A single customer can have several centers. Our system can operate in all these centers. As we are now expanding to more locations, we will have to face the problem of different timezones. A same customer can also have centers with different timezones.
Several events can be created and stored (date and time) using our system in a customer’s center. The ideal behavior for different timezones in the same client is as follows:
Given that an event takes place in a center that is in Timezone A, at noon. If a supervisor of a center that is in Timezone B to look at the date/time of this event, it must view this date/time formatted respecting the Timezone where the event took place (including the daylight saving time of the event venue, if any). This is because what matters is whether an event was held locally (in the Timezone where it was made) at noon (or any other example time). For the supervisor, it is not important to know that at noon when the event was done was 14h where he was.
We use Postgresql as our database and I saw that there are two types
timestamps to save date/time. One of them is the TIMESTAMP
and the other is TIMESTAMPTZ
.
Our entire database uses only the type TIMESTAMP
.
Another scenario that may be quite rare to happen but is still possible, is the case of a center to be changed geographically, with this impacting on the change of its Timezone.
According to my research, the most correct thing to do seems to be to save the Timezone that each center has in a column in the table CENTRO_DISTRIBUICAO
. Trade all kinds TIMESTAMP
in our bank to TIMESTAMPTZ
and at the time of each input of something that saves date/time use the Timezone of the center that is entering the data to save along the offset of the Timezone (since the field TIMESTAMPTZ
does not save the Timezone itself, but only the offset).
My question is whether this is really the best way and the right way to deal with these different timezones. Since I never implemented anything that needed this support, I can’t say.
If I decide to follow this approach, we will need to change the type of all columns in the TIMESTAMP
for TIMESTAMPTZ
. You will also need to recreate all the views that depend on these columns, as the types are being changed. Also, you’ll need to change all queries that handle these columns to apply the center Timezone using AT TIMEZONE
.
The bank currently has as Timezone America/Sao_Paulo
and my fear is to end up doing something wrong while performing the exchange of the columns to TIMESTAMPTZ
. Can this cast end the consistency of timestamps already stored? Which is the correct way to do this, I must first cast or first change the bank Timezone to UTC
?
The solution I described is the best way to deal with the problem?
This approach also deals correctly with each place’s daylight saving time?
Extra information: our server is java (jersey) and the front is both mobile and web.
I’ve been through a similar problem, followed this answer, and is here too
– Robss70
Thanks for the suggestions of posts. I had already consulted one of them. Infezlimente, they did not help much in my case.
– Luiz