pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From msahy...@apache.org
Subject svn commit: r1656598 - in /pdfbox/trunk/xmpbox/src: main/java/org/apache/xmpbox/DateConverter.java test/java/org/apache/xmpbox/DateConverterTest.java
Date Mon, 02 Feb 2015 22:44:06 GMT
Author: msahyoun
Date: Mon Feb  2 22:44:06 2015
New Revision: 1656598

URL: http://svn.apache.org/r1656598
Log:
PDFBOX-2319 millisecond precision is optional, add test case, fix SimpleDateFormatter string

Modified:
    pdfbox/trunk/xmpbox/src/main/java/org/apache/xmpbox/DateConverter.java
    pdfbox/trunk/xmpbox/src/test/java/org/apache/xmpbox/DateConverterTest.java

Modified: pdfbox/trunk/xmpbox/src/main/java/org/apache/xmpbox/DateConverter.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/xmpbox/src/main/java/org/apache/xmpbox/DateConverter.java?rev=1656598&r1=1656597&r2=1656598&view=diff
==============================================================================
--- pdfbox/trunk/xmpbox/src/main/java/org/apache/xmpbox/DateConverter.java (original)
+++ pdfbox/trunk/xmpbox/src/main/java/org/apache/xmpbox/DateConverter.java Mon Feb  2 22:44:06
2015
@@ -233,71 +233,49 @@ public class DateConverter
     }
 
     /**
-     * Append Zero to String Buffer.
-     * 
-     * This will append zero before number < 10 ('1' become '01')
+     * Convert the date to iso 8601 string format.
      * 
-     * @param out The String buffer
-     * @param number The concerned number
+     * @param cal
+     *            The date to convert.
+     * @return The date represented as an ISO 8601 string.
      */
-    private static void zeroAppend(StringBuffer out, int number)
+    public static String toISO8601(Calendar cal)
     {
-        if (number < 10)
-        {
-            out.append("0");
-        }
-        out.append(number);
+        return toISO8601(cal, false);
     }
-
+    
     /**
      * Convert the date to iso 8601 string format.
      * 
-     * @param cal
-     *            The date to convert.
+     * @param cal The date to convert.
+     * @param printMillis Print Milliseconds.
      * @return The date represented as an ISO 8601 string.
      */
-    public static String toISO8601(Calendar cal)
+    public static String toISO8601(Calendar cal, boolean printMillis)
     {
-        StringBuffer retval = new StringBuffer();
-
-        retval.append(cal.get(Calendar.YEAR));
-        retval.append("-");
-        zeroAppend(retval, cal.get(Calendar.MONTH) + 1);
-        retval.append("-");
-        zeroAppend(retval, cal.get(Calendar.DAY_OF_MONTH));
-        retval.append("T");
-        zeroAppend(retval, cal.get(Calendar.HOUR_OF_DAY));
-        retval.append(":");
-        zeroAppend(retval, cal.get(Calendar.MINUTE));
-        retval.append(":");
-        zeroAppend(retval, cal.get(Calendar.SECOND));
-
-        int timeZone = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET);
-        if (timeZone < 0)
+        final SimpleDateFormat iso8601Format = new SimpleDateFormat(
+                "yyyy-MM-dd'T'HH:mm:ssZ"
+            );        
+        final SimpleDateFormat iso8601FormatMillis = new SimpleDateFormat(
+                    "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
+                );
+        
+        StringBuffer dateString = new StringBuffer();
+        
+        if (printMillis)
         {
-            retval.append("-");
+            dateString.append(iso8601FormatMillis.format(cal.getTime()));
         }
         else
         {
-            retval.append("+");
+            dateString.append(iso8601Format.format(cal.getTime()));
         }
-        timeZone = Math.abs(timeZone);
-        // milliseconds/1000 = seconds = seconds / 60 = minutes = minutes/60 =
-        // hours
-        int hours = timeZone / 1000 / 60 / 60;
-        int minutes = (timeZone - (hours * 1000 * 60 * 60)) / 1000 / 1000;
-        if (hours < 10)
-        {
-            retval.append("0");
-        }
-        retval.append(Integer.toString(hours));
-        retval.append(":");
-        if (minutes < 10)
-        {
-            retval.append("0");
-        }
-        retval.append(Integer.toString(minutes));
-        return retval.toString();
+
+        // The Z format option prints the time zone in the format 
+        // +0100. Unfortunately this is not in line with the IDO8601 specification
+        // the format is +01:00. We will insert the colon to the formatted date.
+        dateString.insert(dateString.length()-2, ':');
+        return dateString.toString();
     }
     
     /**

Modified: pdfbox/trunk/xmpbox/src/test/java/org/apache/xmpbox/DateConverterTest.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/xmpbox/src/test/java/org/apache/xmpbox/DateConverterTest.java?rev=1656598&r1=1656597&r2=1656598&view=diff
==============================================================================
--- pdfbox/trunk/xmpbox/src/test/java/org/apache/xmpbox/DateConverterTest.java (original)
+++ pdfbox/trunk/xmpbox/src/test/java/org/apache/xmpbox/DateConverterTest.java Mon Feb  2
22:44:06 2015
@@ -36,7 +36,7 @@ public class DateConverterTest
 {
 
     /**
-     * Test several ISO6801 date formats.
+     * Test parsing several ISO8601 date formats.
      * 
      * Test with additional time zone
      * information normally not supported by ISO8601
@@ -47,7 +47,7 @@ public class DateConverterTest
     public void testDateConversion() throws Exception
     {
 
-        final SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd'T'HH:mm.ss.SSSXXX");
+        final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
         Calendar jaxbCal = null,
                 convDate = null;
         // Test partial dates
@@ -74,6 +74,21 @@ public class DateConverterTest
         jaxbCal = javax.xml.bind.DatatypeConverter.parseDateTime("2015-02-02T16:37:19.192+01:00");
         convDate = DateConverter.toCalendar("2015-02-02T16:37:19.192Europe/Berlin");
         assertEquals(dateFormat.format(jaxbCal.getTime()), dateFormat.format(convDate.getTime()));
-
+    }
+    
+    /**
+     * Test formatting ISO8601 date formats.
+     * 
+     * Test with additional time zone
+     * information normally not supported by ISO8601
+     *
+     * @throws Exception when there is an exception
+     */
+    @Test
+    public void testDateFormatting() throws Exception
+    {
+        Calendar cal = DateConverter.toCalendar("2015-02-02T16:37:19.192Z");
+        assertEquals("2015-02-02T17:37:19+01:00", DateConverter.toISO8601(cal));
+        assertEquals("2015-02-02T17:37:19.192+01:00", DateConverter.toISO8601(cal,true));
     }
 }



Mime
View raw message