Received: from ns.gcom.ru (Garant-Siberia-PostOffice.gcom.ru [194.84.180.33]) by hub.org (8.9.1/8.9.1) with ESMTP id FAA29331 for ; Tue, 27 Oct 1998 05:42:16 -0500 (EST) (envelope-from avd@avd.garsib.ru) Received: (from uucp@localhost) by ns.gcom.ru (8.6.10/8.6.10) id QAA08216; Tue, 27 Oct 1998 16:40:45 +0600 Received: from avd.garsib.ru(194.135.7.80) via SMTP by Garant-Siberia-PostOffice.gcom.ru, id smtpd008212; Tue Oct 27 16:40:42 1998 Received: (from avd@localhost) by avd.garsib.ru (8.8.8/8.8.8) id RAA00303; Tue, 27 Oct 1998 17:51:32 +0600 (NS) (envelope-from avd) To: pgsql-interfaces@postgresql.org cc: Peter T Mount Subject: JDBC, Timestamps, and DateStyle From: Aleksey Demakov Date: 27 Oct 1998 17:51:31 +0600 Message-ID: <87ww5mrznf.fsf@avd.garsib.ru> Lines: 78 X-Mailer: Gnus v5.5/Emacs 20.2 I had problems reading datetime fields with the ResultSet.getTimestamp method. First, it looks like this method always assumes that DateStyle is ISO. At the same time the driver checks current DateStyle in the case of Dates and acts accordingly. IMHO, it's a bit incosistent. As I recall the ODBC driver has no such problems because it always uses the same format for communication with backend. It sets it when it opens a connection. But it is not my main problem since I can set DateStyle by myself. The other problem is that the driver simetimes fails to parse timestamps. I found two reasons for this. The first is that the timestamp can contain fractions of second. The second relates to timezones. I don't quite understand it, though. I simply rewrote the code so it works for me. And I believe it should also work for others. The patch is below. Aleksey *** ResultSet.java.old Thu Sep 3 14:00:49 1998 --- ResultSet.java Tue Oct 27 16:21:53 1998 *************** *** 448,463 **** public Timestamp getTimestamp(int columnIndex) throws SQLException { String s = getString(columnIndex); ! SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:sszzz"); ! if (s != null) { ! int TZ = new Float(s.substring(19)).intValue(); ! TZ = TZ * 60 * 60 * 1000; ! TimeZone zone = TimeZone.getDefault(); ! zone.setRawOffset(TZ); ! String nm = zone.getID(); ! s = s.substring(0,18) + nm; try { java.util.Date d = df.parse(s); return new Timestamp(d.getTime()); --- 448,477 ---- public Timestamp getTimestamp(int columnIndex) throws SQLException { String s = getString(columnIndex); ! SimpleDateFormat df; if (s != null) { ! int i = 19; ! if (s.charAt(i) == '.') ! { ! i++; ! while(Character.isDigit(s.charAt(i))) ! i++; ! df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSzzz"); ! } ! else ! df = new SimpleDateFormat("yyyy-MM-dd HH:mm:sszzz"); ! ! int TZ = new Float(s.substring(i)).intValue(); ! String sign; ! if (TZ < 0) ! { ! sign = "-"; ! TZ = -TZ; ! } ! else ! sign = "+"; ! s = s.substring(0,i) + "GMT" + sign + TZ/10 + TZ%10 + ":00"; try { java.util.Date d = df.parse(s); return new Timestamp(d.getTime()); -- Aleksey Demakov avd@gcom.ru