Skip to main content
summaryrefslogtreecommitdiffstats
blob: 17fc8d0c49d25dcc830e2582c9e5f2d87ca90dd8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*******************************************************************************
 * Copyright (c) 1999, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.http;

import java.util.*;

/**
 * <b>HTTPDate</b> provides several services with regards to dates and date formatting.
 */
public class HttpDate {

	protected Calendar time;
	private boolean valid = true;

	private static final String[] MONTH_NAMES = {HttpMsg.Jan_1, HttpMsg.Feb_2, HttpMsg.Mar_3, HttpMsg.Apr_4, HttpMsg.May_5, HttpMsg.Jun_6, HttpMsg.Jul_7, HttpMsg.Aug_8, HttpMsg.Sep_9, HttpMsg.Oct_10, HttpMsg.Nov_11, HttpMsg.Dec_12};
	private static final String[] WEEK_DAYS = {HttpMsg.Sun_13, HttpMsg.Mon_14, HttpMsg.Tue_15, HttpMsg.Wed_16, HttpMsg.Thu_17, HttpMsg.Fri_18, HttpMsg.Sat_19};

	private static TimeZone gmt = null;

	/**
	 * Constructs an HTTPDate object representing the current time.
	 */
	public HttpDate() { // Now...
		if (gmt == null) {
			gmt = TimeZone.getTimeZone("GMT"); //$NON-NLS-1$
		}
		time = new GregorianCalendar(gmt);
	}

	/**
	 * Constructs an HTTPDate object from the passed long.
	 * @param itime the number of uSeconds since the epoch.
	 */
	public HttpDate(long iTime) { // Raw mill seconds
		this();
		time.setTime(new Date(iTime));
	}

	/**
	 * Constructs an HTTPDate object from an RFC compliant string.
	 * @param iString the time in a formatted string.
	 */
	public HttpDate(String iString) { // RFC2068 3.3.1 compliant
		this();
		if (iString.indexOf(",") > 0) //$NON-NLS-1$
		{
			if (iString.indexOf("-") > 0) //$NON-NLS-1$
			{
				parseRFC1036(iString);
			} else {
				parseRFC1123(iString);
			}
		} else { //asctime type
			parseASC(iString);
		}
	}

	/**
	 * Constructs an HTTPDate object from the passed long.
	 * @param itime the number of uSeconds since the epoch.
	 */
	public HttpDate(Date iTime) { // Raw mill seconds
		this();
		time.setTime(iTime);
	}

	/**
	 * Returns the Date/Time time as an RFC 1123 formatted String
	 */
	public static String format(long time) {
		return (new HttpDate(time).toString());
	}

	protected void formatDoubleDigit(StringBuffer buff, int val) {
		if (val < 10) {
			buff.append('0');
		}

		buff.append(val);
	}

	/**
	 * Returns the time represented as a long.  This represents the number
	 * of uSeconds since the epoch.
	 * @return the time represented as a long.
	 */
	public long getAsLong() {
		return (time.getTime().getTime());
	}

	/**
	 * Returns the time represented as a string.
	 * @return the time represented as a string.
	 */
	public String getAsString() {
		return (toString());
	}

	/**
	 * Returns the day of the month of the internal time, starting at 1
	 * @return the day of the month.
	 */
	public int getDay() {
		return (time.get(Calendar.DATE));
	}

	/**
	 * Returns the day of the week of the internal time.
	 * The number is in the range of 0 to 6 where 0 represents Sunday.
	 * @return  the day of the week.
	 */
	public int getDayOfWeek() {
		return (time.get(Calendar.DAY_OF_WEEK) - 1);
	}

	/**
	 * Returns the hour of the day of the internal time. 24 Hour time
	 * @return  the hour.
	 */
	public int getHour() {
		return (time.get(Calendar.HOUR_OF_DAY));
	}

	/**
	 * Returns the minute of the hour of the internal time.
	 * @return  the minute.
	 */
	public int getMin() {
		return (time.get(Calendar.MINUTE));
	}

	/**
	 * Returns the month of the year of the internal time.
	 * The number is in the range of 0 to 11 where 0 represents January.
	 * @return  the month of the year.
	 */
	public int getMonth() {
		return (time.get(Calendar.MONTH));
	}

	/**
	 * Returns the second of the minute of the internal time.
	 * @return  the second.
	 */
	public int getSec() {
		return (time.get(Calendar.SECOND));
	}

	/**
	 * Returns the year of the internal time.
	 * @return  the year.
	 */
	public int getYear() {
		return (time.get(Calendar.YEAR));
	}

	/**
	 * Checks wether or not the Date this object represents is valid.
	 *  It would be Invalid if the string used to construct the object was a NON
	 * RFC 1123, 1036 or ASC Time conforming Date String.
	 * @return true if this object represents a REAL date.
	 */
	public boolean isValid() {
		return (valid);
	}

	protected int locateMonth(String monthString) throws NumberFormatException {
		for (int i = 0; i < 12; i++) {
			if (MONTH_NAMES[i].equals(monthString)) {
				return (i);
			}
		}
		throw new NumberFormatException("Invalid month: " + monthString); //$NON-NLS-1$
	}

	/**
	 * Returns the current Date/Time as an RFC ASC Time formatted String
	 */
	public static String now() {
		return (new HttpDate().toString());
	}

	protected void parseASC(String str) {
		//ASCTIMEFMT  = "EEE MMM dD HH:MM:SS YYYY"
		//               012345678901234567890123

		int day, month, year, hour, min, sec;

		parsedate: try {
			/* ignore day */

			if (str.charAt(3) != ' ') {
				break parsedate;
			}

			month = locateMonth(str.substring(4, 7));

			if (str.charAt(7) != ' ') {
				break parsedate;
			}

			if (str.charAt(8) == ' ') {
				day = Integer.parseInt(str.substring(9, 10));
			} else {
				day = Integer.parseInt(str.substring(8, 10));
			}

			if (str.charAt(10) != ' ') {
				break parsedate;
			}

			hour = Integer.parseInt(str.substring(11, 13));

			if (str.charAt(13) != ':') {
				break parsedate;
			}

			min = Integer.parseInt(str.substring(14, 16));

			if (str.charAt(16) != ':') {
				break parsedate;
			}

			sec = Integer.parseInt(str.substring(17, 19));

			if (str.charAt(19) != ' ') {
				break parsedate;
			}

			year = Integer.parseInt(str.substring(20));

			time.set(year, month, day, hour, min, sec);
			return;
		} catch (NumberFormatException e) {
			valid = false;
		}		
	}

	protected void parseRFC1036(String str) {
		//RFC1036DATEFMT = "EEEE, DD-MMM-YY HH:MM:SS ZZZ"
		//                        0123456789012345678901

		int day, month, year, hour, min, sec;
		parsedate: try {
			/* skip past day */

			int i = str.indexOf(", "); //$NON-NLS-1$

			if (i == -1) {
				break parsedate;
			}

			str = str.substring(i + 2);

			day = Integer.parseInt(str.substring(0, 2));

			if (str.charAt(2) != '-') {
				break parsedate;
			}

			month = locateMonth(str.substring(3, 6));

			if (str.charAt(6) != '-') {
				break parsedate;
			}

			year = Integer.parseInt(str.substring(7, 9));
			year += (year < 70) ? 2000 : 1900; /* y2k window */

			if (str.charAt(9) != ' ') {
				break parsedate;
			}

			hour = Integer.parseInt(str.substring(10, 12));

			if (str.charAt(12) != ':') {
				break parsedate;
			}

			min = Integer.parseInt(str.substring(13, 15));

			if (str.charAt(15) != ':') {
				break parsedate;
			}

			sec = Integer.parseInt(str.substring(16, 18));

			if (str.charAt(18) != ' ') {
				break parsedate;
			}

			time.set(year, month, day, hour, min, sec);
		} catch (NumberFormatException e) {
			valid = false;
		}
	}

	protected void parseRFC1123(String str) {
		//RFC1123DATEFMT = "EEE, DD MMM YYYY HH:MM:SS ZZZ"
		//                  01234567890123456789012345678

		int day, month, year, hour, min, sec;

		parsedate: try {
			/* ignore day */

			if ((str.charAt(3) != ',') || (str.charAt(4) != ' ')) {
				break parsedate;
			}

			day = Integer.parseInt(str.substring(5, 7));

			if (str.charAt(7) != ' ') {
				break parsedate;
			}

			month = locateMonth(str.substring(8, 11));

			if (str.charAt(11) != ' ') {
				break parsedate;
			}

			year = Integer.parseInt(str.substring(12, 16));

			if (str.charAt(16) != ' ') {
				break parsedate;
			}

			hour = Integer.parseInt(str.substring(17, 19));

			if (str.charAt(19) != ':') {
				break parsedate;
			}

			min = Integer.parseInt(str.substring(20, 22));

			if (str.charAt(22) != ':') {
				break parsedate;
			}

			sec = Integer.parseInt(str.substring(23, 25));

			if (str.charAt(25) != ' ') {
				break parsedate;
			}

			time.set(year, month, day, hour, min, sec);
		} catch (NumberFormatException e) {
			valid = false;
		}


	}

	/**
	 * Returns the time represented as an RFC1123 string.
	 * @return the time represented as a string.
	 */
	public String toString() {
		StringBuffer buff = new StringBuffer(30);
		buff.append(WEEK_DAYS[getDayOfWeek()]);
		buff.append(", "); //$NON-NLS-1$
		formatDoubleDigit(buff, getDay());
		buff.append(' ');
		buff.append(MONTH_NAMES[getMonth()]);
		buff.append(' ');
		buff.append(getYear());
		buff.append(' ');

		formatDoubleDigit(buff, getHour());
		buff.append(':');
		formatDoubleDigit(buff, getMin());
		buff.append(':');
		formatDoubleDigit(buff, getSec());
		buff.append(' ');
		buff.append(time.getTimeZone().getID());

		return (buff.toString());
	}
}

Back to the top