View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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.util.Calendar;
22  import java.util.GregorianCalendar;
23  import java.util.TimeZone;
24  
25  import org.eclipse.jetty.util.StringUtil;
26  
27  /**
28   * ThreadLocal Date formatters for HTTP style dates.
29   *
30   */
31  public class DateGenerator
32  {
33      private static final TimeZone __GMT = TimeZone.getTimeZone("GMT");
34      static
35      {
36          __GMT.setID("GMT");
37      }
38      
39      static final String[] DAYS =
40          { "Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
41      static final String[] MONTHS =
42          { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan"};
43  
44  
45      private static final ThreadLocal<DateGenerator> __dateGenerator =new ThreadLocal<DateGenerator>()
46      {
47          @Override
48          protected DateGenerator initialValue()
49          {
50              return new DateGenerator();
51          }
52      };
53  
54  
55      public final static String __01Jan1970=DateGenerator.formatDate(0);
56      
57      /**
58       * Format HTTP date "EEE, dd MMM yyyy HH:mm:ss 'GMT'"
59       */
60      public static String formatDate(long date)
61      {
62          return __dateGenerator.get().doFormatDate(date);
63      }
64  
65      /**
66       * Format "EEE, dd-MMM-yyyy HH:mm:ss 'GMT'" for cookies
67       */
68      public static void formatCookieDate(StringBuilder buf, long date)
69      {
70          __dateGenerator.get().doFormatCookieDate(buf,date);
71      }
72  
73      /**
74       * Format "EEE, dd-MMM-yyyy HH:mm:ss 'GMT'" for cookies
75       */
76      public static String formatCookieDate(long date)
77      {
78          StringBuilder buf = new StringBuilder(28);
79          formatCookieDate(buf, date);
80          return buf.toString();
81      }
82      
83      private final StringBuilder buf = new StringBuilder(32);
84      private final GregorianCalendar gc = new GregorianCalendar(__GMT);
85  
86      /**
87       * Format HTTP date "EEE, dd MMM yyyy HH:mm:ss 'GMT'"
88       */
89      public String doFormatDate(long date)
90      {
91          buf.setLength(0);
92          gc.setTimeInMillis(date);
93  
94          int day_of_week = gc.get(Calendar.DAY_OF_WEEK);
95          int day_of_month = gc.get(Calendar.DAY_OF_MONTH);
96          int month = gc.get(Calendar.MONTH);
97          int year = gc.get(Calendar.YEAR);
98          int century = year / 100;
99          year = year % 100;
100 
101         int hours = gc.get(Calendar.HOUR_OF_DAY);
102         int minutes = gc.get(Calendar.MINUTE);
103         int seconds = gc.get(Calendar.SECOND);
104 
105         buf.append(DAYS[day_of_week]);
106         buf.append(',');
107         buf.append(' ');
108         StringUtil.append2digits(buf, day_of_month);
109 
110         buf.append(' ');
111         buf.append(MONTHS[month]);
112         buf.append(' ');
113         StringUtil.append2digits(buf, century);
114         StringUtil.append2digits(buf, year);
115 
116         buf.append(' ');
117         StringUtil.append2digits(buf, hours);
118         buf.append(':');
119         StringUtil.append2digits(buf, minutes);
120         buf.append(':');
121         StringUtil.append2digits(buf, seconds);
122         buf.append(" GMT");
123         return buf.toString();
124     }
125 
126     /**
127      * Format "EEE, dd-MMM-yy HH:mm:ss 'GMT'" for cookies
128      */
129     public void doFormatCookieDate(StringBuilder buf, long date)
130     {
131         gc.setTimeInMillis(date);
132 
133         int day_of_week = gc.get(Calendar.DAY_OF_WEEK);
134         int day_of_month = gc.get(Calendar.DAY_OF_MONTH);
135         int month = gc.get(Calendar.MONTH);
136         int year = gc.get(Calendar.YEAR);
137         year = year % 10000;
138 
139         int epoch = (int) ((date / 1000) % (60 * 60 * 24));
140         int seconds = epoch % 60;
141         epoch = epoch / 60;
142         int minutes = epoch % 60;
143         int hours = epoch / 60;
144 
145         buf.append(DAYS[day_of_week]);
146         buf.append(',');
147         buf.append(' ');
148         StringUtil.append2digits(buf, day_of_month);
149 
150         buf.append('-');
151         buf.append(MONTHS[month]);
152         buf.append('-');
153         StringUtil.append2digits(buf, year/100);
154         StringUtil.append2digits(buf, year%100);
155 
156         buf.append(' ');
157         StringUtil.append2digits(buf, hours);
158         buf.append(':');
159         StringUtil.append2digits(buf, minutes);
160         buf.append(':');
161         StringUtil.append2digits(buf, seconds);
162         buf.append(" GMT");
163     }
164 }