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.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.http;
21 import java.util.Calendar;
22 import java.util.GregorianCalendar;
23 import java.util.TimeZone;
25 import org.eclipse.jetty.util.StringUtil;
28 * ThreadLocal Date formatters for HTTP style dates.
31 public class DateGenerator
33 private static final TimeZone __GMT = TimeZone.getTimeZone("GMT");
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"};
45 private static final ThreadLocal<DateGenerator> __dateGenerator =new ThreadLocal<DateGenerator>()
48 protected DateGenerator initialValue()
50 return new DateGenerator();
55 public final static String __01Jan1970=DateGenerator.formatDate(0);
58 * Format HTTP date "EEE, dd MMM yyyy HH:mm:ss 'GMT'"
60 public static String formatDate(long date)
62 return __dateGenerator.get().doFormatDate(date);
66 * Format "EEE, dd-MMM-yyyy HH:mm:ss 'GMT'" for cookies
68 public static void formatCookieDate(StringBuilder buf, long date)
70 __dateGenerator.get().doFormatCookieDate(buf,date);
74 * Format "EEE, dd-MMM-yyyy HH:mm:ss 'GMT'" for cookies
76 public static String formatCookieDate(long date)
78 StringBuilder buf = new StringBuilder(28);
79 formatCookieDate(buf, date);
80 return buf.toString();
83 private final StringBuilder buf = new StringBuilder(32);
84 private final GregorianCalendar gc = new GregorianCalendar(__GMT);
87 * Format HTTP date "EEE, dd MMM yyyy HH:mm:ss 'GMT'"
89 public String doFormatDate(long date)
92 gc.setTimeInMillis(date);
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;
101 int hours = gc.get(Calendar.HOUR_OF_DAY);
102 int minutes = gc.get(Calendar.MINUTE);
103 int seconds = gc.get(Calendar.SECOND);
105 buf.append(DAYS[day_of_week]);
108 StringUtil.append2digits(buf, day_of_month);
111 buf.append(MONTHS[month]);
113 StringUtil.append2digits(buf, century);
114 StringUtil.append2digits(buf, year);
117 StringUtil.append2digits(buf, hours);
119 StringUtil.append2digits(buf, minutes);
121 StringUtil.append2digits(buf, seconds);
123 return buf.toString();
127 * Format "EEE, dd-MMM-yy HH:mm:ss 'GMT'" for cookies
129 public void doFormatCookieDate(StringBuilder buf, long date)
131 gc.setTimeInMillis(date);
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);
139 int epoch = (int) ((date / 1000) % (60 * 60 * 24));
140 int seconds = epoch % 60;
142 int minutes = epoch % 60;
143 int hours = epoch / 60;
145 buf.append(DAYS[day_of_week]);
148 StringUtil.append2digits(buf, day_of_month);
151 buf.append(MONTHS[month]);
153 StringUtil.append2digits(buf, year/100);
154 StringUtil.append2digits(buf, year%100);
157 StringUtil.append2digits(buf, hours);
159 StringUtil.append2digits(buf, minutes);
161 StringUtil.append2digits(buf, seconds);