Return-Path: X-Original-To: apmail-pdfbox-commits-archive@www.apache.org Delivered-To: apmail-pdfbox-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 4D711172E1 for ; Mon, 2 Feb 2015 18:27:42 +0000 (UTC) Received: (qmail 66110 invoked by uid 500); 2 Feb 2015 18:27:43 -0000 Delivered-To: apmail-pdfbox-commits-archive@pdfbox.apache.org Received: (qmail 66086 invoked by uid 500); 2 Feb 2015 18:27:43 -0000 Mailing-List: contact commits-help@pdfbox.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@pdfbox.apache.org Delivered-To: mailing list commits@pdfbox.apache.org Received: (qmail 66076 invoked by uid 99); 2 Feb 2015 18:27:43 -0000 Received: from eris.apache.org (HELO hades.apache.org) (140.211.11.105) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 02 Feb 2015 18:27:43 +0000 Received: from hades.apache.org (localhost [127.0.0.1]) by hades.apache.org (ASF Mail Server at hades.apache.org) with ESMTP id 0FBBEAC0059; Mon, 2 Feb 2015 18:27:43 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1656533 - in /pdfbox/trunk/xmpbox/src: main/java/org/apache/xmpbox/DateConverter.java test/java/org/apache/xmpbox/DateConverterTest.java Date: Mon, 02 Feb 2015 18:27:42 -0000 To: commits@pdfbox.apache.org From: msahyoun@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20150202182743.0FBBEAC0059@hades.apache.org> Author: msahyoun Date: Mon Feb 2 18:27:42 2015 New Revision: 1656533 URL: http://svn.apache.org/r1656533 Log: PDFBOX-2319 support additional date time strings Added: pdfbox/trunk/xmpbox/src/test/java/org/apache/xmpbox/DateConverterTest.java (with props) Modified: pdfbox/trunk/xmpbox/src/main/java/org/apache/xmpbox/DateConverter.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=1656533&r1=1656532&r2=1656533&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 18:27:42 2015 @@ -28,6 +28,9 @@ import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.SimpleTimeZone; +import java.util.TimeZone; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * This class is used to convert dates to strings and back using the PDF date standards. Date are described in @@ -47,8 +50,12 @@ public class DateConverter // to try if the original one does not work. private static final SimpleDateFormat[] POTENTIAL_FORMATS = new SimpleDateFormat[] { new SimpleDateFormat("EEEE, dd MMM yyyy hh:mm:ss a"), - new SimpleDateFormat("EEEE, MMM dd, yyyy hh:mm:ss a"), new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"), - new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz") }; + new SimpleDateFormat("EEEE, MMM dd, yyyy hh:mm:ss a"), + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"), + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz"), + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"), + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S") + }; /** * According to check-style, Utility classes should not have a public or default constructor. @@ -84,7 +91,13 @@ public class DateConverter try { SimpleTimeZone zone = null; - if (date.startsWith("D:")) + + if (Pattern.matches("^\\d{4}-\\d{2}-\\d{2}T.*", date)) + { + // Assuming ISO860 date string + return fromISO8601(date); + } + else if (date.startsWith("D:")) { date = date.substring(2, date.length()); } @@ -281,4 +294,50 @@ public class DateConverter retval.append(Integer.toString(minutes)); return retval.toString(); } + + /** + * Get a Calendar from an ISO8601 date string. + * + * @param dateString + * @return the Calendar instance. + */ + private static Calendar fromISO8601(String dateString) + { + // Pattern to test for a time zone string + Pattern timeZonePattern = Pattern.compile( + "[\\d-]*T?[\\d-\\.]([A-Z]{1,4})$|(.*\\d*)([A-Z][a-z]+\\/[A-Z][a-z]+)$" + ); + Matcher timeZoneMatcher = timeZonePattern.matcher(dateString); + + String timeZoneString = null; + + while (timeZoneMatcher.find()) + { + for (int i = 1; i <= timeZoneMatcher.groupCount(); i++) + { + if (timeZoneMatcher.group(i) != null) + { + timeZoneString = timeZoneMatcher.group(i); + } + } + } + + if (timeZoneString != null) + { + + Calendar cal = javax.xml.bind.DatatypeConverter.parseDateTime( + dateString.substring(0, dateString.indexOf(timeZoneString)) + + ); + + TimeZone z = TimeZone.getTimeZone(timeZoneString); + cal.setTimeZone(z); + + return cal; + } + else + { + return javax.xml.bind.DatatypeConverter.parseDateTime(dateString); + } + } } Added: 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=1656533&view=auto ============================================================================== --- pdfbox/trunk/xmpbox/src/test/java/org/apache/xmpbox/DateConverterTest.java (added) +++ pdfbox/trunk/xmpbox/src/test/java/org/apache/xmpbox/DateConverterTest.java Mon Feb 2 18:27:42 2015 @@ -0,0 +1,61 @@ +package org.apache.xmpbox; + +import static org.junit.Assert.assertEquals; + +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.TimeZone; +import java.util.regex.Pattern; + +import org.junit.Test; + +/** + * Test the date conversion utility. + * + */ +public class DateConverterTest +{ + + /** + * Test several ISO6801 date formats. + * + * Test with additional time zone + * information normally not supported by ISO8601 + * + * @throws Exception when there is an exception + */ + @Test + public void testDateConversion() throws Exception + { + + final SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd'T'HH:mm.ss.SSSXXX"); + Calendar jaxbCal = null, + convDate = null; + // Test partial dates + convDate = DateConverter.toCalendar("2015-02-02"); + assertEquals(2015, convDate.get(Calendar.YEAR)); + + // Test some time zone offsets + jaxbCal = javax.xml.bind.DatatypeConverter.parseDateTime("2015-02-02T16:37:19.192Z"); + convDate = DateConverter.toCalendar("2015-02-02T16:37:19.192Z"); + assertEquals(dateFormat.format(jaxbCal.getTime()), dateFormat.format(convDate.getTime())); + + jaxbCal = javax.xml.bind.DatatypeConverter.parseDateTime("2015-02-02T16:37:19.192+00:00"); + convDate = DateConverter.toCalendar("2015-02-02T16:37:19.192Z"); + assertEquals(dateFormat.format(jaxbCal.getTime()), dateFormat.format(convDate.getTime())); + + jaxbCal = javax.xml.bind.DatatypeConverter.parseDateTime("2015-02-02T16:37:19.192+02:00"); + convDate = DateConverter.toCalendar("2015-02-02T16:37:19.192+02:00"); + assertEquals(dateFormat.format(jaxbCal.getTime()), dateFormat.format(convDate.getTime())); + + jaxbCal = javax.xml.bind.DatatypeConverter.parseDateTime("2015-02-02T16:37:19.192Z"); + convDate = DateConverter.toCalendar("2015-02-02T08:37:19.192PST"); + assertEquals(dateFormat.format(jaxbCal.getTime()), dateFormat.format(convDate.getTime())); + + 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())); + + } +} Propchange: pdfbox/trunk/xmpbox/src/test/java/org/apache/xmpbox/DateConverterTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: pdfbox/trunk/xmpbox/src/test/java/org/apache/xmpbox/DateConverterTest.java ------------------------------------------------------------------------------ svn:mime-type = text/plain