Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.facebook.presto.operator.scalar;
 
 
 
 import static com.facebook.presto.operator.scalar.QuarterOfYearDateTimeField.QUARTER_OF_YEAR;
 import static com.facebook.presto.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT;
 import static com.facebook.presto.spi.type.DateTimeEncoding.packDateTimeWithZone;
 import static com.facebook.presto.spi.type.DateTimeEncoding.unpackMillisUtc;
 import static com.facebook.presto.spi.type.DateTimeEncoding.updateMillisUtc;
 import static com.facebook.presto.spi.type.TimeZoneKey.getTimeZoneKeyForOffset;
 import static com.facebook.presto.type.DateTimeOperators.modulo24Hour;
 import static com.facebook.presto.util.DateTimeZoneIndex.extractZoneOffsetMinutes;
 import static com.facebook.presto.util.DateTimeZoneIndex.getChronology;
 import static com.facebook.presto.util.DateTimeZoneIndex.unpackChronology;
 import static com.facebook.presto.util.Failures.checkCondition;
 import static java.nio.charset.StandardCharsets.UTF_8;
 import static java.util.Locale.ENGLISH;
 import static java.util.concurrent.TimeUnit.DAYS;
 import static java.util.concurrent.TimeUnit.MILLISECONDS;
 import static org.joda.time.DateTimeZone.UTC;
 
 public final class DateTimeFunctions
 {
     {
         @Override
         protected DateTimeFormatter load(Slice format)
         {
             return createDateTimeFormatter(format);
         }
     };
 
     private static final ISOChronology UTC_CHRONOLOGY = ISOChronology.getInstance();
     private static final DateTimeField SECOND_OF_MINUTE = .secondOfMinute();
     private static final DateTimeField DAY_OF_WEEK = .dayOfWeek();
     private static final DateTimeField DAY_OF_MONTH = .dayOfMonth();
     private static final DateTimeField DAY_OF_YEAR = .dayOfYear();
     private static final DateTimeField WEEK_OF_YEAR = .weekOfWeekyear();
     private static final DateTimeField YEAR_OF_WEEK = .weekyear();
     private static final DateTimeField MONTH_OF_YEAR = .monthOfYear();
     private static final DateTimeField QUARTER = .getField();
     private static final DateTimeField YEAR = .year();
     private static final int MILLISECONDS_IN_SECOND = 1000;
     private static final int MILLISECONDS_IN_MINUTE = 60 * ;
     private static final int MILLISECONDS_IN_HOUR = 60 * ;
     private static final int MILLISECONDS_IN_DAY = 24 * ;
 
     private DateTimeFunctions() {}
 
     @Description("current date")
     @ScalarFunction
     public static long currentDate(ConnectorSession session)
     {
         long millis = getChronology(session.getTimeZoneKey()).dayOfMonth().roundFloor(session.getStartTime());
         return .toDays(millis);
     }
 
     @Description("current time with time zone")
     @ScalarFunction
     public static long currentTime(ConnectorSession session)
     {
         // Stack value is number of milliseconds from start of the current day,
         // but the start of the day is relative to the current time zone.
         long millis = getChronology(session.getTimeZoneKey()).millisOfDay().get(session.getStartTime());
         return packDateTimeWithZone(millissession.getTimeZoneKey());
     }
 
    @Description("current time without time zone")
    @ScalarFunction("localtime")
    public static long localTime(ConnectorSession session)
    {
        // Stack value is number of milliseconds from start of the current day,
        // but the start of the day is relative to the current time zone.
        return getChronology(session.getTimeZoneKey()).millisOfDay().get(session.getStartTime());
    }
    @Description("current time zone")
    @ScalarFunction("current_timezone")
    public static Slice currentTimeZone(ConnectorSession session)
    {
        return Slices.copiedBuffer(session.getTimeZoneKey().getId(), );
    }
    @Description("current timestamp with time zone")
    @ScalarFunction(value = "current_timestamp", alias = "now")
    public static long currentTimestamp(ConnectorSession session)
    {
        return packDateTimeWithZone(session.getStartTime(), session.getTimeZoneKey());
    }
    @Description("current timestamp without time zone")
    @ScalarFunction("localtimestamp")
    public static long localTimestamp(ConnectorSession session)
    {
        return session.getStartTime();
    }
    @ScalarFunction("from_unixtime")
    public static long fromUnixTime(@SqlType(.double unixTime)
    {
        return Math.round(unixTime * 1000);
    }
    @ScalarFunction("from_unixtime")
    public static long fromUnixTime(@SqlType(.double unixTime, @SqlType(.long hoursOffset, @SqlType(.long minutesOffset)
    {
        return packDateTimeWithZone(Math.round(unixTime * 1000), (int) (hoursOffset * 60 + minutesOffset));
    }
    @ScalarFunction("to_unixtime")
    public static double toUnixTime(@SqlType(.long timestamp)
    {
        return timestamp / 1000.0;
    }
    @ScalarFunction("to_unixtime")
    public static double toUnixTimeFromTimestampWithTimeZone(@SqlType(.long timestampWithTimeZone)
    {
        return unpackMillisUtc(timestampWithTimeZone) / 1000.0;
    }
    @ScalarFunction("at_timezone")
    public static long timeAtTimeZone(@SqlType(.long timeWithTimeZone, @SqlType(.Slice zoneId)
    {
        return packDateTimeWithZone(unpackMillisUtc(timeWithTimeZone), zoneId.toStringUtf8());
    }
    @ScalarFunction("at_timezone")
    public static long timeAtTimeZone(@SqlType(.long timeWithTimeZone, @SqlType(.long zoneOffset)
    {
        checkCondition((zoneOffset % 60_000) == 0, "Invalid time zone offset interval: interval contains seconds");
        int zoneOffsetMinutes = (int) (zoneOffset / 60_000);
        return packDateTimeWithZone(unpackMillisUtc(timeWithTimeZone), getTimeZoneKeyForOffset(zoneOffsetMinutes));
    }
    @ScalarFunction("at_timezone")
    public static long timestampAtTimeZone(@SqlType(.long timestampWithTimeZone, @SqlType(.Slice zoneId)
    {
        return packDateTimeWithZone(unpackMillisUtc(timestampWithTimeZone), zoneId.toStringUtf8());
    }
    @ScalarFunction("at_timezone")
    public static long timestampAtTimeZone(@SqlType(.long timestampWithTimeZone, @SqlType(.long zoneOffset)
    {
        checkCondition((zoneOffset % 60_000) == 0, "Invalid time zone offset interval: interval contains seconds");
        int zoneOffsetMinutes = (int) (zoneOffset / 60_000);
        return packDateTimeWithZone(unpackMillisUtc(timestampWithTimeZone), getTimeZoneKeyForOffset(zoneOffsetMinutes));
    }
    @Description("truncate to the specified precision in the session timezone")
    @ScalarFunction("date_trunc")
    public static long truncateDate(ConnectorSession session, @SqlType(.Slice unit, @SqlType(.long date)
    {
        long millis = getDateField(unit).roundFloor(.toMillis(date));
        return .toDays(millis);
    }
    @Description("truncate to the specified precision in the session timezone")
    @ScalarFunction("date_trunc")
    public static long truncateTime(ConnectorSession session, @SqlType(.Slice unit, @SqlType(.long time)
    {
        return getTimeField(getChronology(session.getTimeZoneKey()), unit).roundFloor(time);
    }
    @Description("truncate to the specified precision")
    @ScalarFunction("date_trunc")
    public static long truncateTimeWithTimeZone(@SqlType(.Slice unit, @SqlType(.long timeWithTimeZone)
    {
        long millis = getTimeField(unpackChronology(timeWithTimeZone), unit).roundFloor(unpackMillisUtc(timeWithTimeZone));
        return updateMillisUtc(millistimeWithTimeZone);
    }
    @Description("truncate to the specified precision in the session timezone")
    @ScalarFunction("date_trunc")
    public static long truncateTimestamp(ConnectorSession session, @SqlType(.Slice unit, @SqlType(.long timestamp)
    {
        return getTimestampField(getChronology(session.getTimeZoneKey()), unit).roundFloor(timestamp);
    }
    @Description("truncate to the specified precision")
    @ScalarFunction("date_trunc")
    public static long truncateTimestampWithTimezone(@SqlType(.Slice unit, @SqlType(.long timestampWithTimeZone)
    {
        long millis = getTimestampField(unpackChronology(timestampWithTimeZone), unit).roundFloor(unpackMillisUtc(timestampWithTimeZone));
        return updateMillisUtc(millistimestampWithTimeZone);
    }
    @Description("add the specified amount of date to the given date")
    @ScalarFunction("date_add")
    public static long addFieldValueDate(ConnectorSession session, @SqlType(.Slice unit, @SqlType(.long value, @SqlType(.long date)
    {
        long millis = getDateField(unit).add(.toMillis(date), Ints.checkedCast(value));
        return .toDays(millis);
    }
    @Description("add the specified amount of time to the given time")
    @ScalarFunction("date_add")
    public static long addFieldValueTime(ConnectorSession session, @SqlType(.Slice unit, @SqlType(.long value, @SqlType(.long time)
    {
        ISOChronology chronology = getChronology(session.getTimeZoneKey());
        return modulo24Hour(chronologygetTimeField(chronologyunit).add(time, Ints.checkedCast(value)));
    }
    @Description("add the specified amount of time to the given time")
    @ScalarFunction("date_add")
    public static long addFieldValueTimeWithTimeZone(
            @SqlType(.Slice unit,
            @SqlType(.long value,
            @SqlType(.long timeWithTimeZone)
    {
        ISOChronology chronology = unpackChronology(timeWithTimeZone);
        long millis = modulo24Hour(chronologygetTimeField(chronologyunit).add(unpackMillisUtc(timeWithTimeZone), Ints.checkedCast(value)));
        return updateMillisUtc(millistimeWithTimeZone);
    }
    @Description("add the specified amount of time to the given timestamp")
    @ScalarFunction("date_add")
    public static long addFieldValueTimestamp(
            ConnectorSession session,
            @SqlType(.Slice unit,
            @SqlType(.long value,
            @SqlType(.long timestamp)
    {
        return getTimestampField(getChronology(session.getTimeZoneKey()), unit).add(timestamp, Ints.checkedCast(value));
    }
    @Description("add the specified amount of time to the given timestamp")
    @ScalarFunction("date_add")
    public static long addFieldValueTimestampWithTimeZone(
            @SqlType(.Slice unit,
            @SqlType(.long value,
            @SqlType(.long timestampWithTimeZone)
    {
        long millis = getTimestampField(unpackChronology(timestampWithTimeZone), unit).add(unpackMillisUtc(timestampWithTimeZone), Ints.checkedCast(value));
        return updateMillisUtc(millistimestampWithTimeZone);
    }
    @Description("difference of the given dates in the given unit")
    @ScalarFunction("date_diff")
    public static long diffDate(ConnectorSession session, @SqlType(.Slice unit, @SqlType(.long date1, @SqlType(.long date2)
    {
        return getDateField(unit).getDifferenceAsLong(.toMillis(date2), .toMillis(date1));
    }
    @Description("difference of the given times in the given unit")
    @ScalarFunction("date_diff")
    public static long diffTime(ConnectorSession session, @SqlType(.Slice unit, @SqlType(.long time1, @SqlType(.long time2)
    {
        ISOChronology chronology = getChronology(session.getTimeZoneKey());
        return getTimeField(chronologyunit).getDifferenceAsLong(time2time1);
    }
    @Description("difference of the given times in the given unit")
    @ScalarFunction("date_diff")
    public static long diffTimeWithTimeZone(
            @SqlType(.Slice unit,
            @SqlType(.long timeWithTimeZone1,
            @SqlType(.long timeWithTimeZone2)
    {
        return getTimeField(unpackChronology(timeWithTimeZone1), unit).getDifferenceAsLong(unpackMillisUtc(timeWithTimeZone2), unpackMillisUtc(timeWithTimeZone1));
    }
    @Description("difference of the given times in the given unit")
    @ScalarFunction("date_diff")
    public static long diffTimestamp(
            ConnectorSession session,
            @SqlType(.Slice unit,
            @SqlType(.long timestamp1,
            @SqlType(.long timestamp2)
    {
        return getTimestampField(getChronology(session.getTimeZoneKey()), unit).getDifferenceAsLong(timestamp2timestamp1);
    }
    @Description("difference of the given times in the given unit")
    @ScalarFunction("date_diff")
    public static long diffTimestampWithTimeZone(
            @SqlType(.Slice unit,
            @SqlType(.long timestampWithTimeZone1,
            @SqlType(.long timestampWithTimeZone2)
    {
        return getTimestampField(unpackChronology(timestampWithTimeZone1), unit).getDifferenceAsLong(unpackMillisUtc(timestampWithTimeZone2), unpackMillisUtc(timestampWithTimeZone1));
    }
    private static DateTimeField getDateField(ISOChronology chronologySlice unit)
    {
        String unitString = unit.toString().toLowerCase();
        switch (unitString) {
            case "day":
                return chronology.dayOfMonth();
            case "week":
                return chronology.weekOfWeekyear();
            case "month":
                return chronology.monthOfYear();
            case "quarter":
                return .getField(chronology);
            case "year":
                return chronology.year();
        }
        throw new PrestoException("'" + unitString + "' is not a valid DATE field");
    }
    private static DateTimeField getTimeField(ISOChronology chronologySlice unit)
    {
        String unitString = unit.toString().toLowerCase();
        switch (unitString) {
            case "millisecond":
                return chronology.millisOfSecond();
            case "second":
                return chronology.secondOfMinute();
            case "minute":
                return chronology.minuteOfHour();
            case "hour":
                return chronology.hourOfDay();
        }
        throw new PrestoException("'" + unitString + "' is not a valid Time field");
    }
    private static DateTimeField getTimestampField(ISOChronology chronologySlice unit)
    {
        String unitString = unit.toString().toLowerCase();
        switch (unitString) {
            case "millisecond":
                return chronology.millisOfSecond();
            case "second":
                return chronology.secondOfMinute();
            case "minute":
                return chronology.minuteOfHour();
            case "hour":
                return chronology.hourOfDay();
            case "day":
                return chronology.dayOfMonth();
            case "week":
                return chronology.weekOfWeekyear();
            case "month":
                return chronology.monthOfYear();
            case "quarter":
                return .getField(chronology);
            case "year":
                return chronology.year();
        }
        throw new PrestoException("'" + unitString + "' is not a valid Timestamp field");
    }
    @Description("parses the specified date/time by the given format")
    public static long parseDatetime(ConnectorSession session, @SqlType(.Slice datetime, @SqlType(.Slice formatString)
    {
        String pattern = formatString.toString();
        DateTimeFormatter formatter = DateTimeFormat.forPattern(pattern)
                .withChronology(getChronology(session.getTimeZoneKey()))
                .withOffsetParsed()
                .withLocale(session.getLocale());
        String datetimeString = datetime.toString();
        return DateTimeZoneIndex.packDateTimeWithZone(parseDateTimeHelper(formatterdatetimeString));
    }
    private static DateTime parseDateTimeHelper(DateTimeFormatter formatterString datetimeString)
    {
        try {
            return formatter.parseDateTime(datetimeString);
        }
        catch (IllegalArgumentException e) {
            throw new PrestoException(e);
        }
    }
    @Description("formats the given time by the given format")
    public static Slice formatDatetime(ConnectorSession session, @SqlType(.long timestamp, @SqlType(.Slice formatString)
    {
        return formatDatetime(getChronology(session.getTimeZoneKey()), session.getLocale(), timestampformatString);
    }
    @Description("formats the given time by the given format")
    @ScalarFunction("format_datetime")
    public static Slice formatDatetimeWithTimeZone(
            ConnectorSession session,
            @SqlType(.long timestampWithTimeZone,
            @SqlType(.Slice formatString)
    {
        return formatDatetime(unpackChronology(timestampWithTimeZone), session.getLocale(), unpackMillisUtc(timestampWithTimeZone), formatString);
    }
    private static Slice formatDatetime(ISOChronology chronologyLocale localelong timestampSlice formatString)
    {
        String pattern = formatString.toString();
        DateTimeFormatter formatter = DateTimeFormat.forPattern(pattern)
                .withChronology(chronology)
                .withLocale(locale);
        String datetimeString = formatter.print(timestamp);
        return Slices.wrappedBuffer(datetimeString.getBytes());
    }
    public static Slice dateFormat(ConnectorSession session, @SqlType(.long timestamp, @SqlType(.Slice formatString)
    {
        return dateFormat(getChronology(session.getTimeZoneKey()), session.getLocale(), timestampformatString);
    }
    @ScalarFunction("date_format")
    public static Slice dateFormatWithTimeZone(
            ConnectorSession session,
            @SqlType(.long timestampWithTimeZone,
            @SqlType(.Slice formatString)
    {
        return dateFormat(unpackChronology(timestampWithTimeZone), session.getLocale(), unpackMillisUtc(timestampWithTimeZone), formatString);
    }
    private static Slice dateFormat(ISOChronology chronologyLocale localelong timestampSlice formatString)
    {
        DateTimeFormatter formatter = .get(formatString)
                .withChronology(chronology)
                .withLocale(locale);
        return Slices.copiedBuffer(formatter.print(timestamp), );
    }
    public static long dateParse(ConnectorSession session, @SqlType(.Slice dateTime, @SqlType(.Slice formatString)
    {
        DateTimeFormatter formatter = .get(formatString)
                .withChronology(getChronology(session.getTimeZoneKey()))
                .withLocale(session.getLocale());
        try {
            return formatter.parseMillis(dateTime.toString());
        }
        catch (IllegalArgumentException e) {
            throw new PrestoException(e);
        }
    }
    @Description("second of the minute of the given timestamp")
    @ScalarFunction("second")
    public static long secondFromTimestamp(@SqlType(.long timestamp)
    {
        // Time is effectively UTC so no need for a custom chronology
        return .get(timestamp);
    }
    @Description("second of the minute of the given timestamp")
    @ScalarFunction("second")
    public static long secondFromTimestampWithTimeZone(@SqlType(.long timestampWithTimeZone)
    {
        // Time is effectively UTC so no need for a custom chronology
        return .get(unpackMillisUtc(timestampWithTimeZone));
    }
    @Description("second of the minute of the given time")
    @ScalarFunction("second")
    public static long secondFromTime(@SqlType(.long time)
    {
        // Time is effectively UTC so no need for a custom chronology
        return .get(time);
    }
    @Description("second of the minute of the given time")
    @ScalarFunction("second")
    public static long secondFromTimeWithTimeZone(@SqlType(.long time)
    {
        // Time is effectively UTC so no need for a custom chronology
        return .get(unpackMillisUtc(time));
    }
    @Description("second of the minute of the given interval")
    @ScalarFunction("second")
    public static long secondFromInterval(@SqlType(.long milliseconds)
    {
        return (milliseconds % ) / ;
    }
    @Description("minute of the hour of the given timestamp")
    @ScalarFunction("minute")
    public static long minuteFromTimestamp(ConnectorSession session, @SqlType(.long timestamp)
    {
        return getChronology(session.getTimeZoneKey()).minuteOfHour().get(timestamp);
    }
    @Description("minute of the hour of the given timestamp")
    @ScalarFunction("minute")
    public static long minuteFromTimestampWithTimeZone(@SqlType(.long timestampWithTimeZone)
    {
        return unpackChronology(timestampWithTimeZone).minuteOfHour().get(unpackMillisUtc(timestampWithTimeZone));
    }
    @Description("minute of the hour of the given time")
    @ScalarFunction("minute")
    public static long minuteFromTime(ConnectorSession session, @SqlType(.long time)
    {
        return getChronology(session.getTimeZoneKey()).minuteOfHour().get(time);
    }
    @Description("minute of the hour of the given time")
    @ScalarFunction("minute")
    public static long minuteFromTimeWithTimeZone(@SqlType(.long timeWithTimeZone)
    {
        return unpackChronology(timeWithTimeZone).minuteOfHour().get(unpackMillisUtc(timeWithTimeZone));
    }
    @Description("minute of the hour of the given interval")
    @ScalarFunction("minute")
    public static long minuteFromInterval(@SqlType(.long milliseconds)
    {
        return (milliseconds % ) / ;
    }
    @Description("hour of the day of the given timestamp")
    @ScalarFunction("hour")
    public static long hourFromTimestamp(ConnectorSession session, @SqlType(.long timestamp)
    {
        return getChronology(session.getTimeZoneKey()).hourOfDay().get(timestamp);
    }
    @Description("hour of the day of the given timestamp")
    @ScalarFunction("hour")
    public static long hourFromTimestampWithTimeZone(@SqlType(.long timestampWithTimeZone)
    {
        return unpackChronology(timestampWithTimeZone).hourOfDay().get(unpackMillisUtc(timestampWithTimeZone));
    }
    @Description("hour of the day of the given time")
    @ScalarFunction("hour")
    public static long hourFromTime(ConnectorSession session, @SqlType(.long time)
    {
        return getChronology(session.getTimeZoneKey()).hourOfDay().get(time);
    }
    @Description("hour of the day of the given time")
    @ScalarFunction("hour")
    public static long hourFromTimeWithTimeZone(@SqlType(.long timeWithTimeZone)
    {
        return unpackChronology(timeWithTimeZone).hourOfDay().get(unpackMillisUtc(timeWithTimeZone));
    }
    @Description("hour of the day of the given interval")
    @ScalarFunction("hour")
    public static long hourFromInterval(@SqlType(.long milliseconds)
    {
        return (milliseconds % ) / ;
    }
    @Description("day of the week of the given timestamp")
    @ScalarFunction(value = "day_of_week", alias = "dow")
    public static long dayOfWeekFromTimestamp(ConnectorSession session, @SqlType(.long timestamp)
    {
        return getChronology(session.getTimeZoneKey()).dayOfWeek().get(timestamp);
    }
    @Description("day of the week of the given timestamp")
    @ScalarFunction(value = "day_of_week", alias = "dow")
    public static long dayOfWeekFromTimestampWithTimeZone(@SqlType(.long timestampWithTimeZone)
    {
        return unpackChronology(timestampWithTimeZone).dayOfWeek().get(unpackMillisUtc(timestampWithTimeZone));
    }
    @Description("day of the week of the given date")
    @ScalarFunction(value = "day_of_week", alias = "dow")
    public static long dayOfWeekFromDate(@SqlType(.long date)
    {
        return .get(.toMillis(date));
    }
    @Description("day of the month of the given timestamp")
    @ScalarFunction(value = "day", alias = "day_of_month")
    public static long dayFromTimestamp(ConnectorSession session, @SqlType(.long timestamp)
    {
        return getChronology(session.getTimeZoneKey()).dayOfMonth().get(timestamp);
    }
    @Description("day of the month of the given timestamp")
    @ScalarFunction(value = "day", alias = "day_of_month")
    public static long dayFromTimestampWithTimeZone(@SqlType(.long timestampWithTimeZone)
    {
        return unpackChronology(timestampWithTimeZone).dayOfMonth().get(unpackMillisUtc(timestampWithTimeZone));
    }
    @Description("day of the month of the given date")
    @ScalarFunction(value = "day", alias = "day_of_month")
    public static long dayFromDate(@SqlType(.long date)
    {
        return .get(.toMillis(date));
    }
    @Description("day of the month of the given interval")
    @ScalarFunction(value = "day", alias = "day_of_month")
    public static long dayFromInterval(@SqlType(.long milliseconds)
    {
        return milliseconds / ;
    }
    @Description("day of the year of the given timestamp")
    @ScalarFunction(value = "day_of_year", alias = "doy")
    public static long dayOfYearFromTimestamp(ConnectorSession session, @SqlType(.long timestamp)
    {
        return getChronology(session.getTimeZoneKey()).dayOfYear().get(timestamp);
    }
    @Description("day of the year of the given timestamp")
    @ScalarFunction(value = "day_of_year", alias = "doy")
    public static long dayOfYearFromTimestampWithTimeZone(@SqlType(.long timestampWithTimeZone)
    {
        return unpackChronology(timestampWithTimeZone).dayOfYear().get(unpackMillisUtc(timestampWithTimeZone));
    }
    @Description("day of the year of the given date")
    @ScalarFunction(value = "day_of_year", alias = "doy")
    public static long dayOfYearFromDate(@SqlType(.long date)
    {
        return .get(.toMillis(date));
    }
    @Description("week of the year of the given timestamp")
    @ScalarFunction(value = "week", alias = "week_of_year")
    public static long weekFromTimestamp(ConnectorSession session, @SqlType(.long timestamp)
    {
        return getChronology(session.getTimeZoneKey()).weekOfWeekyear().get(timestamp);
    }
    @Description("week of the year of the given timestamp")
    @ScalarFunction(value = "week", alias = "week_of_year")
    public static long weekFromTimestampWithTimeZone(@SqlType(.long timestampWithTimeZone)
    {
        return unpackChronology(timestampWithTimeZone).weekOfWeekyear().get(unpackMillisUtc(timestampWithTimeZone));
    }
    @Description("week of the year of the given date")
    @ScalarFunction(value = "week", alias = "week_of_year")
    public static long weekFromDate(@SqlType(.long date)
    {
        return .get(.toMillis(date));
    }
    @Description("year of the ISO week of the given timestamp")
    @ScalarFunction(value = "year_of_week", alias = "yow")
    public static long yearOfWeekFromTimestamp(ConnectorSession session, @SqlType(.long timestamp)
    {
        return getChronology(session.getTimeZoneKey()).weekyear().get(timestamp);
    }
    @Description("year of the ISO week of the given timestamp")
    @ScalarFunction(value = "year_of_week", alias = "yow")
    public static long yearOfWeekFromTimestampWithTimeZone(@SqlType(.long timestampWithTimeZone)
    {
        return unpackChronology(timestampWithTimeZone).weekyear().get(unpackMillisUtc(timestampWithTimeZone));
    }
    @Description("year of the ISO week of the given date")
    @ScalarFunction(value = "year_of_week", alias = "yow")
    public static long yearOfWeekFromDate(@SqlType(.long date)
    {
        return .get(.toMillis(date));
    }
    @Description("month of the year of the given timestamp")
    @ScalarFunction("month")
    public static long monthFromTimestamp(ConnectorSession session, @SqlType(.long timestamp)
    {
        return getChronology(session.getTimeZoneKey()).monthOfYear().get(timestamp);
    }
    @Description("month of the year of the given timestamp")
    @ScalarFunction("month")
    public static long monthFromTimestampWithTimeZone(@SqlType(.long timestampWithTimeZone)
    {
        return unpackChronology(timestampWithTimeZone).monthOfYear().get(unpackMillisUtc(timestampWithTimeZone));
    }
    @Description("month of the year of the given date")
    @ScalarFunction("month")
    public static long monthFromDate(@SqlType(.long date)
    {
        return .get(.toMillis(date));
    }
    @Description("month of the year of the given interval")
    @ScalarFunction("month")
    public static long monthFromInterval(@SqlType(.long months)
    {
        return months % 12;
    }
    @Description("quarter of the year of the given timestamp")
    @ScalarFunction("quarter")
    public static long quarterFromTimestamp(ConnectorSession session, @SqlType(.long timestamp)
    {
        return .getField(getChronology(session.getTimeZoneKey())).get(timestamp);
    }
    @Description("quarter of the year of the given timestamp")
    @ScalarFunction("quarter")
    public static long quarterFromTimestampWithTimeZone(@SqlType(.long timestampWithTimeZone)
    {
        return .getField(unpackChronology(timestampWithTimeZone)).get(unpackMillisUtc(timestampWithTimeZone));
    }
    @Description("quarter of the year of the given date")
    @ScalarFunction("quarter")
    public static long quarterFromDate(@SqlType(.long date)
    {
        return .get(.toMillis(date));
    }
    @Description("year of the given timestamp")
    @ScalarFunction("year")
    public static long yearFromTimestamp(ConnectorSession session, @SqlType(.long timestamp)
    {
        return getChronology(session.getTimeZoneKey()).year().get(timestamp);
    }
    @Description("year of the given timestamp")
    @ScalarFunction("year")
    public static long yearFromTimestampWithTimeZone(@SqlType(.long timestampWithTimeZone)
    {
        return unpackChronology(timestampWithTimeZone).year().get(unpackMillisUtc(timestampWithTimeZone));
    }
    @Description("year of the given date")
    @ScalarFunction("year")
    public static long yearFromDate(@SqlType(.long date)
    {
        return .get(.toMillis(date));
    }
    @Description("year of the given interval")
    @ScalarFunction("year")
    public static long yearFromInterval(@SqlType(.long months)
    {
        return months / 12;
    }
    @Description("time zone minute of the given timestamp")
    @ScalarFunction("timezone_minute")
    public static long timeZoneMinuteFromTimestampWithTimeZone(@SqlType(.long timestampWithTimeZone)
    {
        return extractZoneOffsetMinutes(timestampWithTimeZone) % 60;
    }
    @Description("time zone hour of the given timestamp")
    @ScalarFunction("timezone_hour")
    public static long timeZoneHourFromTimestampWithTimeZone(@SqlType(.long timestampWithTimeZone)
    {
        return extractZoneOffsetMinutes(timestampWithTimeZone) / 60;
    }
    @SuppressWarnings("fallthrough")
    public static DateTimeFormatter createDateTimeFormatter(Slice format)
    {
        DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
        String formatString = format.toString();
        boolean escaped = false;
        for (int i = 0; i < format.length(); i++) {
            char character = formatString.charAt(i);
            if (escaped) {
                switch (character) {
                    case 'a'// %a Abbreviated weekday name (Sun..Sat)
                        builder.appendDayOfWeekShortText();
                        break;
                    case 'b'// %b Abbreviated month name (Jan..Dec)
                        builder.appendMonthOfYearShortText();
                        break;
                    case 'c'// %c Month, numeric (0..12)
                        builder.appendMonthOfYear(1);
                        break;
                    case 'd'// %d Day of the month, numeric (00..31)
                        builder.appendDayOfMonth(2);
                        break;
                    case 'e'// %e Day of the month, numeric (0..31)
                        builder.appendDayOfMonth(1);
                        break;
                    case 'f'// %f Microseconds (000000..999999)
                        builder.appendFractionOfSecond(6, 6);
                        break;
                    case 'H'// %H Hour (00..23)
                        builder.appendHourOfDay(2);
                        break;
                    case 'h'// %h Hour (01..12)
                    case 'I'// %I Hour (01..12)
                        builder.appendClockhourOfHalfday(2);
                        break;
                    case 'i'// %i Minutes, numeric (00..59)
                        builder.appendMinuteOfHour(2);
                        break;
                    case 'j'// %j Day of year (001..366)
                        builder.appendDayOfYear(3);
                        break;
                    case 'k'// %k Hour (0..23)
                        builder.appendClockhourOfDay(1);
                        break;
                    case 'l'// %l Hour (1..12)
                        builder.appendClockhourOfHalfday(1);