/[classpath]/inetlib/source/gnu/inet/http/HTTPDateFormat.java
ViewVC logotype

Diff of /inetlib/source/gnu/inet/http/HTTPDateFormat.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.3 by dog, Thu Oct 21 15:21:55 2004 UTC revision 1.4 by dog, Thu Nov 25 22:15:05 2004 UTC
# Line 63  public class HTTPDateFormat Line 63  public class HTTPDateFormat
63      "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"      "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
64    };    };
65    
66    public HTTPDateFormat ()    public HTTPDateFormat()
67    {    {
68      calendar = new GregorianCalendar (TimeZone.getTimeZone ("GMT"));      calendar = new GregorianCalendar(TimeZone.getTimeZone ("GMT"));
69      numberFormat = new DecimalFormat ();      numberFormat = new DecimalFormat();
70    }    }
71    
72    /**    /**
# Line 78  public class HTTPDateFormat Line 78  public class HTTPDateFormat
78     * @param field the current field position     * @param field the current field position
79     * @return the modified buffer     * @return the modified buffer
80     */     */
81    public StringBuffer format (Date date, StringBuffer buf,    public StringBuffer format(Date date, StringBuffer buf,
82        FieldPosition field)                               FieldPosition field)
83    {    {
84      calendar.clear ();      calendar.clear();
85      calendar.setTime (date);      calendar.setTime(date);
86      buf.setLength (0);      buf.setLength(0);
87    
88      // Day of week      // Day of week
89      buf.append (DAYS_OF_WEEK[calendar.get (Calendar.DAY_OF_WEEK)]);      buf.append(DAYS_OF_WEEK[calendar.get(Calendar.DAY_OF_WEEK)]);
90      buf.append (',');      buf.append(',');
91      buf.append (' ');      buf.append(' ');
92    
93      // Day of month      // Day of month
94      int day = calendar.get (Calendar.DAY_OF_MONTH);      int day = calendar.get(Calendar.DAY_OF_MONTH);
95      buf.append (Character.forDigit (day / 10, 10));      buf.append(Character.forDigit(day / 10, 10));
96      buf.append (Character.forDigit (day % 10, 10));      buf.append(Character.forDigit(day % 10, 10));
97      buf.append (' ');      buf.append(' ');
98    
99      // Month      // Month
100      buf.append (MONTHS[calendar.get (Calendar.MONTH)]);      buf.append(MONTHS[calendar.get(Calendar.MONTH)]);
101      buf.append (' ');      buf.append(' ');
102    
103      // Year      // Year
104      int year = calendar.get (Calendar.YEAR);      int year = calendar.get(Calendar.YEAR);
105      if (year < 1000)      if (year < 1000)
106        {        {
107          buf.append ('0');          buf.append('0');
108          if (year < 100)          if (year < 100)
109            {            {
110              buf.append ('0');              buf.append('0');
111              if (year < 10)              if (year < 10)
112                {                {
113                  buf.append ('0');                  buf.append('0');
114                }                }
115            }            }
116        }        }
117      buf.append (Integer.toString (year));      buf.append(Integer.toString(year));
118      buf.append (' ');      buf.append(' ');
119    
120      // Hour      // Hour
121      int hour = calendar.get (Calendar.HOUR_OF_DAY);      int hour = calendar.get(Calendar.HOUR_OF_DAY);
122      buf.append (Character.forDigit (hour / 10, 10));      buf.append(Character.forDigit(hour / 10, 10));
123      buf.append (Character.forDigit (hour % 10, 10));      buf.append(Character.forDigit(hour % 10, 10));
124      buf.append (':');      buf.append(':');
125    
126      // Minute      // Minute
127      int minute = calendar.get (Calendar.MINUTE);      int minute = calendar.get(Calendar.MINUTE);
128      buf.append (Character.forDigit (minute / 10, 10));      buf.append(Character.forDigit(minute / 10, 10));
129      buf.append (Character.forDigit (minute % 10, 10));      buf.append(Character.forDigit(minute % 10, 10));
130      buf.append (':');      buf.append(':');
131    
132      // Second      // Second
133      int second = calendar.get (Calendar.SECOND);      int second = calendar.get(Calendar.SECOND);
134      buf.append (Character.forDigit (second / 10, 10));      buf.append(Character.forDigit(second / 10, 10));
135      buf.append (Character.forDigit (second % 10, 10));      buf.append(Character.forDigit(second % 10, 10));
136      buf.append (' ');      buf.append(' ');
137    
138      // Timezone      // Timezone
139      // Get time offset in minutes      // Get time offset in minutes
140      int zoneOffset = (calendar.get (Calendar.ZONE_OFFSET) +      int zoneOffset =(calendar.get(Calendar.ZONE_OFFSET) +
141                        calendar.get (Calendar.DST_OFFSET)) / 60000;                       calendar.get(Calendar.DST_OFFSET)) / 60000;
142            
143      // Apply + or - appropriately      // Apply + or - appropriately
144      if (zoneOffset < 0)      if (zoneOffset < 0)
145        {        {
146          zoneOffset = -zoneOffset;          zoneOffset = -zoneOffset;
147          buf.append ('-');          buf.append('-');
148        }        }
149      else      else
150        {        {
151          buf.append ('+');          buf.append('+');
152        }        }
153            
154      // Set the 2 2-char fields as specified above      // Set the 2 2-char fields as specified above
155      int tzhours = zoneOffset / 60;      int tzhours = zoneOffset / 60;
156      buf.append (Character.forDigit (tzhours / 10, 10));      buf.append(Character.forDigit(tzhours / 10, 10));
157      buf.append (Character.forDigit (tzhours % 10, 10));      buf.append(Character.forDigit(tzhours % 10, 10));
158      int tzminutes = zoneOffset % 60;      int tzminutes = zoneOffset % 60;
159      buf.append (Character.forDigit (tzminutes / 10, 10));      buf.append(Character.forDigit(tzminutes / 10, 10));
160      buf.append (Character.forDigit (tzminutes % 10, 10));      buf.append(Character.forDigit(tzminutes % 10, 10));
161    
162      field.setBeginIndex (0);      field.setBeginIndex(0);
163      field.setEndIndex (buf.length ());      field.setEndIndex(buf.length());
164      return buf;      return buf;
165    }    }
166    
# Line 169  public class HTTPDateFormat Line 169  public class HTTPDateFormat
169     * @param text the formatted date to be parsed     * @param text the formatted date to be parsed
170     * @param pos the current parse position     * @param pos the current parse position
171     */     */
172    public Date parse (String text, ParsePosition pos)    public Date parse(String text, ParsePosition pos)
173    {    {
174      int date, month, year, hour, minute, second;      int date, month, year, hour, minute, second;
175      String monthText;      String monthText;
176      int start = 0, end = -1;      int start = 0, end = -1;
177      int len = text.length ();      int len = text.length();
178      calendar.clear ();      calendar.clear();
179      pos.setIndex (start);      pos.setIndex(start);
180      try      try
181        {        {
182          // Advance to date          // Advance to date
183          if (Character.isLetter (text.charAt (start)))          if (Character.isLetter(text.charAt(start)))
184            {            {
185              start = skipNonWhitespace (text, start);              start = skipNonWhitespace(text, start);
186            }            }
187          // Determine mode          // Determine mode
188          switch (start)          switch(start)
189            {            {
190            case 3:            case 3:
191              // asctime              // asctime
192              start = skipWhitespace (text, start);              start = skipWhitespace(text, start);
193              pos.setIndex (start);              pos.setIndex(start);
194              end = skipNonWhitespace (text, start + 1);              end = skipNonWhitespace(text, start + 1);
195              monthText = text.substring (start, end);              monthText = text.substring(start, end);
196              month = -1;              month = -1;
197              for (int i = 0; i < 12; i++)              for (int i = 0; i < 12; i++)
198                {                {
199                  if (MONTHS[i].equals (monthText))                  if (MONTHS[i].equals(monthText))
200                    {                    {
201                      month = i;                      month = i;
202                      break;                      break;
# Line 204  public class HTTPDateFormat Line 204  public class HTTPDateFormat
204                }                }
205              if (month == -1)              if (month == -1)
206                {                {
207                  pos.setErrorIndex (end);                  pos.setErrorIndex(end);
208                  return null;                  return null;
209                }                }
210              // Advance to date              // Advance to date
211              start = skipWhitespace (text, end + 1);              start = skipWhitespace(text, end + 1);
212              pos.setIndex (start);              pos.setIndex(start);
213              end = skipNonWhitespace (text, start + 1);              end = skipNonWhitespace(text, start + 1);
214              date = Integer.parseInt (text.substring (start, end));              date = Integer.parseInt(text.substring(start, end));
215              // Advance to hour              // Advance to hour
216              start = skipWhitespace (text, end + 1);              start = skipWhitespace(text, end + 1);
217              pos.setIndex (start);              pos.setIndex(start);
218              end = skipTo (text, start + 1, ':');              end = skipTo(text, start + 1, ':');
219              hour = Integer.parseInt (text.substring (start, end));              hour = Integer.parseInt(text.substring(start, end));
220              // Advance to minute              // Advance to minute
221              start = end + 1;              start = end + 1;
222              pos.setIndex (start);              pos.setIndex(start);
223              end = skipTo (text, start + 1, ':');              end = skipTo(text, start + 1, ':');
224              minute = Integer.parseInt (text.substring (start, end));              minute = Integer.parseInt(text.substring(start, end));
225              // Advance to second              // Advance to second
226              start = end + 1;              start = end + 1;
227              pos.setIndex (start);              pos.setIndex(start);
228              end = skipNonWhitespace (text, start + 1);              end = skipNonWhitespace(text, start + 1);
229              second = Integer.parseInt (text.substring (start, end));              second = Integer.parseInt(text.substring(start, end));
230              // Advance to year              // Advance to year
231              start = skipWhitespace (text, end + 1);              start = skipWhitespace(text, end + 1);
232              pos.setIndex (start);              pos.setIndex(start);
233              end = skipNonWhitespace (text, start + 1);              end = skipNonWhitespace(text, start + 1);
234              year = Integer.parseInt (text.substring (start, end));              year = Integer.parseInt(text.substring(start, end));
235              break;              break;
236            case 0:            case 0:
237            case 4:            case 4:
238              // rfc822              // rfc822
239              start = skipWhitespace (text, start);              start = skipWhitespace(text, start);
240              pos.setIndex (start);              pos.setIndex(start);
241              end = skipNonWhitespace (text, start + 1);              end = skipNonWhitespace(text, start + 1);
242              date = Integer.parseInt (text.substring (start, end));              date = Integer.parseInt(text.substring(start, end));
243              // Advance to month              // Advance to month
244              start = skipWhitespace (text, end + 1);              start = skipWhitespace(text, end + 1);
245              pos.setIndex (start);              pos.setIndex(start);
246              end = skipNonWhitespace (text, start + 1);              end = skipNonWhitespace(text, start + 1);
247              monthText = text.substring (start, end);              monthText = text.substring(start, end);
248              month = -1;              month = -1;
249              for (int i = 0; i < 12; i++)              for (int i = 0; i < 12; i++)
250                {                {
251                  if (MONTHS[i].equals (monthText))                  if (MONTHS[i].equals(monthText))
252                    {                    {
253                      month = i;                      month = i;
254                      break;                      break;
# Line 256  public class HTTPDateFormat Line 256  public class HTTPDateFormat
256                }                }
257              if (month == -1)              if (month == -1)
258                {                {
259                  pos.setErrorIndex (end);                  pos.setErrorIndex(end);
260                  return null;                  return null;
261                }                }
262              // Advance to year              // Advance to year
263              start = skipWhitespace (text, end + 1);              start = skipWhitespace(text, end + 1);
264              pos.setIndex (start);              pos.setIndex(start);
265              end = skipNonWhitespace (text, start + 1);              end = skipNonWhitespace(text, start + 1);
266              year = Integer.parseInt (text.substring (start, end));              year = Integer.parseInt(text.substring(start, end));
267              // Advance to hour              // Advance to hour
268              start = skipWhitespace (text, end + 1);              start = skipWhitespace(text, end + 1);
269              pos.setIndex (start);              pos.setIndex(start);
270              end = skipTo (text, start + 1, ':');              end = skipTo(text, start + 1, ':');
271              hour = Integer.parseInt (text.substring (start, end));              hour = Integer.parseInt(text.substring(start, end));
272              // Advance to minute              // Advance to minute
273              start = end + 1;              start = end + 1;
274              pos.setIndex (start);              pos.setIndex(start);
275              end = skipTo (text, start + 1, ':');              end = skipTo(text, start + 1, ':');
276              minute = Integer.parseInt (text.substring (start, end));              minute = Integer.parseInt(text.substring(start, end));
277              // Advance to second              // Advance to second
278              start = end + 1;              start = end + 1;
279              pos.setIndex (start);              pos.setIndex(start);
280              end = start + 1;              end = start + 1;
281              while (end < len && !Character.isWhitespace (text.charAt (end)))              while (end < len && !Character.isWhitespace(text.charAt(end)))
282                {                {
283                  end++;                  end++;
284                }                }
285              second = Integer.parseInt (text.substring (start, end));              second = Integer.parseInt(text.substring(start, end));
286              break;              break;
287            default:            default:
288              // rfc850 (obsolete)              // rfc850(obsolete)
289              start = skipWhitespace (text, start);              start = skipWhitespace(text, start);
290              pos.setIndex (start);              pos.setIndex(start);
291              end = skipTo (text, start + 1, '-');              end = skipTo(text, start + 1, '-');
292              date = Integer.parseInt (text.substring (start, end));              date = Integer.parseInt(text.substring(start, end));
293              // Advance to month              // Advance to month
294              start = end + 1;              start = end + 1;
295              pos.setIndex (start);              pos.setIndex(start);
296              end = skipTo (text, start + 1, '-');              end = skipTo(text, start + 1, '-');
297              monthText = text.substring (start, end);              monthText = text.substring(start, end);
298              month = -1;              month = -1;
299              for (int i = 0; i < 12; i++)              for (int i = 0; i < 12; i++)
300                {                {
301                  if (MONTHS[i].equals (monthText))                  if (MONTHS[i].equals(monthText))
302                    {                    {
303                      month = i;                      month = i;
304                      break;                      break;
# Line 306  public class HTTPDateFormat Line 306  public class HTTPDateFormat
306                }                }
307              if (month == -1)              if (month == -1)
308                {                {
309                  pos.setErrorIndex (end);                  pos.setErrorIndex(end);
310                  return null;                  return null;
311                }                }
312              // Advance to year              // Advance to year
313              start = end + 1;              start = end + 1;
314              pos.setIndex (start);              pos.setIndex(start);
315              end = skipNonWhitespace (text, start + 1);              end = skipNonWhitespace(text, start + 1);
316              year = 1900 + Integer.parseInt (text.substring (start, end));              year = 1900 + Integer.parseInt(text.substring(start, end));
317              // Advance to hour              // Advance to hour
318              start = skipWhitespace (text, end + 1);              start = skipWhitespace(text, end + 1);
319              pos.setIndex (start);              pos.setIndex(start);
320              end = skipTo (text, start + 1, ':');              end = skipTo(text, start + 1, ':');
321              hour = Integer.parseInt (text.substring (start, end));              hour = Integer.parseInt(text.substring(start, end));
322              // Advance to minute              // Advance to minute
323              start = end + 1;              start = end + 1;
324              pos.setIndex (start);              pos.setIndex(start);
325              end = skipTo (text, start + 1, ':');              end = skipTo(text, start + 1, ':');
326              minute = Integer.parseInt (text.substring (start, end));              minute = Integer.parseInt(text.substring(start, end));
327              // Advance to second              // Advance to second
328              start = end + 1;              start = end + 1;
329              pos.setIndex (start);              pos.setIndex(start);
330              end = start + 1;              end = start + 1;
331              while (end < len && !Character.isWhitespace (text.charAt (end)))              while (end < len && !Character.isWhitespace(text.charAt(end)))
332                {                {
333                  end++;                  end++;
334                }                }
335              second = Integer.parseInt (text.substring (start, end));              second = Integer.parseInt(text.substring(start, end));
336            }            }
337                    
338          calendar.set (Calendar.YEAR, year);          calendar.set(Calendar.YEAR, year);
339          calendar.set (Calendar.MONTH, month);          calendar.set(Calendar.MONTH, month);
340          calendar.set (Calendar.DAY_OF_MONTH, date);          calendar.set(Calendar.DAY_OF_MONTH, date);
341          calendar.set (Calendar.HOUR, hour);          calendar.set(Calendar.HOUR, hour);
342          calendar.set (Calendar.MINUTE, minute);          calendar.set(Calendar.MINUTE, minute);
343          calendar.set (Calendar.SECOND, second);          calendar.set(Calendar.SECOND, second);
344                    
345          if (end != len)          if (end != len)
346            {            {
347              // Timezone              // Timezone
348              start = skipWhitespace (text, end + 1);              start = skipWhitespace(text, end + 1);
349              end = start + 1;              end = start + 1;
350              while (end < len && !Character.isWhitespace (text.charAt (end)))              while (end < len && !Character.isWhitespace(text.charAt(end)))
351                {                {
352                  end++;                  end++;
353                }                }
354              char pm = text.charAt (start);              char pm = text.charAt(start);
355              if (Character.isLetter (pm))              if (Character.isLetter(pm))
356                {                {
357                  TimeZone tz =                  TimeZone tz =
358                    TimeZone.getTimeZone (text.substring (start, end));                    TimeZone.getTimeZone(text.substring(start, end));
359                  calendar.set (Calendar.ZONE_OFFSET, tz.getRawOffset ());                  calendar.set(Calendar.ZONE_OFFSET, tz.getRawOffset());
360                }                }
361              else              else
362                {                {
363                  int zoneOffset = 0;                  int zoneOffset = 0;
364                  zoneOffset += 600 * Character.digit (text.charAt (++start), 10);                  zoneOffset += 600 * Character.digit(text.charAt(++start), 10);
365                  zoneOffset += 60 * Character.digit (text.charAt (++start), 10);                  zoneOffset += 60 * Character.digit(text.charAt(++start), 10);
366                  zoneOffset += 10 * Character.digit (text.charAt (++start), 10);                  zoneOffset += 10 * Character.digit(text.charAt(++start), 10);
367                  zoneOffset += Character.digit (text.charAt (++start), 10);                  zoneOffset += Character.digit(text.charAt(++start), 10);
368                  zoneOffset *= 60000; // minutes -> ms                  zoneOffset *= 60000; // minutes -> ms
369                  if ('-' == pm)                  if ('-' == pm)
370                    {                    {
371                      zoneOffset = -zoneOffset;                      zoneOffset = -zoneOffset;
372                    }                    }
373                  calendar.set (Calendar.ZONE_OFFSET, zoneOffset);                  calendar.set(Calendar.ZONE_OFFSET, zoneOffset);
374                }                }
375            }            }
376          pos.setIndex (end);          pos.setIndex(end);
377                    
378          return calendar.getTime();          return calendar.getTime();
379        }        }
380      catch (NumberFormatException e)      catch (NumberFormatException e)
381        {        {
382          pos.setErrorIndex (Math.max (start, end));          pos.setErrorIndex(Math.max(start, end));
383        }        }
384      catch (StringIndexOutOfBoundsException e)      catch (StringIndexOutOfBoundsException e)
385        {        {
386          pos.setErrorIndex (Math.max (start, end));          pos.setErrorIndex(Math.max(start, end));
387        }        }
388      return null;      return null;
389    }    }
390    
391    private int skipWhitespace (String text, int pos)    private int skipWhitespace(String text, int pos)
392    {    {
393      while (Character.isWhitespace (text.charAt (pos)))      while(Character.isWhitespace(text.charAt(pos)))
394        {        {
395          pos++;          pos++;
396        }        }
397      return pos;          return pos;    
398    }    }
399    
400    private int skipNonWhitespace (String text, int pos)    private int skipNonWhitespace(String text, int pos)
401    {    {
402      while (!Character.isWhitespace (text.charAt (pos)))      while(!Character.isWhitespace(text.charAt(pos)))
403        {        {
404          pos++;          pos++;
405        }        }
406      return pos;          return pos;    
407    }    }
408    
409    private int skipTo (String text, int pos, char c)    private int skipTo(String text, int pos, char c)
410    {    {
411      while (text.charAt (pos) != c)      while(text.charAt(pos) != c)
412        {        {
413          pos++;          pos++;
414        }        }
# Line 418  public class HTTPDateFormat Line 418  public class HTTPDateFormat
418    /**    /**
419     * Don't allow setting the calendar.     * Don't allow setting the calendar.
420     */     */
421    public void setCalendar (Calendar newCalendar)    public void setCalendar(Calendar newCalendar)
422    {    {
423      throw new UnsupportedOperationException ();      throw new UnsupportedOperationException();
424    }    }
425    
426    /**    /**
427     * Don't allow setting the NumberFormat.     * Don't allow setting the NumberFormat.
428     */     */
429    public void setNumberFormat (NumberFormat newNumberFormat)    public void setNumberFormat(NumberFormat newNumberFormat)
430    {    {
431      throw new UnsupportedOperationException ();      throw new UnsupportedOperationException();
432    }    }
433    
434  }  }
435    

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.4

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26