Commit 5fd6d92
committed
Result set Timestamp handling improvements
DuckDB supports both `TIMESTAMP WITHOUT TIME ZONE` and
`TIMESTAMP WITH TIME ZONE`. When gettting Java `Timestamp` from a
result set, user can optionally request desired time zone by specifying
a `Calendar` instance.
Thus we are getting the following invariants:
1. `TIMESTAMP WITH TIME ZONE`, no `Calendar`: returns
`Timestamp` in a system-default (JVM) time zone that represents the
same moment in time as the DB's `TIMESTAMP WITH TIME ZONE`. For example
with a system-default time zone being `EET` (`UTC+2`) the timestamp with
`12:00` time and `CET` (`UTC+1`) DB time zone will be returned with
`13:00` time (in `EET`).
2. `TIMESTAMP WITH TIME ZONE`, user-specified Calendar: the same logic
is used as in point `1.`, just the time-zone from the user-specified
Calendar is used instead of the system-default time zone.
3. `TIMESTAMP WITHOUT TIME ZONE`, no `Calendar`: returns the timestamp
that has the same numeric values of all components as the DB's
`TIMESTAMP WITHOUT TIME ZONE`. For example the timestamp with `12:00`
time will be returned with the same `12:00` time in any system-defaut
time zone.
4. `TIMESTAMP WITHOUT TIME ZONE`, user-specified `Calendar`: this case
is tricky because user is requesting to adjust the timestamp for the
specified timezone, but DB's timestamp does not denote any time zone
that can be used as a starting point for adjustment. In this case we
are using the same logic as Postgres' JDBC driver:
https://github.com/pgjdbc/pgjdbc/blob/d86d7ce19030230f14e573b8095f04721d1d8e35/pgjdbc/src/main/java/org/postgresql/jdbc/TimestampUtils.java#L1606
So we assume that the incoming time stamp belongs to the
system-default time zone. Then we calculating the offset between the
system-default and the user-requested time zones that is effective at
the moment of time defined by our timestamp in a system-default time
zone. Note, that such offset can be different depending on the time of
year due to DST. And then shifting the timestamp by this offset - this
can seen as a "reverse" shift comparing to points `1.` and `2.`. For
example, with a system-default time zone being `EET` (`UTC+2`) and
user-requested time zone being `CET` (`UTC+1`), the timestamp with time
`12:00` will be shifted by the difference between `EET` and `CET` and
returned as `13:00` (in `CET`).
Additionally the logic added for getting date-only and time-only values
from the `TIMESTAMP_*` fields is added. It is done by getting the Java
`Timestamp` first (using the invariants described above) and then
getting date-only or time-only part of this timestamp.
Testing: existing tests adjusted to match the new time zone handling
logic; new test is added for getting `Date` and `Time` from `TIMESTAMP`.
Fixes: duckdb#1461 parent e8506d9 commit 5fd6d92
File tree
4 files changed
+342
-182
lines changed- src
- main/java/org/duckdb
- test/java/org/duckdb
4 files changed
+342
-182
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
| 34 | + | |
39 | 35 | | |
40 | 36 | | |
41 | 37 | | |
| |||
372 | 368 | | |
373 | 369 | | |
374 | 370 | | |
375 | | - | |
| 371 | + | |
376 | 372 | | |
377 | 373 | | |
378 | 374 | | |
| |||
911 | 907 | | |
912 | 908 | | |
913 | 909 | | |
914 | | - | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
915 | 914 | | |
916 | 915 | | |
917 | 916 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
| 7 | + | |
13 | 8 | | |
14 | 9 | | |
15 | 10 | | |
| |||
38 | 33 | | |
39 | 34 | | |
40 | 35 | | |
41 | | - | |
42 | | - | |
43 | | - | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
44 | 55 | | |
45 | 56 | | |
46 | | - | |
47 | | - | |
48 | | - | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
49 | 62 | | |
50 | 63 | | |
51 | | - | |
52 | | - | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
53 | 77 | | |
54 | 78 | | |
55 | 79 | | |
| |||
78 | 102 | | |
79 | 103 | | |
80 | 104 | | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | 105 | | |
102 | 106 | | |
103 | 107 | | |
| |||
130 | 134 | | |
131 | 135 | | |
132 | 136 | | |
133 | | - | |
| 137 | + | |
134 | 138 | | |
135 | 139 | | |
136 | 140 | | |
| |||
0 commit comments