]> source.dussan.org Git - poi.git/commitdiff
#59780 OPC support for a wider range of timezone'd created and modified date formats...
authorNick Burch <nick@apache.org>
Mon, 4 Jul 2016 20:55:07 +0000 (20:55 +0000)
committerNick Burch <nick@apache.org>
Mon, 4 Jul 2016 20:55:07 +0000 (20:55 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1751379 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/openxml4j/opc/internal/PackagePropertiesPart.java
src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackageCoreProperties.java
test-data/openxml4j/OPCCompliance_CoreProperties_AlternateTimezones.docx

index bb93c36926be3a204032b80524c5db00055fe3c8..b8091973e95eaf1f86a5a67f6824166c73bb4928 100644 (file)
@@ -50,7 +50,7 @@ public final class PackagePropertiesPart extends PackagePart implements
 
        public final static String NAMESPACE_DCTERMS_URI = "http://purl.org/dc/terms/";
 
-       private final static String DEFAULT_DATEFORMAT =     "yyyy-MM-dd'T'HH:mm:ss'Z'";
+       private final static String DEFAULT_DATEFORMAT =   "yyyy-MM-dd'T'HH:mm:ss'Z'";
 
        private final static String[] DATE_FORMATS = new String[]{
                        DEFAULT_DATEFORMAT,
@@ -61,9 +61,12 @@ public final class PackagePropertiesPart extends PackagePart implements
        //When we move to Java 7, we should be able to add another
        //date format to DATE_FORMATS that uses XXX and get rid of this
        //and TIME_ZONE_PAT
+       // TODO Fix this after the Java 7 upgrade
        private final String[] TZ_DATE_FORMATS = new String[]{
                        "yyyy-MM-dd'T'HH:mm:ssz",
-                       "yyyy-MM-dd'T'HH:mm:ss.SSSz"
+            "yyyy-MM-dd'T'HH:mm:ss.Sz",
+            "yyyy-MM-dd'T'HH:mm:ss.SSz",
+                       "yyyy-MM-dd'T'HH:mm:ss.SSSz",
        };
 
        private final Pattern TIME_ZONE_PAT = Pattern.compile("([-+]\\d\\d):?(\\d\\d)");
index e8bbcd9bdfe6878e1c3b6b12e709858fe1bfe305..52ee4ffd83f5c129bf1c910361d3881a4b2ff729 100644 (file)
@@ -273,21 +273,52 @@ public final class TestPackageCoreProperties {
     }
        
        @Test
-       @Ignore
        public void testAlternateCorePropertyTimezones() throws Exception {
         InputStream is = OpenXML4JTestDataSamples.openSampleStream("OPCCompliance_CoreProperties_AlternateTimezones.docx");
         OPCPackage pkg = OPCPackage.open(is);
         PackagePropertiesPart props = (PackagePropertiesPart)pkg.getPackageProperties();
         is.close();
+        
+        // We need predictable dates for testing!
+        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ROOT);
+        df.setTimeZone(LocaleUtil.TIMEZONE_UTC);
 
         // Check text properties first
-        assertEquals("Lorem Ipsu", props.getTitleProperty().getValue());
+        assertEquals("Lorem Ipsum", props.getTitleProperty().getValue());
         assertEquals("Apache POI", props.getCreatorProperty().getValue());
         
         // Created at has a +3 timezone and milliseconds
-        //   2006-10-13T18:06:00.1234+03:00
+        //   2006-10-13T18:06:00.123+03:00
+        // = 2006-10-13T15:06:00.123+00:00
+        assertEquals("2006-10-13T15:06:00Z", props.getCreatedPropertyString());
+        assertEquals("2006-10-13T15:06:00.123Z", df.format(props.getCreatedProperty().getValue()));
         
         // Modified at has a -13 timezone but no milliseconds
         //   2007-06-20T07:59:00-13:00
+        // = 2007-06-20T20:59:00-13:00
+        assertEquals("2007-06-20T20:59:00Z", props.getModifiedPropertyString());
+        assertEquals("2007-06-20T20:59:00.000Z", df.format(props.getModifiedProperty().getValue()));
+        
+        
+        // Ensure we can change them with other timezones and still read back OK
+        props.setCreatedProperty("2007-06-20T20:57:00+13:00");
+        props.setModifiedProperty("2007-06-20T20:59:00.123-13:00");
+        
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        pkg.save(baos);
+        pkg = OPCPackage.open(new ByteArrayInputStream(baos.toByteArray()));
+        
+        // Check text properties first - should be unchanged
+        assertEquals("Lorem Ipsum", props.getTitleProperty().getValue());
+        assertEquals("Apache POI", props.getCreatorProperty().getValue());
+        
+        // Check the updated times
+        //   2007-06-20T20:57:00+13:00
+        // = 2007-06-20T07:57:00Z
+        assertEquals("2007-06-20T07:57:00.000Z", df.format(props.getCreatedProperty().getValue()));
+        
+        //   2007-06-20T20:59:00.123-13:00
+        // = 2007-06-21T09:59:00.123Z
+        assertEquals("2007-06-21T09:59:00.123Z", df.format(props.getModifiedProperty().getValue()));
        }
 }
index 8b4de4a2e270c058bc3c2a6e91a47a0f3ce86552..b2a9418530475a6710232558d4481a78ce95282a 100644 (file)
Binary files a/test-data/openxml4j/OPCCompliance_CoreProperties_AlternateTimezones.docx and b/test-data/openxml4j/OPCCompliance_CoreProperties_AlternateTimezones.docx differ