001: /*
002: ** Java cvs client library package.
003: ** Copyright (c) 1997-2002 by Timothy Gerard Endres
004: **
005: ** This program is free software.
006: **
007: ** You may redistribute it and/or modify it under the terms of the GNU
008: ** Library General Public License (LGPL) as published by the Free Software
009: ** Foundation.
010: **
011: ** Version 2 of the license should be included with this distribution in
012: ** the file LICENSE.txt, as well as License.html. If the license is not
013: ** included with this distribution, you may find a copy at the FSF web
014: ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the Free
015: ** Software Foundation at 59 Temple Place - Suite 330, Boston, MA 02111 USA.
016: **
017: ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
018: ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
019: ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
020: ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
021: ** REDISTRIBUTION OF THIS SOFTWARE.
022: **
023: */
024:
025: package com.ice.cvsc;
026:
027: import java.lang.*;
028: import java.text.*;
029: import java.util.*;
030:
031: /**
032: * The CVSTimestampFormat class implements the code necessary
033: * to format and parse CVS Entry timestamps, which come in the
034: * flavor of 'Wed Mar 4 1997 15:43:06'.
035: *
036: * <strong>NOTE</strong> This class <em>explicitly</em> operates
037: * entirely in the 'Locale.US' locality. Thus, this class is
038: * <em>not</em> useful for display purposes, since the values
039: * are not localized.
040: *
041: * @version $Revision: 2.5 $
042: * @author Timothy Gerard Endres, <a href="mailto:time@ice.com">time@ice.com</a>.
043: * @see CVSClient
044: * @see CVSRequest
045: */
046:
047: public class CVSTimestampFormat extends Format {
048: static public final String RCS_ID = "$Id: CVSTimestampFormat.java,v 2.5 2003/07/27 01:08:32 time Exp $";
049: static public final String RCS_REV = "$Revision: 2.5 $";
050:
051: static public final String DEFAULT_GMT_TZID = "GMT";
052:
053: static private TimeZone tz;
054: static private String timezoneID;
055:
056: static {
057: CVSTimestampFormat.timezoneID = CVSTimestampFormat.DEFAULT_GMT_TZID;
058:
059: CVSTimestampFormat.tz = TimeZone
060: .getTimeZone(CVSTimestampFormat.timezoneID);
061: }
062:
063: static public final CVSTimestampFormat getInstance() {
064: return new CVSTimestampFormat();
065: }
066:
067: static public final void setTimeZoneID(String timezoneID) {
068: CVSTimestampFormat.timezoneID = timezoneID;
069: CVSTimestampFormat.tz = TimeZone
070: .getTimeZone(CVSTimestampFormat.timezoneID);
071: }
072:
073: public CVSTimestampFormat() {
074: super ();
075: }
076:
077: public String format(CVSTimestamp stamp)
078: throws IllegalArgumentException {
079: return this .formatTimeZone(stamp, CVSTimestampFormat.tz);
080: }
081:
082: public String formatTimeZone(CVSTimestamp stamp, TimeZone tz)
083: throws IllegalArgumentException {
084: Locale loc = Locale.US;
085: SimpleDateFormat dateFormat;
086:
087: dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy",
088: loc);
089:
090: dateFormat.setTimeZone(tz);
091: String result = dateFormat.format(stamp);
092:
093: return result;
094: }
095:
096: public StringBuffer format(Object stamp, StringBuffer appendTo,
097: FieldPosition fieldPos) throws IllegalArgumentException {
098: // UNDONE - handle fieldPos!
099: String tmpFormat = this .format((CVSTimestamp) stamp);
100: appendTo.append(tmpFormat);
101: return appendTo;
102: }
103:
104: public String formatTerse(CVSTimestamp stamp) {
105: return this .formatTerseTimeZone(stamp, CVSTimestampFormat.tz);
106: }
107:
108: public String formatTerseTimeZone(CVSTimestamp stamp, TimeZone tz) {
109: Locale loc = Locale.US;
110: SimpleDateFormat dateFormat;
111:
112: dateFormat = new SimpleDateFormat("yyMMdd HH:mm", loc);
113:
114: dateFormat.setTimeZone(CVSTimestampFormat.tz);
115:
116: String result = dateFormat.format(stamp);
117:
118: return result;
119: }
120:
121: public CVSTimestamp parse(String source) throws ParseException {
122: return parseTimestamp(source);
123: }
124:
125: public Object parseObject(String source, ParsePosition pos) {
126: CVSTimestamp stamp = null;
127:
128: try {
129: stamp = this .parseTimestamp(source);
130: } catch (ParseException ex) {
131: stamp = null;
132: }
133:
134: return (Object) stamp;
135: }
136:
137: public CVSTimestamp parseTimestamp(String source)
138: throws ParseException {
139: ParsePosition pos = new ParsePosition(0);
140: return this .parseTimestamp(source, pos);
141: }
142:
143: public CVSTimestamp parseTimestamp(String source, ParsePosition pos)
144: throws ParseException {
145: Locale loc = Locale.US;
146: SimpleDateFormat dateFormat;
147:
148: dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy",
149: loc);
150:
151: dateFormat.setTimeZone(CVSTimestampFormat.tz);
152:
153: Date result = dateFormat.parse(source, pos);
154:
155: // NOTE SimpleDateFormat IGNORANTLY returns null instead
156: // of throwing a ParseException
157: if (result == null)
158: throw new ParseException("invalid timestamp '" + source
159: + "'", 0);
160:
161: return new CVSTimestamp(result);
162: }
163:
164: public static void main(String[] args) {
165: CVSTimestampFormat fmt = CVSTimestampFormat.getInstance();
166:
167: try {
168: CVSTimestamp ts = fmt.parseTimestamp(args[0],
169: new ParsePosition(0));
170:
171: System.err.println("TS = " + ts);
172: } catch (ParseException ex) {
173: ex.printStackTrace();
174: }
175: }
176:
177: }
|