calypso.util
Class NetworkDate

java.lang.Object
  |
  +--calypso.util.NetworkDate

public class NetworkDate
extends java.lang.Object

Parses a date out of a string of bytes. Call it like this:

Note that it operates on bytes, not chars, since network dates are always ASCII.

Why would you want to use this instead of java.text.DateFormat.parse()? Because this algorithm has been tested in the field against real-world message headers for several years (the C code hadn't changed substantively since Netscape 2.0.) (There had been DST-related problems, but the tokenizer/parser was always sound.)

See Also:
Date, Calendar, DateFormat

Method Summary
static java.util.Date parseDate(ByteBuf string)
          The same, but assumes GMT is the default timezone.
static java.util.Date parseDate(ByteBuf string, boolean default_to_gmt)
          Like parseLong(), but returns a new Date object instead.
static long parseLong(byte[] string, int start, int end, boolean default_to_gmt)
          The same, but takes a byte array and a region within it, instead of a ByteBuf.
static long parseLong(ByteBuf string)
          The same, but assumes GMT is the default timezone.
static long parseLong(ByteBuf buf, boolean default_to_gmt)
          This parses a time/date string into a Time object.
static long UTC(int year, int month, int date, int hour, int min, int sec)
          Composes the given date into a number suitable for passing to new Date(n).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

parseLong

public static long parseLong(ByteBuf buf,
                             boolean default_to_gmt)
This parses a time/date string into a Time object. If it can't be parsed, -1 is returned.

Many formats are handled, including:

But note that 6/5/95 is ambiguous, since it's not obvious which is the day and which is the month. (6/13/95 is not ambiguous, however.)

Parameters:
buf - The bytes to parse. This assumes the input to be a sequence of 8-bit ASCII characters. High-bit characters are handled; 16-bit Unicode characters are not.
default_to_gmt - If the input string doesn't contain a description of the timezone, then this argument determines whether the string is interpreted relative to the local time zone (false) or to GMT (true). The correct value to pass in for this argument depends on what standard specified the time string which you are parsing; for RFC822 dates, this argument should be true.
Returns:
Microseconds since Jan 1, 1900, GMT. You can pass this to new Date(long). Returns -1 if the string is unparsable (no other negative value will ever be returned; dates before the Epoch are not handled.)

parseLong

public static long parseLong(byte[] string,
                             int start,
                             int end,
                             boolean default_to_gmt)
The same, but takes a byte array and a region within it, instead of a ByteBuf.

parseLong

public static long parseLong(ByteBuf string)
The same, but assumes GMT is the default timezone.

parseDate

public static java.util.Date parseDate(ByteBuf string,
                                       boolean default_to_gmt)
Like parseLong(), but returns a new Date object instead.

parseDate

public static java.util.Date parseDate(ByteBuf string)
The same, but assumes GMT is the default timezone.

UTC

public static long UTC(int year,
                       int month,
                       int date,
                       int hour,
                       int min,
                       int sec)
Composes the given date into a number suitable for passing to new Date(n). This is the number of milliseconds past the Epoch.