From: Nick Burch Date: Mon, 4 Jul 2016 20:55:07 +0000 (+0000) Subject: #59780 OPC support for a wider range of timezone'd created and modified date formats... X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=90e4babfc1b863f05da983ecd500308a6e1b76ef;p=poi.git #59780 OPC support for a wider range of timezone'd created and modified date formats in package properties git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1751379 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/PackagePropertiesPart.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/PackagePropertiesPart.java index bb93c36926..b8091973e9 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/PackagePropertiesPart.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/PackagePropertiesPart.java @@ -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)"); diff --git a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackageCoreProperties.java b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackageCoreProperties.java index e8bbcd9bdf..52ee4ffd83 100644 --- a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackageCoreProperties.java +++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackageCoreProperties.java @@ -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())); } } diff --git a/test-data/openxml4j/OPCCompliance_CoreProperties_AlternateTimezones.docx b/test-data/openxml4j/OPCCompliance_CoreProperties_AlternateTimezones.docx index 8b4de4a2e2..b2a9418530 100644 Binary files a/test-data/openxml4j/OPCCompliance_CoreProperties_AlternateTimezones.docx and b/test-data/openxml4j/OPCCompliance_CoreProperties_AlternateTimezones.docx differ