Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id DD6E1133616B for ; Sun, 19 Sep 2010 18:28:27 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 42325-07 for ; Sun, 19 Sep 2010 21:28:21 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from tremor.opencloud.co.nz (tremor.opencloud.co.nz [131.203.101.230]) by mail.postgresql.org (Postfix) with ESMTP id 6513C1336018 for ; Sun, 19 Sep 2010 18:28:20 -0300 (ADT) Received: from 121-98-80-66.bitstream.orcon.net.nz ([121.98.80.66] helo=[192.168.1.2]) by tremor.opencloud.co.nz with esmtpsa (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.69) (envelope-from ) id 1OxRQl-000748-NE; Mon, 20 Sep 2010 09:28:15 +1200 Message-ID: <4C968069.4050801@opencloud.com> Date: Mon, 20 Sep 2010 09:28:09 +1200 From: Oliver Jowett User-Agent: Thunderbird 2.0.0.24 (X11/20100411) MIME-Version: 1.0 To: Lukas Eder CC: pgsql-jdbc@postgresql.org Subject: Re: java.sql.ResultSet.getTime() returns wrong time References: In-Reply-To: X-Enigmail-Version: 0.95.7 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201009/37 X-Sequence-Number: 20907 Lukas Eder wrote: > I have experienced a very peculiar issue with the postgresql JDBC driver > when calling java.sql.ResultSet.getTime() or getTimestamp() on a field > of type timetz. > > Here is how to reproduce the issue: > > 1. Set date and time to 2010-09-19 14:57:00 CEST (central european > summer time: UTC+2) or something similar > 2. Fetch "SELECT current_time" from the Postgres database directly. > This will return the correct time, e.g "14:57:17.116452+02" > 3. Fetch "SELECT current_time" from the Postgres JDBC driver. This > will return a wrong time, e.g 13:57:17 The problem is this: there is no information that says the time string that you are converting is covered by a particular set of timezone rules. The only information given to the driver is that it is a time with a fixed offset of +0200. So the returned time is a representation of 1970-01-01 14:57:17 +0200, NOT 1970-01-01 14:57:17 in your local timezone. When java.sql.Time then applies the local timezone, you are in essence asking "What time is 1970-01-01 14:57:17 +0200 in the local timezone?" to which the answer is "13:57:17". Using the current date instead of 1970-01-01 is explicitly wrong for java.sql.Time (see its javadoc), and is conceptually wrong for timestamps: how do you know the JVM's current timezone rules are applicable to that particular time? Consider the case where you stored current_time in a timetz column, then retrieved it 6 months later after the local daylight savings rules changed. Did you see Kris's earlier response here? See http://archives.postgresql.org/pgsql-jdbc/2010-05/msg00052.php. The problem is we need to pass around a timezone offset, but JDBC + java.util.Date give us no way to do that without subclassing those types (which seems a bit hairy). Without that extra data, timetz just doesn't map well to any of the standard Java date/time types. -O