[libpostgresql-jdbc-java] 11/13: Parse timezones that have offsets in seconds. 8.2 servers now return this information so we must be able to handle it.

Emmanuel Bourg ebourg-guest at moszumanska.debian.org
Mon Jan 9 10:20:23 UTC 2017


This is an automated email from the git hooks/post-receive script.

ebourg-guest pushed a commit to tag REL8_2_505
in repository libpostgresql-jdbc-java.

commit 4cea0388d5346da526bb3eb7b22d0ff44ca5719f
Author: Kris Jurka <books at ejurka.com>
Date:   Mon Apr 16 18:31:44 2007 +0000

    Parse timezones that have offsets in seconds.  8.2 servers now
    return this information so we must be able to handle it.
---
 org/postgresql/jdbc2/AbstractJdbc2Connection.java |  4 +-
 org/postgresql/jdbc2/TimestampUtils.java          | 52 ++++++++++++++++-------
 org/postgresql/test/jdbc2/TimezoneTest.java       | 23 ++++++++--
 3 files changed, 58 insertions(+), 21 deletions(-)

diff --git a/org/postgresql/jdbc2/AbstractJdbc2Connection.java b/org/postgresql/jdbc2/AbstractJdbc2Connection.java
index 561b3ee..58e158f 100644
--- a/org/postgresql/jdbc2/AbstractJdbc2Connection.java
+++ b/org/postgresql/jdbc2/AbstractJdbc2Connection.java
@@ -3,7 +3,7 @@
 * Copyright (c) 2004-2005, PostgreSQL Global Development Group
 *
 * IDENTIFICATION
-*   $PostgreSQL: pgjdbc/org/postgresql/jdbc2/AbstractJdbc2Connection.java,v 1.39 2006/11/05 06:14:18 jurka Exp $
+*   $PostgreSQL: pgjdbc/org/postgresql/jdbc2/AbstractJdbc2Connection.java,v 1.40 2006/12/01 08:53:45 jurka Exp $
 *
 *-------------------------------------------------------------------------
 */
@@ -151,7 +151,7 @@ public abstract class AbstractJdbc2Connection implements BaseConnection
         }
 
         // Initialize timestamp stuff
-        timestampUtils = new TimestampUtils(haveMinimumServerVersion("7.4"));
+        timestampUtils = new TimestampUtils(haveMinimumServerVersion("7.4"), haveMinimumServerVersion("8.2"));
 
         // Initialize common queries.
         commitQuery = getQueryExecutor().createSimpleQuery("COMMIT");
diff --git a/org/postgresql/jdbc2/TimestampUtils.java b/org/postgresql/jdbc2/TimestampUtils.java
index 62848cf..a89e64d 100644
--- a/org/postgresql/jdbc2/TimestampUtils.java
+++ b/org/postgresql/jdbc2/TimestampUtils.java
@@ -3,7 +3,7 @@
 * Copyright (c) 2003-2005, PostgreSQL Global Development Group
 *
 * IDENTIFICATION
-*   $PostgreSQL: pgjdbc/org/postgresql/jdbc2/TimestampUtils.java,v 1.19.2.1 2007/01/05 00:34:13 jurka Exp $
+*   $PostgreSQL: pgjdbc/org/postgresql/jdbc2/TimestampUtils.java,v 1.19.2.2 2007/04/16 17:05:55 jurka Exp $
 *
 *-------------------------------------------------------------------------
 */
@@ -33,15 +33,17 @@ public class TimestampUtils {
     private Calendar calCache;
     private int calCacheZone;
 
-    private boolean min74;
+    private final boolean min74;
+    private final boolean min82;
 
-    TimestampUtils(boolean min74) {
+    TimestampUtils(boolean min74, boolean min82) {
         this.min74 = min74;
+        this.min82 = min82;
     }
 
-    private Calendar getCalendar(int sign, int hr, int min) {
-        int unified = sign * (hr * 100 + min);
-        if (calCache != null && calCacheZone == unified)
+    private Calendar getCalendar(int sign, int hr, int min, int sec) {
+        int rawOffset = sign * (((hr * 60 + min) * 60 + sec) * 1000);
+        if (calCache != null && calCacheZone == rawOffset)
             return calCache;
                 
         StringBuffer zoneID = new StringBuffer("GMT");
@@ -50,10 +52,12 @@ public class TimestampUtils {
         zoneID.append(hr);
         if (min < 10) zoneID.append('0');
         zoneID.append(min);
+        if (sec < 10) zoneID.append('0');
+        zoneID.append(sec);
         
-        TimeZone syntheticTZ = TimeZone.getTimeZone(zoneID.toString());
+        TimeZone syntheticTZ = new SimpleTimeZone(rawOffset, zoneID.toString());
         calCache = new GregorianCalendar(syntheticTZ);
-        calCacheZone = unified;
+        calCacheZone = rawOffset;
         return calCache;
     }
         
@@ -188,7 +192,7 @@ public class TimestampUtils {
             sep = charAt(s, start);
             if (sep == '-' || sep == '+') {
                 int tzsign = (sep == '-') ? -1 : 1;
-                int tzhr, tzmin;
+                int tzhr, tzmin, tzsec;
     
                 end = firstNonDigit(s, start+1);    // Skip +/-
                 tzhr = number(s, start+1, end);
@@ -202,11 +206,21 @@ public class TimestampUtils {
                 } else {
                     tzmin = 0;
                 }
-               
+
+                tzsec = 0;
+                if (min82) {
+                    sep = charAt(s, start);
+                    if (sep == ':') {
+                        end = firstNonDigit(s, start+1);  // Skip ':'
+                        tzsec = number(s, start+1, end);
+                        start = end;
+                    }
+                }
+
                 // Setting offset does not seem to work correctly in all
                 // cases.. So get a fresh calendar for a synthetic timezone
                 // instead
-                result.tz = getCalendar(tzsign, tzhr, tzmin);
+                result.tz = getCalendar(tzsign, tzhr, tzmin, tzsec);
 
                 start = skipWhitespace(s, start);  // Skip trailing whitespace
             }
@@ -532,13 +546,14 @@ public class TimestampUtils {
         sb.append(decimalStr, 0, 6);
     }
 
-    private static void appendTimeZone(StringBuffer sb, java.util.Calendar cal)
+    private void appendTimeZone(StringBuffer sb, java.util.Calendar cal)
     {
-        int offset = (cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET)) / 1000 / 60;
+        int offset = (cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET)) / 1000;
 
         int absoff = Math.abs(offset);
-        int hours = absoff / 60;
-        int mins = absoff - hours * 60;
+        int hours = absoff / 60 / 60;
+        int mins = (absoff - hours * 60 * 60) / 60;
+        int secs = absoff - hours * 60 * 60 - mins * 60;
 
         sb.append((offset >= 0) ? " +" : " -");
 
@@ -551,6 +566,13 @@ public class TimestampUtils {
         if (mins < 10)
             sb.append('0');
         sb.append(mins);
+
+        if (min82) {
+            sb.append(':');
+            if (secs < 10)
+                sb.append('0');
+            sb.append(secs);
+        }
     }
 
     private static void appendEra(StringBuffer sb, Calendar cal)
diff --git a/org/postgresql/test/jdbc2/TimezoneTest.java b/org/postgresql/test/jdbc2/TimezoneTest.java
index b1e1aa2..a368a1d 100644
--- a/org/postgresql/test/jdbc2/TimezoneTest.java
+++ b/org/postgresql/test/jdbc2/TimezoneTest.java
@@ -3,7 +3,7 @@
 * Copyright (c) 2005, PostgreSQL Global Development Group
 *
 * IDENTIFICATION
-*   $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/TimezoneTest.java,v 1.2 2005/08/01 23:30:01 oliver Exp $
+*   $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/TimezoneTest.java,v 1.3 2005/09/14 00:06:19 jurka Exp $
 *
 *-------------------------------------------------------------------------
 */
@@ -646,10 +646,25 @@ public class TimezoneTest extends TestCase
 
     public void testHalfHourTimezone() throws Exception
     {
-        // Check our parser handles a timezone like e.g. 03:30 correctly
-        ResultSet rs = con.createStatement().executeQuery("SELECT '1969-12-31 20:30:00-03:30'::text");
-
+        Statement stmt = con.createStatement();
+        stmt.execute("SET TimeZone = 'GMT+3:30'");
+        ResultSet rs = stmt.executeQuery("SELECT '1969-12-31 20:30:00'::timestamptz");
         assertTrue(rs.next());
         assertEquals(0L, rs.getTimestamp(1).getTime());
     }
+
+    public void testTimezoneWithSeconds() throws SQLException
+    {
+        if (!TestUtil.haveMinimumServerVersion(con, "8.2"))
+            return;
+
+        Statement stmt = con.createStatement();
+        stmt.execute("SET TimeZone = 'Europe/Helsinki'");
+        ResultSet rs = stmt.executeQuery("SELECT '1920-01-01'::timestamptz");
+        rs.next();
+        // select extract(epoch from '1920-01-01'::timestamptz - 'epoch'::timestamptz) * 1000;
+
+        assertEquals(-1577929192000L, rs.getTimestamp(1).getTime());
+    }
+
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/libpostgresql-jdbc-java.git



More information about the pkg-java-commits mailing list