Time zone setting and time zone library
Timezone setting
Section titled “Timezone setting”Two system parameters control time zones (see Time zone parameters):
| Parameter | Applies to | Default |
|---|---|---|
| timezone | A session | The value of server_timezone |
| server_timezone | The database server | The operating system's time zone |
A session changes its own time zone with SET TIME ZONE or by setting the parameter:
SET TIME ZONE 'Europe/Bucharest';SET TIME ZONE '+02:00';SET SYSTEM PARAMETERS 'timezone=Europe/Bucharest';The session time zone affects the types TIMESTAMP, TIMESTAMPLTZ and DATETIMELTZ, the conversion of values without time zone to the time zone types, and functions such as SYS_DATETIME. See Date/time types with time zone and Time zone functions.
Time zones written as region names (Europe/Bucharest) need the time zone library, which contains the IANA time zone database. A library is installed with CoraDB. Because governments change offsets and daylight-saving rules, update the library from time to time as described below. Offsets such as +02:00 do not need the library.
Compiling timezone library
Section titled “Compiling timezone library”To update the time zone library with the latest IANA data, stop the CoraDB service, then:
Windows
Section titled “Windows”-
Download the latest Time Zone Data (
tzdata*.tar.gz) from https://www.iana.org/time-zones. -
Extract it into
%CORADB%\timezones\tzdata. -
Run
make_tz_x64.batfrom%CORADB%\bin. It createslibcoradb_timezones.dllin%CORADB%\lib.make_tz_x64.bat
The script needs Microsoft Visual Studio with the C++ tools, like make_locale.
-
Download the latest Time Zone Data from https://www.iana.org/time-zones.
-
Extract it into
$CORADB/timezones/tzdata. -
Run
make_tz.sh. It createslibcoradb_timezones.soin$CORADB/lib.make_tz.sh
make_tz.sh -h shows the options: -t target (64-bit), -m release|debug, and -g new|extend. On Windows the corresponding arguments are /release, /debug, /new and /extend.
Timezone library and database compatibility
Section titled “Timezone library and database compatibility”The library contains an MD5 checksum of its time zone data. Every database stores the checksum of the library it was created with, in the timezone_checksum column of db_root. A database cannot be used with a library whose checksum differs: after rebuilding the library with new data, either create new databases, or use the extend mode, which keeps existing databases usable:
- It builds a library that merges the old and the new data: regions that no longer exist in the new data are kept, so that values stored in the database remain valid.
- It updates values stored in the databases where a changed offset or daylight-saving rule makes the old encoding wrong, and stores the new checksum.
make_tz.sh -g extend # Linuxmake_tz_x64.bat /extend # WindowsThe extend mode processes every database listed in databases.txt (in $CORADB_DATABASES), by running coradb gen_tz -g extend <database> for each. If one fails, the script stops, and databases already processed may need to be restored from backup. Take a backup before you start.
When several users share one CoraDB installation, the extend can only update the databases that the user running it can access and that are listed in that user's databases.txt. Either give that user access to all database directories and the same CORADB_DATABASES, or run the extend once per user:
- Back up the time zone library and
databases.txt; - Remove from
databases.txtall databases except the current user's; - Run the extend;
- Restore
databases.txtand, for all users except the last, the time zone library.
Usage of timezone data types with JDBC
Section titled “Usage of timezone data types with JDBC”The JDBC driver takes all time zone information from the CoraDB server. Java and CoraDB both use IANA data, but region names and rules may differ between versions, so do not mix Java's time zone handling with CoraDB's.
Values of all time zone types are returned as coradb.sql.CORADBTimestamptz objects. To insert one:
String datetime = "2000-01-01 01:02:03.123";CORADBTimestamptz tz = CORADBTimestamptz.valueOf(datetime, true, "Europe/Kiev"); // true: DATETIME precisionPreparedStatement ps = conn.prepareStatement("INSERT INTO event_log VALUES (?)");ps.setObject(1, tz);ps.executeUpdate();To read one:
ResultSet rs = conn.createStatement().executeQuery("SELECT happened_at FROM event_log");while (rs.next()) { CORADBTimestamptz value = (CORADBTimestamptz) rs.getObject(1); System.out.println(value);}A CORADBTimestamptz stores its local date and time as if they were UTC, in milliseconds like java.util.Date, and keeps the time zone separately. getTime() therefore returns the wall-clock value encoded as UTC, while getUnixTime() returns the real moment, in seconds since the Unix epoch, taking the time zone into account:
CORADBTimestamptz tz = CORADBTimestamptz.valueOf("2000-01-01 01:02:03.123", true, "Asia/Seoul");System.out.println(tz.getTime());System.out.println(tz.getUnixTime());