]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/http/DateParser.java
Importing upstream Jetty jetty-9.2.1.v20140609
[gigi.git] / lib / jetty / org / eclipse / jetty / http / DateParser.java
1 //
2 //  ========================================================================
3 //  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 //  ------------------------------------------------------------------------
5 //  All rights reserved. This program and the accompanying materials
6 //  are made available under the terms of the Eclipse Public License v1.0
7 //  and Apache License v2.0 which accompanies this distribution.
8 //
9 //      The Eclipse Public License is available at
10 //      http://www.eclipse.org/legal/epl-v10.html
11 //
12 //      The Apache License v2.0 is available at
13 //      http://www.opensource.org/licenses/apache2.0.php
14 //
15 //  You may elect to redistribute this code under either of these licenses.
16 //  ========================================================================
17 //
18
19 package org.eclipse.jetty.http;
20
21 import java.text.SimpleDateFormat;
22 import java.util.Date;
23 import java.util.Locale;
24 import java.util.TimeZone;
25
26 /**
27  * ThreadLocal data parsers for HTTP style dates
28  *
29  */
30 class DateParser
31 {
32     private static final TimeZone __GMT = TimeZone.getTimeZone("GMT");
33     static
34     {
35         __GMT.setID("GMT");
36     }
37     
38     final static String __dateReceiveFmt[] =
39     {
40         "EEE, dd MMM yyyy HH:mm:ss zzz",
41         "EEE, dd-MMM-yy HH:mm:ss",
42         "EEE MMM dd HH:mm:ss yyyy",
43
44         "EEE, dd MMM yyyy HH:mm:ss", "EEE dd MMM yyyy HH:mm:ss zzz",
45         "EEE dd MMM yyyy HH:mm:ss", "EEE MMM dd yyyy HH:mm:ss zzz", "EEE MMM dd yyyy HH:mm:ss",
46         "EEE MMM-dd-yyyy HH:mm:ss zzz", "EEE MMM-dd-yyyy HH:mm:ss", "dd MMM yyyy HH:mm:ss zzz",
47         "dd MMM yyyy HH:mm:ss", "dd-MMM-yy HH:mm:ss zzz", "dd-MMM-yy HH:mm:ss", "MMM dd HH:mm:ss yyyy zzz",
48         "MMM dd HH:mm:ss yyyy", "EEE MMM dd HH:mm:ss yyyy zzz",
49         "EEE, MMM dd HH:mm:ss yyyy zzz", "EEE, MMM dd HH:mm:ss yyyy", "EEE, dd-MMM-yy HH:mm:ss zzz",
50         "EEE dd-MMM-yy HH:mm:ss zzz", "EEE dd-MMM-yy HH:mm:ss",
51     };
52
53     public static long parseDate(String date)
54     {
55         return __dateParser.get().parse(date);
56     }
57
58     private static final ThreadLocal<DateParser> __dateParser =new ThreadLocal<DateParser>()
59     {
60         @Override
61         protected DateParser initialValue()
62         {
63             return new DateParser();
64         }
65     };
66     
67     final SimpleDateFormat _dateReceive[]= new SimpleDateFormat[__dateReceiveFmt.length];
68
69     private long parse(final String dateVal)
70     {
71         for (int i = 0; i < _dateReceive.length; i++)
72         {
73             if (_dateReceive[i] == null)
74             {
75                 _dateReceive[i] = new SimpleDateFormat(__dateReceiveFmt[i], Locale.US);
76                 _dateReceive[i].setTimeZone(__GMT);
77             }
78
79             try
80             {
81                 Date date = (Date) _dateReceive[i].parseObject(dateVal);
82                 return date.getTime();
83             }
84             catch (java.lang.Exception e)
85             {
86                 // LOG.ignore(e);
87             }
88         }
89
90         if (dateVal.endsWith(" GMT"))
91         {
92             final String val = dateVal.substring(0, dateVal.length() - 4);
93
94             for (SimpleDateFormat element : _dateReceive)
95             {
96                 try
97                 {
98                     Date date = (Date) element.parseObject(val);
99                     return date.getTime();
100                 }
101                 catch (java.lang.Exception e)
102                 {
103                     // LOG.ignore(e);
104                 }
105             }
106         }
107         return -1;
108     }
109 }