View Javadoc

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