org.eigenbase.util14
Class DateTimeUtil

java.lang.Object
  extended by org.eigenbase.util14.DateTimeUtil

public class DateTimeUtil
extends Object

Utility functions for datetime types: date, time, timestamp. Refactored from SqlParserUtil because they are required by the Jdbc driver. TODO: review methods for performance. Due to allocations required, it may be preferable to introduce a "formatter" with the required state.

Since:
Sep 28, 2006
Version:
$Id: //open/dev/farrago/src/org/eigenbase/util14/DateTimeUtil.java#9 $

Nested Class Summary
static class DateTimeUtil.PrecisionTime
          Helper class for parsePrecisionDateTimeLiteral(java.lang.String, java.lang.String, java.util.TimeZone)
 
Field Summary
static String DateFormatStr
          the SimpleDateFormat string for ISO dates, "yyyy-MM-dd"
static TimeZone defaultZone
          the Java default time zone
static TimeZone gmtZone
          the GMT time zone
static long MILLIS_PER_DAY
          The number of milliseconds in a day.
static String TimeFormatStr
          the SimpleDateFormat string for ISO times, "HH:mm:ss"
static String TimestampFormatStr
          the SimpleDateFormat string for ISO timestamps, "yyyy-MM-dd HH:mm:ss"
 
Constructor Summary
DateTimeUtil()
           
 
Method Summary
static void checkDateFormat(String pattern)
          Checks if the date/time format is valid
static TimeZone getTimeZone(Calendar cal)
          Gets the active time zone based on a Calendar argument
static SimpleDateFormat newDateFormat(String format)
          Creates a new date formatter with Farrago specific options.
static Calendar parseDateFormat(String s, String pattern, TimeZone tz)
          Parses a string using SimpleDateFormat and a given pattern.
private static Calendar parseDateFormat(String s, String pattern, TimeZone tz, ParsePosition pp)
          Parses a string using SimpleDateFormat and a given pattern.
static DateTimeUtil.PrecisionTime parsePrecisionDateTimeLiteral(String s, String pattern, TimeZone tz)
          Parses a string using SimpleDateFormat and a given pattern, and if present, parses a fractional seconds component.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DateFormatStr

public static final String DateFormatStr
the SimpleDateFormat string for ISO dates, "yyyy-MM-dd"

See Also:
Constant Field Values

TimeFormatStr

public static final String TimeFormatStr
the SimpleDateFormat string for ISO times, "HH:mm:ss"

See Also:
Constant Field Values

TimestampFormatStr

public static final String TimestampFormatStr
the SimpleDateFormat string for ISO timestamps, "yyyy-MM-dd HH:mm:ss"

See Also:
Constant Field Values

gmtZone

public static final TimeZone gmtZone
the GMT time zone


defaultZone

public static final TimeZone defaultZone
the Java default time zone


MILLIS_PER_DAY

public static long MILLIS_PER_DAY
The number of milliseconds in a day.

In the fennel calculator, this is the modulo 'mask' when converting TIMESTAMP values to DATE and TIME values.

Constructor Detail

DateTimeUtil

public DateTimeUtil()
Method Detail

parseDateFormat

private static Calendar parseDateFormat(String s,
                                        String pattern,
                                        TimeZone tz,
                                        ParsePosition pp)
Parses a string using SimpleDateFormat and a given pattern. This method parses a string at the specified parse position and if successful, updates the parse position to the index after the last character used. The parsing is strict and requires months to be less than 12, days to be less than 31, etc.

Parameters:
s - string to be parsed
pattern - SimpleDateFormat pattern
tz - time zone in which to interpret string. Defaults to the Java default time zone
pp - position to start parsing from
Returns:
a Calendar initialized with the parsed value, or null if parsing failed. If returned, the Calendar is configured to the GMT time zone.
"Precondition:"
pattern != null

parseDateFormat

public static Calendar parseDateFormat(String s,
                                       String pattern,
                                       TimeZone tz)
Parses a string using SimpleDateFormat and a given pattern. The entire string must match the pattern specified.

Parameters:
s - string to be parsed
pattern - SimpleDateFormat pattern
tz - time zone in which to interpret string. Defaults to the Java default time zone
Returns:
a Calendar initialized with the parsed value, or null if parsing failed. If returned, the Calendar is configured to the GMT time zone.
"Precondition:"
pattern != null

parsePrecisionDateTimeLiteral

public static DateTimeUtil.PrecisionTime parsePrecisionDateTimeLiteral(String s,
                                                                       String pattern,
                                                                       TimeZone tz)
Parses a string using SimpleDateFormat and a given pattern, and if present, parses a fractional seconds component. The fractional seconds component must begin with a decimal point ('.') followed by numeric digits. The precision is rounded to a maximum of 3 digits of fractional seconds precision (to obtain milliseconds).

Parameters:
s - string to be parsed
pattern - SimpleDateFormat pattern
tz - time zone in which to interpret string. Defaults to the local time zone
Returns:
a PrecisionTime initialized with the parsed value, or null if parsing failed. The PrecisionTime contains a GMT Calendar and a precision.
"Precondition:"
pattern != null

getTimeZone

public static TimeZone getTimeZone(Calendar cal)
Gets the active time zone based on a Calendar argument


checkDateFormat

public static void checkDateFormat(String pattern)
Checks if the date/time format is valid

Parameters:
pattern - SimpleDateFormat pattern
Throws:
IllegalArgumentException - if the given pattern is invalid

newDateFormat

public static SimpleDateFormat newDateFormat(String format)
Creates a new date formatter with Farrago specific options. Farrago parsing is strict and does not allow values such as day 0, month 13, etc.

Parameters:
format - SimpleDateFormat pattern