001: // HttpDate.java
002: // $Id: HttpDate.java,v 1.9 2002/07/17 14:22:10 ylafon Exp $$
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.www.http;
007:
008: import java.util.Date;
009: import java.util.Calendar;
010: import java.util.TimeZone;
011:
012: public class HttpDate extends BasicValue {
013:
014: protected static byte bdays[][] = {
015: { (byte) 'P', (byte) 'a', (byte) 'd' },
016: { (byte) 'S', (byte) 'u', (byte) 'n' },
017: { (byte) 'M', (byte) 'o', (byte) 'n' },
018: { (byte) 'T', (byte) 'u', (byte) 'e' },
019: { (byte) 'W', (byte) 'e', (byte) 'd' },
020: { (byte) 'T', (byte) 'h', (byte) 'u' },
021: { (byte) 'F', (byte) 'r', (byte) 'i' },
022: { (byte) 'S', (byte) 'a', (byte) 't' } };
023:
024: protected static byte bmonthes[][] = {
025: { (byte) 'J', (byte) 'a', (byte) 'n' },
026: { (byte) 'F', (byte) 'e', (byte) 'b' },
027: { (byte) 'M', (byte) 'a', (byte) 'r' },
028: { (byte) 'A', (byte) 'p', (byte) 'r' },
029: { (byte) 'M', (byte) 'a', (byte) 'y' },
030: { (byte) 'J', (byte) 'u', (byte) 'n' },
031: { (byte) 'J', (byte) 'u', (byte) 'l' },
032: { (byte) 'A', (byte) 'u', (byte) 'g' },
033: { (byte) 'S', (byte) 'e', (byte) 'p' },
034: { (byte) 'O', (byte) 'c', (byte) 't' },
035: { (byte) 'N', (byte) 'o', (byte) 'v' },
036: { (byte) 'D', (byte) 'e', (byte) 'c' } };
037:
038: protected Long date = null;
039:
040: protected Calendar cal = null;
041:
042: protected void parse() {
043: ParseState ps = new ParseState();
044: ps.ioff = roff;
045: ps.bufend = rlen;
046: date = new Long(HttpParser.parseDateOrDeltaSeconds(raw, ps));
047: }
048:
049: protected void updateByteValue() {
050: if (cal == null) {
051: TimeZone tz = TimeZone.getTimeZone("UTC");
052: cal = Calendar.getInstance(tz);
053: }
054: Date now = new Date(date.longValue());
055: cal.setTime(now);
056: // Dump the date, according to HTTP/1.1 prefered format
057: byte buf[] = new byte[29];
058: int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
059: int j = 0;
060: for (int i = 0; i < 3; i++) {
061: buf[j++] = bdays[dayofweek][i];
062: }
063: buf[j++] = (byte) ',';
064: buf[j++] = (byte) ' ';
065: int day = cal.get(Calendar.DAY_OF_MONTH);
066: if (day < 10) {
067: buf[j++] = 48;
068: buf[j++] = (byte) (48 + day);
069: } else {
070: buf[j++] = (byte) (48 + (day / 10));
071: buf[j++] = (byte) (48 + (day % 10));
072: }
073: buf[j++] = (byte) ' ';
074: int month = cal.get(Calendar.MONTH);
075: for (int i = 0; i < 3; i++) {
076: buf[j++] = bmonthes[month][i];
077: }
078: buf[j++] = (byte) ' ';
079: int year = cal.get(Calendar.YEAR);
080: // not y10k compliant
081: buf[j + 3] = (byte) (48 + (year % 10));
082: year = year / 10;
083: buf[j + 2] = (byte) (48 + (year % 10));
084: year = year / 10;
085: buf[j + 1] = (byte) (48 + (year % 10));
086: year = year / 10;
087: buf[j] = (byte) (48 + year);
088: j += 4;
089: buf[j++] = (byte) ' ';
090: int hour = cal.get(Calendar.HOUR_OF_DAY);
091: if (hour < 10) {
092: buf[j++] = (byte) 48;
093: buf[j++] = (byte) (48 + hour);
094: } else {
095: buf[j++] = (byte) (48 + (hour / 10));
096: buf[j++] = (byte) (48 + (hour % 10));
097: }
098: buf[j++] = (byte) ':';
099: int minute = cal.get(Calendar.MINUTE);
100: if (minute < 10) {
101: buf[j++] = (byte) 48;
102: buf[j++] = (byte) (48 + minute);
103: } else {
104: buf[j++] = (byte) (48 + (minute / 10));
105: buf[j++] = (byte) (48 + (minute % 10));
106: }
107: buf[j++] = (byte) ':';
108: int second = cal.get(Calendar.SECOND);
109: if (second < 10) {
110: buf[j++] = (byte) 48;
111: buf[j++] = (byte) (48 + second);
112: } else {
113: buf[j++] = (byte) (48 + (second / 10));
114: buf[j++] = (byte) (48 + (second % 10));
115: }
116: buf[j++] = (byte) ' ';
117: buf[j++] = (byte) 'G';
118: buf[j++] = (byte) 'M';
119: buf[j++] = (byte) 'T';
120: raw = buf;
121: roff = 0;
122: rlen = raw.length;
123: }
124:
125: /**
126: * Get the date value.
127: * @return A Long giving the date as a number of mmilliseconds since epoch.
128: */
129:
130: public Object getValue() {
131: validate();
132: return date;
133: }
134:
135: /**
136: * Set this date object value.
137: * @param date The new date value, as the number of milliseconds since
138: * epoch.
139: */
140:
141: public void setValue(long date) {
142: if (date == this .date.longValue())
143: return;
144: invalidateByteValue();
145: this .date = new Long(date);
146: this .isValid = true;
147: }
148:
149: HttpDate(boolean isValid, long date) {
150: this .isValid = isValid;
151: this .date = new Long(date);
152: }
153:
154: HttpDate() {
155: this .isValid = false;
156: }
157:
158: }
|