X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id DE23852BF3 for ; Sun, 24 Jul 2005 18:58:51 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 81584-09 for ; Sun, 24 Jul 2005 21:58:44 +0000 (GMT) Received: from wproxy.gmail.com (wproxy.gmail.com [64.233.184.207]) by svr1.postgresql.org (Postfix) with ESMTP id DE617529B8 for ; Sun, 24 Jul 2005 18:58:42 -0300 (ADT) Received: by wproxy.gmail.com with SMTP id i34so834858wra for ; Sun, 24 Jul 2005 14:58:45 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition; b=VtMvZr/AUBIQaZHoC+OhnKkZtijEoduBc9VM1Q4+P8NwCrmos4tsGsmu6oBPwdZqE/VVZYKm3WiTL+qQVS97iK5ocUki3nki1NTTiVl/XqdcUvBKf452/lX+zxn5yg6mCnyn+E3QUU34wYzvvgf+Akyfhjj6uXp+6hmiBVnrzHQ= Received: by 10.54.13.37 with SMTP id 37mr2007333wrm; Sun, 24 Jul 2005 14:58:44 -0700 (PDT) Received: by 10.54.143.14 with HTTP; Sun, 24 Jul 2005 14:58:44 -0700 (PDT) Message-ID: Date: Sun, 24 Jul 2005 23:58:44 +0200 From: "emergency.shower@gmail.com" Reply-To: "emergency.shower@gmail.com" To: pgsql-jdbc@postgresql.org Subject: Timestamp weirdness Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.117 tagged_above=0 required=5 tests=AWL, RCVD_BY_IP X-Spam-Level: X-Archive-Number: 200507/274 X-Sequence-Number: 13422 Hi, With PostgreSQL 8.0.3 and the postgresql-jdbc-8.1dev-400 JDBC driver, there are a number of problems when writing timestamps to and reading them from the database. First of all, I would expect PreparedStatemant#setTimestamp("fld", Timestamp, Calendar) and ResultSet#getTimestamp("fld", Calendar) to be complementary methods. When writing a java.sql.Timestamp to a TIMESTAMP WITHOUT TIME ZONE or to a TIMESTAMP WITH TIME ZONE database field and then reading it using the same java.util.Calendar should return the same Timestamp value that was previously written to the database. However, if final java.sql Timestamp ts; final java.util.Calendar cal; ts =3D new Timestamp(0L); ts.setNanos(0); cal =3D Calendar.getInstance(TimeZone.getTimeZone("UTC")); then writing with PreparedStatemant#setTimestamp("fld", ts, cal) and reading with ResultSet#getTimestamp("fld", Calendar) will give a result that differs from the original value by the server's local time zone offset if "fld" is TIMESTAMP WITHOUT TIME ZONE. When reading/writing to a TIMESTAMP WITH TIME ZONE the correctness of the result depends on the server's time zone. E.g. if the server has the Newfoundland time zone the result differs from the correct one by 30 minutes. If we use a Calendar other than UTC, with a non-zero time zone offset, the results are generally incorrect and difficult to predict. They seem to depend on - the database servers local time zone, - the the Calendar's time zone offset, - the database field's WITH or WITHOUT TIME ZONE attribute, - and rounding errors if the Calendar's time zone offset contains fractions of hours. Here's the behaviour that I would expect: 1) The values in the database should not depend on the database's local time zone. 2) When writing to a TIMESTAMP WITH TIME ZONE field, the driver should not perform any time zone conversions and should store the Timestamp's (UTC) y, M, d, H, m, s values directly to the database. The Calendar, if given, should be ignored. 3) When writing to a TIMESTAMP WITHOUT TIME ZONE field, the driver should calculate the Timestamp's y, M, d, H, m, s values in the given Calendar's time zone and should store these values in the database. 4) When reading from a TIMESTAMP WITH TIME ZONE field, the driver should create a Timestamp by interpreting the y, M, d, H, m, s values as UTC timestamp fields. The Calendar, if given, should be ignored. 5) When reading from a TIMESTAMP WITHOUT TIME ZONE field, the driver should create a Timestamp by interpreting the y, M, d, H, m, s values in the context of the given Calendar. Does this make sense? Regards, Alex