001: /*
002: * $Id: PdfDate.java 2911 2007-09-06 06:39:00Z xlv $
003: *
004: * Copyright 1999, 2000, 2001, 2002 Bruno Lowagie
005: *
006: * The contents of this file are subject to the Mozilla Public License Version 1.1
007: * (the "License"); you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
009: *
010: * Software distributed under the License is distributed on an "AS IS" basis,
011: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
012: * for the specific language governing rights and limitations under the License.
013: *
014: * The Original Code is 'iText, a free JAVA-PDF library'.
015: *
016: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
017: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
018: * All Rights Reserved.
019: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
020: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
021: *
022: * Contributor(s): all the names of the contributors are added in the source code
023: * where applicable.
024: *
025: * Alternatively, the contents of this file may be used under the terms of the
026: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
027: * provisions of LGPL are applicable instead of those above. If you wish to
028: * allow use of your version of this file only under the terms of the LGPL
029: * License and not to allow others to use your version of this file under
030: * the MPL, indicate your decision by deleting the provisions above and
031: * replace them with the notice and other provisions required by the LGPL.
032: * If you do not delete the provisions above, a recipient may use your version
033: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
034: *
035: * This library is free software; you can redistribute it and/or modify it
036: * under the terms of the MPL as stated above or under the terms of the GNU
037: * Library General Public License as published by the Free Software Foundation;
038: * either version 2 of the License, or any later version.
039: *
040: * This library is distributed in the hope that it will be useful, but WITHOUT
041: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
042: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
043: * details.
044: *
045: * If you didn't download this code from the following link, you should check if
046: * you aren't using an obsolete version:
047: * http://www.lowagie.com/iText/
048: */
049:
050: package com.lowagie.text.pdf;
051:
052: import java.util.Calendar;
053: import java.util.GregorianCalendar;
054: import java.util.SimpleTimeZone;
055:
056: /**
057: * <CODE>PdfDate</CODE> is the PDF date object.
058: * <P>
059: * PDF defines a standard date format. The PDF date format closely follows the format
060: * defined by the international standard ASN.1 (Abstract Syntax Notation One, defined
061: * in CCITT X.208 or ISO/IEC 8824). A date is a <CODE>PdfString</CODE> of the form:
062: * <P><BLOCKQUOTE>
063: * (D:YYYYMMDDHHmmSSOHH'mm')
064: * </BLOCKQUOTE><P>
065: * This object is described in the 'Portable Document Format Reference Manual version 1.3'
066: * section 7.2 (page 183-184)
067: *
068: * @see PdfString
069: * @see java.util.GregorianCalendar
070: */
071:
072: public class PdfDate extends PdfString {
073:
074: private static final int DATE_SPACE[] = { Calendar.YEAR, 4, 0,
075: Calendar.MONTH, 2, -1, Calendar.DAY_OF_MONTH, 2, 0,
076: Calendar.HOUR_OF_DAY, 2, 0, Calendar.MINUTE, 2, 0,
077: Calendar.SECOND, 2, 0 };
078:
079: // constructors
080:
081: /**
082: * Constructs a <CODE>PdfDate</CODE>-object.
083: *
084: * @param d the date that has to be turned into a <CODE>PdfDate</CODE>-object
085: */
086:
087: public PdfDate(Calendar d) {
088: super ();
089: StringBuffer date = new StringBuffer("D:");
090: date.append(setLength(d.get(Calendar.YEAR), 4));
091: date.append(setLength(d.get(Calendar.MONTH) + 1, 2));
092: date.append(setLength(d.get(Calendar.DATE), 2));
093: date.append(setLength(d.get(Calendar.HOUR_OF_DAY), 2));
094: date.append(setLength(d.get(Calendar.MINUTE), 2));
095: date.append(setLength(d.get(Calendar.SECOND), 2));
096: int timezone = (d.get(Calendar.ZONE_OFFSET) + d
097: .get(Calendar.DST_OFFSET))
098: / (60 * 60 * 1000);
099: if (timezone == 0) {
100: date.append('Z');
101: } else if (timezone < 0) {
102: date.append('-');
103: timezone = -timezone;
104: } else {
105: date.append('+');
106: }
107: if (timezone != 0) {
108: date.append(setLength(timezone, 2)).append('\'');
109: int zone = Math.abs((d.get(Calendar.ZONE_OFFSET) + d
110: .get(Calendar.DST_OFFSET))
111: / (60 * 1000))
112: - (timezone * 60);
113: date.append(setLength(zone, 2)).append('\'');
114: }
115: value = date.toString();
116: }
117:
118: /**
119: * Constructs a <CODE>PdfDate</CODE>-object, representing the current day and time.
120: */
121:
122: public PdfDate() {
123: this (new GregorianCalendar());
124: }
125:
126: /**
127: * Adds a number of leading zeros to a given <CODE>String</CODE> in order to get a <CODE>String</CODE>
128: * of a certain length.
129: *
130: * @param i a given number
131: * @param length the length of the resulting <CODE>String</CODE>
132: * @return the resulting <CODE>String</CODE>
133: */
134:
135: private String setLength(int i, int length) { // 1.3-1.4 problem fixed by Finn Bock
136: StringBuffer tmp = new StringBuffer();
137: tmp.append(i);
138: while (tmp.length() < length) {
139: tmp.insert(0, "0");
140: }
141: tmp.setLength(length);
142: return tmp.toString();
143: }
144:
145: /**
146: * Gives the W3C format of the PdfDate.
147: * @return a formatted date
148: */
149: public String getW3CDate() {
150: return getW3CDate(value);
151: }
152:
153: /**
154: * Gives the W3C format of the PdfDate.
155: * @param d the date in the format D:YYYYMMDDHHmmSSOHH'mm'
156: * @return a formatted date
157: */
158: public static String getW3CDate(String d) {
159: if (d.startsWith("D:"))
160: d = d.substring(2);
161: StringBuffer sb = new StringBuffer();
162: if (d.length() < 4)
163: return "0000";
164: sb.append(d.substring(0, 4)); //year
165: d = d.substring(4);
166: if (d.length() < 2)
167: return sb.toString();
168: sb.append('-').append(d.substring(0, 2)); //month
169: d = d.substring(2);
170: if (d.length() < 2)
171: return sb.toString();
172: sb.append('-').append(d.substring(0, 2)); //day
173: d = d.substring(2);
174: if (d.length() < 2)
175: return sb.toString();
176: sb.append('T').append(d.substring(0, 2)); //hour
177: d = d.substring(2);
178: if (d.length() < 2) {
179: sb.append(":00Z");
180: return sb.toString();
181: }
182: sb.append(':').append(d.substring(0, 2)); //minute
183: d = d.substring(2);
184: if (d.length() < 2) {
185: sb.append('Z');
186: return sb.toString();
187: }
188: sb.append(':').append(d.substring(0, 2)); //second
189: d = d.substring(2);
190: if (d.startsWith("-") || d.startsWith("+")) {
191: String sign = d.substring(0, 1);
192: d = d.substring(1);
193: String h = "00";
194: String m = "00";
195: if (d.length() >= 2) {
196: h = d.substring(0, 2);
197: if (d.length() > 2) {
198: d = d.substring(3);
199: if (d.length() >= 2)
200: m = d.substring(0, 2);
201: }
202: sb.append(sign).append(h).append(':').append(m);
203: return sb.toString();
204: }
205: }
206: sb.append('Z');
207: return sb.toString();
208: }
209:
210: /**
211: * Converts a PDF string representing a date into a Calendar.
212: * @param s the PDF string representing a date
213: * @return a <CODE>Calendar</CODE> representing the date or <CODE>null</CODE> if the string
214: * was not a date
215: */
216: public static Calendar decode(String s) {
217: try {
218: if (s.startsWith("D:"))
219: s = s.substring(2);
220: GregorianCalendar calendar;
221: int slen = s.length();
222: int idx = s.indexOf('Z');
223: if (idx >= 0) {
224: slen = idx;
225: calendar = new GregorianCalendar(new SimpleTimeZone(0,
226: "ZPDF"));
227: } else {
228: int sign = 1;
229: idx = s.indexOf('+');
230: if (idx < 0) {
231: idx = s.indexOf('-');
232: if (idx >= 0)
233: sign = -1;
234: }
235: if (idx < 0)
236: calendar = new GregorianCalendar();
237: else {
238: int offset = Integer.parseInt(s.substring(idx + 1,
239: idx + 3)) * 60;
240: if (idx + 5 < s.length())
241: offset += Integer.parseInt(s.substring(idx + 4,
242: idx + 6));
243: calendar = new GregorianCalendar(
244: new SimpleTimeZone(offset * sign * 60000,
245: "ZPDF"));
246: slen = idx;
247: }
248: }
249: calendar.clear();
250: idx = 0;
251: for (int k = 0; k < DATE_SPACE.length; k += 3) {
252: if (idx >= slen)
253: break;
254: calendar.set(DATE_SPACE[k], Integer.parseInt(s
255: .substring(idx, idx + DATE_SPACE[k + 1]))
256: + DATE_SPACE[k + 2]);
257: idx += DATE_SPACE[k + 1];
258: }
259: return calendar;
260: } catch (Exception e) {
261: return null;
262: }
263: }
264: }
|