Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 8B07D1337BF2 for ; Tue, 5 Apr 2011 12:04:41 -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 48893-01-2 for ; Tue, 5 Apr 2011 15:04:22 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from smadev.internal.net (host3.dynacom.ondsl.gr [62.103.35.211]) by mail.postgresql.org (Postfix) with ESMTP id 542451337BF5 for ; Tue, 5 Apr 2011 12:04:21 -0300 (ADT) Received: from smadev.internal.net (localhost.internal.net [127.0.0.1]) by smadev.internal.net (8.14.2/8.14.2) with ESMTP id p35F4Jmb060305 for ; Tue, 5 Apr 2011 18:04:19 +0300 (EEST) (envelope-from achill@matrix.gatewaynet.com) Received: from localhost (localhost [[UNIX: localhost]]) by smadev.internal.net (8.14.2/8.14.2/Submit) id p35F4IBJ060304 for pgsql-jdbc@postgresql.org; Tue, 5 Apr 2011 18:04:18 +0300 (EEST) (envelope-from achill@matrix.gatewaynet.com) From: Achilleas Mantzios Organization: Dynacom Tankers Mgmt To: pgsql-jdbc@postgresql.org Subject: time and timetz : Do I miss something? Date: Tue, 5 Apr 2011 17:04:17 +0200 User-Agent: KMail/1.9.7 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <201104051804.18535.achill@matrix.gatewaynet.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.451 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RCVD_IN_BRBL_LASTEXT=1.449 X-Spam-Level: X-Archive-Number: 201104/26 X-Sequence-Number: 21752 Hello, i was just testing this against a variety of OSs/pgsql vesrions (from 8.4-701 JDBC 3 to 9.0-801 JDBC 4) across both FreeBSD (6.1 -> 8-2) and Linux SLES 10. This is the current time zone (EEST=UTC+3) (local Greek time zone): postgres@smadevnew:~> date Tue Apr 5 17:33:10 EEST 2011 This is the table: dynacom=# \d testts Table "public.testts" Column | Type | Modifiers --------------+-----------------------------+----------- adate | date | atime | time without time zone | atimestamp | timestamp without time zone | atimetz | time with time zone | atimestamptz | timestamp with time zone | dynacom=# SELECT * from testts; adate | atime | atimestamp | atimetz | atimestamptz ------------+----------+---------------------+-------------+------------------------ 2011-04-05 | 00:00:00 | 2011-04-05 00:00:00 | 00:00:00+03 | 2011-04-05 00:00:00+03 (1 row) This is what the following code gives: Class Classdriver = Class.forName("org.postgresql.Driver"); java.sql.Driver driver=(java.sql.Driver)Classdriver.newInstance(); out.println(driver.getMajorVersion()+"."+driver.getMinorVersion()+"
"); out.println("Zone Offset millis="+Calendar.getInstance().getTimeZone().getRawOffset()+"
"); out.println("DST millis="+Calendar.getInstance().getTimeZone().getDSTSavings()+"
"); st = con.prepareStatement("SELECT adate,atime,atimestamp,atimetz,atimestamptz FROM testts"); rs = st.executeQuery(); if (!rs.next()) { rs.close(); st.close(); throw new Exception("data is supposed to be there"); } java.sql.Date sqldate_adate = rs.getDate(1); out.println("sqldate_adate="+sqldate_adate+"
"); SimpleDateFormat simpleDateFormatGTZ = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S Z"); SimpleDateFormat simpleDateFormatGMT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S Z"); simpleDateFormatGMT.setTimeZone(TimeZone.getTimeZone("GMT")); java.sql.Timestamp atimestamp = rs.getTimestamp(3); out.println("atimestamp="+atimestamp+"
"); out.println("atimestamp millis="+atimestamp.getTime()+"
"); out.println("atimestamp GRK format="+simpleDateFormatGTZ.format(atimestamp)+"
"); out.println("atimestamp GMT format="+simpleDateFormatGMT.format(atimestamp)+"
"); java.sql.Timestamp atimestamptz = rs.getTimestamp(5); out.println("atimestamptz="+atimestamptz+"
"); out.println("atimestamptz millis="+atimestamptz.getTime()+"
"); out.println("atimestamptz GRK format="+simpleDateFormatGTZ.format(atimestamptz)+"
"); out.println("atimestamptz GMT format="+simpleDateFormatGMT.format(atimestamptz)+"
"); java.sql.Time atime = rs.getTime(2); out.println("atime="+atime+"
"); out.println("atime millis="+atime.getTime()+"
"); out.println("atime in hours="+((double)atime.getTime()/(1000*3600))+"
"); java.sql.Time atimetz = rs.getTime(4); out.println("atimetz="+atimetz+"
"); out.println("atimetz millis="+atimetz.getTime()+"
"); out.println("atimetz in hours="+((double)atimetz.getTime()/(1000*3600))+"
"); produces: 9.0 Zone Offset millis=7200000 DST millis=3600000 sqldate_adate=2011-04-05 atimestamp=2011-04-05 00:00:00.0 atimestamp millis=1301950800000 atimestamp GRK format=2011-04-05 00:00:00.0 +0300 atimestamp GMT format=2011-04-04 21:00:00.0 +0000 atimestamptz=2011-04-05 00:00:00.0 atimestamptz millis=1301950800000 atimestamptz GRK format=2011-04-05 00:00:00.0 +0300 atimestamptz GMT format=2011-04-04 21:00:00.0 +0000 atime=00:00:00 atime millis=-7200000 atime in hours=-2.0 atimetz=23:00:00 atimetz millis=-10800000 atimetz in hours=-3.0 IMO, the values it gets for the atimestamp (without tz) and atimestamptz (with tz) are sane. I dont supply any Calendar in getTimestamp for the "without tz" timestamp so the driver correctly assumes we are talking about the local Greek time zone. Correctly 2011-04-05 00:00:00.0 +0300=2011-04-04 21:00:00.0 +0000 However i dont quite get the results for atime (without tz) and atimetz (with tz): For atime it correctly prints the time 00:00:00, but the millis should be -10800000, and in hours should be -3.0. For atimetz it incorrectly prints "23:00:00" , but correctly states that the millis=-10800000, and in hours -3.0. Shouldn't at least timetz print the same time as in the case with the timestamptz? The below code prints: st = con.prepareStatement("SELECT atimestamptz::timetz,atimestamptz FROM testts"); rs = st.executeQuery(); atimetz = rs.getTime(1); out.println("atimetz="+atimetz+"
"); atimestamptz = rs.getTimestamp(2); out.println("atimestamptz="+atimestamptz+"
"); atimetz=23:00:00 atimestamptz=2011-04-05 00:00:00.0 Thanx for any thoughts. -- Achilleas Mantzios