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