001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.tomcat.util.buf;
018:
019: import java.text.DateFormat;
020: import java.text.FieldPosition;
021: import java.text.ParseException;
022: import java.text.SimpleDateFormat;
023: import java.util.Date;
024: import java.util.Locale;
025: import java.util.TimeZone;
026:
027: import org.apache.tomcat.util.res.StringManager;
028:
029: /**
030: * Common place for date utils.
031: *
032: * @deprecated Will be replaced with a more efficient impl, based on
033: * FastDateFormat, with an API using less objects.
034: * @author dac@eng.sun.com
035: * @author Jason Hunter [jch@eng.sun.com]
036: * @author James Todd [gonzo@eng.sun.com]
037: * @author Costin Manolache
038: */
039: public class DateTool {
040:
041: /** US locale - all HTTP dates are in english
042: */
043: private final static Locale LOCALE_US = Locale.US;
044:
045: /** GMT timezone - all HTTP dates are on GMT
046: */
047: public final static TimeZone GMT_ZONE = TimeZone.getTimeZone("GMT");
048:
049: /** format for RFC 1123 date string -- "Sun, 06 Nov 1994 08:49:37 GMT"
050: */
051: public final static String RFC1123_PATTERN = "EEE, dd MMM yyyy HH:mm:ss z";
052:
053: // format for RFC 1036 date string -- "Sunday, 06-Nov-94 08:49:37 GMT"
054: public final static String rfc1036Pattern = "EEEEEEEEE, dd-MMM-yy HH:mm:ss z";
055:
056: // format for C asctime() date string -- "Sun Nov 6 08:49:37 1994"
057: public final static String asctimePattern = "EEE MMM d HH:mm:ss yyyy";
058:
059: /** Pattern used for old cookies
060: */
061: private final static String OLD_COOKIE_PATTERN = "EEE, dd-MMM-yyyy HH:mm:ss z";
062:
063: /** DateFormat to be used to format dates. Called from MessageBytes
064: */
065: private final static DateFormat rfc1123Format = new SimpleDateFormat(
066: RFC1123_PATTERN, LOCALE_US);
067:
068: /** DateFormat to be used to format old netscape cookies
069: Called from ServerCookie
070: */
071: private final static DateFormat oldCookieFormat = new SimpleDateFormat(
072: OLD_COOKIE_PATTERN, LOCALE_US);
073:
074: private final static DateFormat rfc1036Format = new SimpleDateFormat(
075: rfc1036Pattern, LOCALE_US);
076:
077: private final static DateFormat asctimeFormat = new SimpleDateFormat(
078: asctimePattern, LOCALE_US);
079:
080: static {
081: rfc1123Format.setTimeZone(GMT_ZONE);
082: oldCookieFormat.setTimeZone(GMT_ZONE);
083: rfc1036Format.setTimeZone(GMT_ZONE);
084: asctimeFormat.setTimeZone(GMT_ZONE);
085: }
086:
087: private static String rfc1123DS;
088: private static long rfc1123Sec;
089:
090: private static StringManager sm = StringManager
091: .getManager("org.apache.tomcat.util.buf.res");
092:
093: // Called from MessageBytes.getTime()
094: static long parseDate(MessageBytes value) {
095: return parseDate(value.toString());
096: }
097:
098: // Called from MessageBytes.setTime
099: /**
100: */
101: public static String format1123(Date d) {
102: String dstr = null;
103: synchronized (rfc1123Format) {
104: dstr = format1123(d, rfc1123Format);
105: }
106: return dstr;
107: }
108:
109: public static String format1123(Date d, DateFormat df) {
110: long dt = d.getTime() / 1000;
111: if ((rfc1123DS != null) && (dt == rfc1123Sec))
112: return rfc1123DS;
113: rfc1123DS = df.format(d);
114: rfc1123Sec = dt;
115: return rfc1123DS;
116: }
117:
118: // Called from ServerCookie
119: /**
120: */
121: public static void formatOldCookie(Date d, StringBuffer sb,
122: FieldPosition fp) {
123: synchronized (oldCookieFormat) {
124: oldCookieFormat.format(d, sb, fp);
125: }
126: }
127:
128: // Called from ServerCookie
129: public static String formatOldCookie(Date d) {
130: String ocf = null;
131: synchronized (oldCookieFormat) {
132: ocf = oldCookieFormat.format(d);
133: }
134: return ocf;
135: }
136:
137: /** Called from HttpServletRequest.getDateHeader().
138: Not efficient - but not very used.
139: */
140: public static long parseDate(String dateString) {
141: DateFormat[] format = { rfc1123Format, rfc1036Format,
142: asctimeFormat };
143: return parseDate(dateString, format);
144: }
145:
146: public static long parseDate(String dateString, DateFormat[] format) {
147: Date date = null;
148: for (int i = 0; i < format.length; i++) {
149: try {
150: date = format[i].parse(dateString);
151: return date.getTime();
152: } catch (ParseException e) {
153: } catch (StringIndexOutOfBoundsException e) {
154: }
155: }
156: String msg = sm.getString("httpDate.pe", dateString);
157: throw new IllegalArgumentException(msg);
158: }
159:
160: }
|