How-to: Time zones
An application whose users, warehouses or branches are in several time zones works with two kinds of values. A moment, such as when a shipment left the warehouse, is the same for everybody and is stored as ZDATETIME. A local date and time, what the clock of a particular place shows, is stored as DATETIME and denotes a moment only together with a time zone. The conversion between the two and currentDateTime[] use the time zone of the current locale: the user's time zone or, in an action initiated by the system, the server's. The properties of the Time module that take the name of a time zone use that zone instead.
The examples use warehouses and shipments:
REQUIRE Time;
CLASS Stock 'Warehouse';
name 'Name' = DATA ISTRING[100] (Stock) IN id;
// a name of the IANA time zone database, such as Europe/Berlin
timeZone 'Time zone' = DATA STRING[50] (Stock);
CLASS Shipment 'Shipment';
stock 'Warehouse' = DATA Stock (Shipment) NONULL DELETE;
shipped 'Shipped' = DATA ZDATETIME (Shipment) INDEXED;
Example 1
Task
When a shipment is created, we need to record the moment of its creation, so that the records made by users in different time zones and by the scheduler can be compared, and every user sees the moment in the user's own time.
Solution
created 'Created' = DATA ZDATETIME (Shipment);
// currentZDateTime() is the moment itself. currentDateTime() written to a DATETIME property
// would be the local date and time of whoever creates the shipment
WHEN SET(Shipment s IS Shipment) DO
created(s) <- currentZDateTime();
currentZDateTime[] returns the moment itself, so the value does not depend on who creates the shipment. A form shows a ZDATETIME property in the time zone of the user who has opened it, and the conversion DATETIME(created(s)) gives the local date and time in the time zone of the current locale.
currentDateTime[] written to a DATETIME property would keep the local date and time of whoever created the shipment: for one and the same moment 18:04 from a user in Almaty, 08:04 from a user in New York and the server's time from a scheduler task, and a stored value no longer tells which moment it was.
Example 2
Task
Every warehouse works by its own clock. We need the local date and time at which a shipment left its warehouse, the date of that day by the warehouse's calendar, and the current date and time of a warehouse.
Solution
localShipped 'Shipped, warehouse time' (Shipment s) = toDateTime(shipped(s), timeZone(stock(s)));
localDate 'Shipping date' (Shipment s) = toDate(localShipped(s));
now 'Warehouse time' (Stock st) = currentDateTime(timeZone(st));
today 'Warehouse date' (Stock st) = currentDate(timeZone(st));
toDateTime[ZDATETIME, STRING] returns the local date and time the moment has in the given time zone, whoever reads it. today[Stock] is the date of now[Stock], whereas currentDate[] is the date of the server in the server's time zone, the same for every warehouse.
The time zone is given by its name in the IANA time zone database. An offset is not a replacement for it: UTC+5 is read by PostgreSQL as five hours west of Greenwich. For a warehouse whose time zone is not set, the four properties are NULL, and the time zone of the current locale is not used instead. With a name the DBMS does not know, a query that reads them fails.
Example 3
Task
A shipment is planned by the clock of its warehouse: the planned date and time are entered as that clock will show them. We need to store the moment of the planned shipment. A warehouse also stops taking orders for the same day at a time of its own, and we need to know whether that time has come.
Solution
planned 'Planned' = DATA ZDATETIME (Shipment);
plan 'Plan' (Shipment s, DATETIME local) {
planned(s) <- toZDateTime(local, timeZone(stock(s)));
}
cutOffTime 'Cut-off time' = DATA TIME (Stock);
cutOff 'Cut-off' (Stock st, DATE d) = toZDateTime(toDateTime(d, cutOffTime(st)), timeZone(st));
closedForToday 'Closed for today' (Stock st) = currentZDateTime() >= cutOff(st, today(st));
toZDateTime[DATETIME, STRING] returns the moment at which the clock of the given time zone shows the given date and time. On a day when the clocks are moved forward, a time that the clock skips is moved forward by the length of the gap, and on a day when they are moved back, a time that the clock shows twice gives the later of the two moments.
cutOff[Stock, DATE] composes the local date and time from the date and the time of the warehouse and converts them in the same way, and closedForToday[Stock] compares two moments, which does not involve any time zone.
Example 4
Task
We need the number of shipments that left a warehouse on a given date of the warehouse's calendar. There are many shipments.
Solution
countShipments 'Number of shipments' (Stock st, DATE d) = GROUP SUM 1 IF stock(Shipment s) = st AND
shipped(s) >= toZDateTime(d, timeZone(st)) AND shipped(s) < toZDateTime(sum(d, 1), timeZone(st));
A day of a time zone lasts from the moment its date starts to the moment the next date starts, and toZDateTime[DATE, STRING] returns both: it is 24 hours, and less or more on a day when the clocks are moved. The stored moment of a shipment is compared with these two moments as it is, so the DBMS can select the shipments by the index on shipped[Shipment]. A condition on the converted value, localDate(s) = d, gives that index no bounds to select by.