import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
+import java.util.Locale;
import java.util.TimeZone;
import org.apache.commons.logging.Log;
}
/** Formatting pattern for PDF date */
- protected static final SimpleDateFormat DATE_FORMAT
- = new SimpleDateFormat("'D:'yyyyMMddHHmmss");
+ protected static final SimpleDateFormat DATE_FORMAT;
+ static {
+ DATE_FORMAT = new SimpleDateFormat("'D:'yyyyMMddHHmmss", Locale.ENGLISH);
+ DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT"));
+ }
+
/**
* Formats a date/time according to the PDF specification
* (D:YYYYMMDDHHmmSSOHH'mm').
* @param time date/time value to format
+ * @param tz the time zone
* @return the requested String representation
*/
- protected String formatDateTime(Date time) {
- StringBuffer sb = new StringBuffer();
- sb.append(DATE_FORMAT.format(time));
- TimeZone tz = TimeZone.getDefault();
- Calendar cal = Calendar.getInstance();
+ protected String formatDateTime(Date time, TimeZone tz) {
+ Calendar cal = Calendar.getInstance(tz, Locale.ENGLISH);
cal.setTime(time);
- int era = cal.get(Calendar.ERA);
- int year = cal.get(Calendar.YEAR);
- int month = cal.get(Calendar.MONTH);
- int day = cal.get(Calendar.DAY_OF_MONTH);
- int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
- int milliseconds = cal.get(Calendar.HOUR_OF_DAY) * 1000 * 60 * 60;
- milliseconds += cal.get(Calendar.MINUTE) * 1000 * 60;
- milliseconds += cal.get(Calendar.SECOND) * 1000;
- milliseconds += cal.get(Calendar.MILLISECOND);
+ int offset = cal.get(Calendar.ZONE_OFFSET);
+ offset += cal.get(Calendar.DST_OFFSET);
+
+ //DateFormat is operating on GMT so adjust for time zone offset
+ Date dt1 = new Date(time.getTime() + offset);
+ StringBuffer sb = new StringBuffer();
+ sb.append(DATE_FORMAT.format(dt1));
+
+ offset /= (1000 * 60); //Convert to minutes
- int offset = tz.getOffset(era, year, month, day, dayOfWeek, milliseconds);
if (offset == 0) {
sb.append('Z');
} else {
} else {
sb.append('-');
}
- final int HOUR = (1000 * 60 * 60);
- int offsetHour = Math.abs(offset / HOUR);
- int offsetMinutes = (offset - (offsetHour * HOUR)) / (1000 * 60);
+ int offsetHour = Math.abs(offset / 60);
+ int offsetMinutes = Math.abs(offset % 60);
if (offsetHour < 10) {
sb.append('0');
}
return sb.toString();
}
+ /**
+ * Formats a date/time according to the PDF specification.
+ * (D:YYYYMMDDHHmmSSOHH'mm').
+ * @param time date/time value to format
+ * @return the requested String representation
+ */
+ protected String formatDateTime(Date time) {
+ return formatDateTime(time, TimeZone.getDefault());
+ }
+
}
<changes>
<release version="FOP Trunk">
+ <action context="Fonts" dev="JM" type="fix">
+ Bugfix for date formatting with negative time zones in the PDF's Info object.
+ </action>
<action context="Fonts" dev="JM" type="add">
Added an option to disable the default sRGB profile in PDF output for those who
don't care about color fidelity, but care about PDF file size.
package org.apache.fop;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.fop.pdf.PDFObjectTestCase;
import org.apache.fop.traits.BorderPropsTestCase;
import org.apache.fop.traits.TraitColorTestCase;
import org.apache.fop.util.DataURIResolverTestCase;
import org.apache.fop.util.PDFNumberTestCase;
import org.apache.fop.util.UnitConvTestCase;
-import junit.framework.Test;
-import junit.framework.TestSuite;
-
/**
* Test suite for FOP's utility classes.
*/
"Test suite for FOP's utility classes");
//$JUnit-BEGIN$
suite.addTest(new TestSuite(PDFNumberTestCase.class));
+ suite.addTest(new TestSuite(PDFObjectTestCase.class));
suite.addTest(new TestSuite(UnitConvTestCase.class));
suite.addTest(new TestSuite(TraitColorTestCase.class));
suite.addTest(new TestSuite(BorderPropsTestCase.class));
--- /dev/null
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* $Id$ */
+
+package org.apache.fop.pdf;
+
+import java.util.Calendar;
+import java.util.Date;
+import java.util.Locale;
+import java.util.TimeZone;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests the PDFObject class.
+ */
+public class PDFObjectTestCase extends TestCase {
+
+ /**
+ * Tests date/time formatting in PDFObject.
+ * @throws Exception if an error occurs
+ */
+ public void testDateFormatting() throws Exception {
+ Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.ENGLISH);
+ cal.set(2008, Calendar.FEBRUARY, 07, 15, 11, 07);
+ cal.set(Calendar.MILLISECOND, 0);
+ Date dt = cal.getTime();
+
+ MyPDFObject obj = new MyPDFObject();
+ String s = obj.formatDateTime(dt, TimeZone.getTimeZone("GMT"));
+ assertEquals("D:20080207151107Z", s);
+ s = obj.formatDateTime(dt, TimeZone.getTimeZone("GMT+02:00"));
+ assertEquals("D:20080207171107+02'00'", s);
+ s = obj.formatDateTime(dt, TimeZone.getTimeZone("GMT+02:30"));
+ assertEquals("D:20080207174107+02'30'", s);
+ s = obj.formatDateTime(dt, TimeZone.getTimeZone("GMT-08:00"));
+ assertEquals("D:20080207071107-08'00'", s);
+ }
+
+ private class MyPDFObject extends PDFObject {
+
+ }
+
+}