Package org.apache.calcite.avatica.util
Class DateTimeUtils
java.lang.Object
org.apache.calcite.avatica.util.DateTimeUtils
Utility functions for datetime types: date, time, timestamp.
Used 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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe SimpleDateFormat string for ISO dates, "yyyy-MM-dd".static final TimeZoneThe Java default time zone.static final intThe julian date of the epoch, 1970-01-01.static final TimeZoneDeprecated.static final longThe number of milliseconds in a day.static final longThe number of milliseconds in an hour.static final longThe number of milliseconds in a minute.static final longThe number of milliseconds in a second.static final longThe number of seconds in a day.static final StringThe SimpleDateFormat string for ISO times, "HH:mm:ss".static final StringThe SimpleDateFormat string for ISO timestamps, "yyyy-MM-dd HH:mm:ss".static final TimeZoneThe UTC time zone.static final CalendarCalendar set to the epoch (1970-01-01 00:00:00 UTC). -
Method Summary
Modifier and TypeMethodDescriptionstatic intaddMonths(int date, int m) Adds a given number of months to a date, represented as the number of days since the epoch.static longaddMonths(long timestamp, int m) Adds a given number of months to a timestamp, represented as the number of milliseconds since the epoch.static Calendarcalendar()Creates an instance ofCalendarin the root locale and UTC time zone.static voidcheckDateFormat(String pattern) Checks if the date/time format is validstatic intstatic intdigitCount(int v) static longfloorDiv(long x, long y) Divide, rounding towards negative infinity.static longfloorMod(long x, long y) Modulo, always returning a non-negative result.static TimeZonegetTimeZone(Calendar cal) Gets the active time zone based on a Calendar argumentstatic StringintervalDayTimeToString(long v, TimeUnitRange range, int scale) static StringintervalYearMonthToString(int v, TimeUnitRange range) static booleanReturns whether a value is anOffsetDateTime.static SimpleDateFormatnewDateFormat(String format) Creates a new date formatter with Farrago specific options.static StringBuildernumber(StringBuilder buf, int v, int n) static StringReturns the value of aOffsetDateTimeas a string.static CalendarparseDateFormat(String s, String pattern, TimeZone tz) Deprecated.static CalendarparseDateFormat(String s, DateFormat dateFormat, TimeZone tz) Parses a string usingSimpleDateFormatand a given pattern.static DateTimeUtils.PrecisionTimeparsePrecisionDateTimeLiteral(String s, String pattern, TimeZone tz) Deprecated.static DateTimeUtils.PrecisionTimeparsePrecisionDateTimeLiteral(String s, DateFormat dateFormat, TimeZone tz, int maxPrecision) Parses a string usingSimpleDateFormatand a given pattern, and if present, parses a fractional seconds component.static longpowerX(long a, long b) Cheap, unsafe, long power.static longresetDate(long timestamp) Resets to epoch (1970-01-01) the "date" part of a timestamp.static longresetTime(long timestamp) Resets to zero the "time" part of a timestamp.static intsubtractMonths(int date0, int date1) Finds the number of months between two dates, each represented as the number of days since the epoch.static intsubtractMonths(long t0, long t1) static longstatic intstatic inttimeStringToUnixDate(String v, int start) static longunixDateCeil(TimeUnitRange range, long date) static longunixDateExtract(TimeUnitRange range, long date) static longunixDateFloor(TimeUnitRange range, long date) static StringunixDateToString(int date) Helper for CAST({date} AS VARCHAR(n)).static intunixTimeExtract(TimeUnitRange range, int time) Extracts a time unit from a time value (milliseconds since midnight).static longunixTimestamp(int year, int month, int day, int hour, int minute, int second) static longunixTimestampCeil(TimeUnitRange range, long timestamp) static intunixTimestampExtract(TimeUnitRange range, long timestamp) Extracts a time unit from a UNIX date (milliseconds since epoch).static longunixTimestampFloor(TimeUnitRange range, long timestamp) static StringunixTimestampToString(long timestamp) Helper for CAST({timestamp} AS VARCHAR(n)).static StringunixTimestampToString(long timestamp, int precision) static StringunixTimeToString(int time) Helper for CAST({timestamp} AS VARCHAR(n)).static StringunixTimeToString(int time, int precision) static intymdToJulian(int year, int month, int day) Calculates the Julian Day Number for any valid date in the Gregorian calendar.static intymdToUnixDate(int year, int month, int day)
-
Field Details
-
EPOCH_JULIAN
public static final int EPOCH_JULIANThe julian date of the epoch, 1970-01-01.- See Also:
-
DATE_FORMAT_STRING
The SimpleDateFormat string for ISO dates, "yyyy-MM-dd".- See Also:
-
TIME_FORMAT_STRING
The SimpleDateFormat string for ISO times, "HH:mm:ss".- See Also:
-
TIMESTAMP_FORMAT_STRING
The SimpleDateFormat string for ISO timestamps, "yyyy-MM-dd HH:mm:ss".- See Also:
-
GMT_ZONE
Deprecated.UseUTC_ZONEThe GMT time zone. -
UTC_ZONE
The UTC time zone. -
DEFAULT_ZONE
The Java default time zone. -
MILLIS_PER_SECOND
public static final long MILLIS_PER_SECONDThe number of milliseconds in a second.- See Also:
-
MILLIS_PER_MINUTE
public static final long MILLIS_PER_MINUTEThe number of milliseconds in a minute.- See Also:
-
MILLIS_PER_HOUR
public static final long MILLIS_PER_HOURThe number of milliseconds in an hour.- See Also:
-
MILLIS_PER_DAY
public static final long MILLIS_PER_DAYThe number of milliseconds in a day.This is the modulo 'mask' used when converting TIMESTAMP values to DATE and TIME values.
- See Also:
-
SECONDS_PER_DAY
public static final long SECONDS_PER_DAYThe number of seconds in a day.- See Also:
-
ZERO_CALENDAR
Calendar set to the epoch (1970-01-01 00:00:00 UTC). Useful for initializing other values. Calendars are not immutable, so be careful not to screw up this object for everyone else.
-
-
Method Details
-
parseDateFormat
Deprecated. -
parseDateFormat
Parses a string usingSimpleDateFormatand a given pattern. The entire string must match the pattern specified.- Parameters:
s- string to be parseddateFormat- Date formattz- 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 UTC time zone.
-
parsePrecisionDateTimeLiteral
@Deprecated public static DateTimeUtils.PrecisionTime parsePrecisionDateTimeLiteral(String s, String pattern, TimeZone tz) Deprecated. -
parsePrecisionDateTimeLiteral
public static DateTimeUtils.PrecisionTime parsePrecisionDateTimeLiteral(String s, DateFormat dateFormat, TimeZone tz, int maxPrecision) Parses a string usingSimpleDateFormatand 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 parseddateFormat- Date formattz- time zone in which to interpret string. Defaults to the local time zone- Returns:
- a
PrecisionTimeinitialized with the parsed value, or null if parsing failed. The PrecisionTime contains a GMT Calendar and a precision.
-
getTimeZone
Gets the active time zone based on a Calendar argument -
checkDateFormat
Checks if the date/time format is valid- Parameters:
pattern-SimpleDateFormatpattern- Throws:
IllegalArgumentException- if the given pattern is invalid
-
newDateFormat
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-SimpleDateFormatpattern
-
unixTimestampToString
Helper for CAST({timestamp} AS VARCHAR(n)). -
unixTimestampToString
-
unixTimeToString
Helper for CAST({timestamp} AS VARCHAR(n)). -
unixTimeToString
-
unixDateToString
Helper for CAST({date} AS VARCHAR(n)). -
intervalYearMonthToString
-
number
-
digitCount
public static int digitCount(int v) -
powerX
public static long powerX(long a, long b) Cheap, unsafe, long power. power(2, 3) returns 8. -
intervalDayTimeToString
-
dateStringToUnixDate
-
timeStringToUnixDate
-
timeStringToUnixDate
-
timestampStringToUnixDate
-
unixDateExtract
-
unixTimestampExtract
Extracts a time unit from a UNIX date (milliseconds since epoch). -
unixTimeExtract
Extracts a time unit from a time value (milliseconds since midnight). -
resetTime
public static long resetTime(long timestamp) Resets to zero the "time" part of a timestamp. -
resetDate
public static long resetDate(long timestamp) Resets to epoch (1970-01-01) the "date" part of a timestamp. -
unixTimestampFloor
-
unixDateFloor
-
unixTimestampCeil
-
unixDateCeil
-
ymdToUnixDate
public static int ymdToUnixDate(int year, int month, int day) -
ymdToJulian
public static int ymdToJulian(int year, int month, int day) Calculates the Julian Day Number for any valid date in the Gregorian calendar.If date is invalid, result is unspecified.
See an explanation of this algorithm.
- Parameters:
year- Year (e.g. 2020 means 2020 CE, 1 means 1 CE, 0 means 1 BCE because there is no 0 CE, -1 means 2 BCE, etc.)month- Month (between 1 and 12 inclusive, 1 meaning January)day- Day of month (between 1 and 31 inclusive)
-
unixTimestamp
public static long unixTimestamp(int year, int month, int day, int hour, int minute, int second) -
addMonths
public static long addMonths(long timestamp, int m) Adds a given number of months to a timestamp, represented as the number of milliseconds since the epoch. -
addMonths
public static int addMonths(int date, int m) Adds a given number of months to a date, represented as the number of days since the epoch. -
subtractMonths
public static int subtractMonths(int date0, int date1) Finds the number of months between two dates, each represented as the number of days since the epoch. -
subtractMonths
public static int subtractMonths(long t0, long t1) -
floorDiv
public static long floorDiv(long x, long y) Divide, rounding towards negative infinity. -
floorMod
public static long floorMod(long x, long y) Modulo, always returning a non-negative result. -
calendar
Creates an instance ofCalendarin the root locale and UTC time zone. -
isOffsetDateTime
Returns whether a value is anOffsetDateTime. -
offsetDateTimeValue
Returns the value of aOffsetDateTimeas a string.
-
UTC_ZONE