You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2016/05/18 18:06:28 UTC

svn commit: r1744442 - /pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/util/DateConverter.java

Author: tilman
Date: Wed May 18 18:06:28 2016
New Revision: 1744442

URL: http://svn.apache.org/viewvc?rev=1744442&view=rev
Log:
PDFBOX-3352: set zone id

Modified:
    pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/util/DateConverter.java

Modified: pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/util/DateConverter.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/util/DateConverter.java?rev=1744442&r1=1744441&r2=1744442&view=diff
==============================================================================
--- pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/util/DateConverter.java (original)
+++ pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/util/DateConverter.java Wed May 18 18:06:28 2016
@@ -486,7 +486,7 @@ public class DateConverter
         {                    // we parsed a time zone in default format
             int hrSign = (sign == '-' ? -1 :+1);
             tz.setRawOffset(restrainTZoffset(hrSign*(tzHours*MILLIS_PER_HOUR + tzMin*MILLIS_PER_MINUTE))); 
-            tz.setID("unknown");
+            updateZoneId(tz);
         }
         else if ( ! hadGMT)
         {            // try to process as a name; "GMT" or "UTC" has already been processed
@@ -509,6 +509,43 @@ public class DateConverter
     }
     
     /**
+     * Update the zone ID based on the raw offset. This is either GMT, GMT+hh:mm or GMT-hh:mm, where
+     * n is between 1 and 14. The highest negative hour is -14, the highest positive hour is 12.
+     * Zones that don't fit in this schema are set to zone ID "unknown".
+     *
+     * @param tz the time zone to update.
+     */
+    private static void updateZoneId(TimeZone tz)
+    {
+        // https://garygregory.wordpress.com/2013/06/18/what-are-the-java-timezone-ids/
+        int offset = tz.getRawOffset();
+        char pm = '+';
+        if (offset < 0)
+        {
+            pm = '-';
+            offset = -offset;
+        }
+        int hh = offset / 3600000;
+        int mm = offset % 3600000 / 60000;
+        if (offset == 0)
+        {
+            tz.setID("GMT");
+        }
+        else if (pm == '+' && hh <= 12)
+        {
+            tz.setID(String.format("GMT+%02d:%02d", hh, mm));
+        }
+        else if (pm == '-' && hh <= 14)
+        {
+            tz.setID(String.format("GMT-%02d:%02d", hh, mm));
+        }
+        else
+        {
+            tz.setID("unknown");
+        }
+    }
+    
+    /**
      * Parses a big-endian date: year month day hour min sec.
      * The year must be four digits. Other fields may be adjacent 
      * and delimited by length or they may follow appropriate delimiters.