public static SqlTimestamp sqlTimestampOf(long millis, ConnectorSession session) { if (session.isLegacyTimestamp()) { return new SqlTimestamp(millis, session.getTimeZoneKey()); } else { return new SqlTimestamp(millis); } }
@ScalarOperator(CAST) @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) public static long castToTimestampWithTimeZone(ConnectorSession session, @SqlType(StandardTypes.DATE) long value) { long utcMillis = TimeUnit.DAYS.toMillis(value); // date is encoded as milliseconds at midnight in UTC // convert to midnight in the session timezone ISOChronology chronology = getChronology(session.getTimeZoneKey()); long millis = utcMillis - chronology.getZone().getOffset(utcMillis); return packDateTimeWithZone(millis, session.getTimeZoneKey()); }
private static long parseMillis(ConnectorSession session, Slice dateTime, Slice formatString) { TimeZoneKey timeZoneKey = UTC_KEY; if (session.isLegacyTimestamp()) { timeZoneKey = session.getTimeZoneKey(); } return parseMillis(timeZoneKey, session.getLocale(), dateTime, formatString); }
@Description("current time zone") @ScalarFunction("current_timezone") @SqlType(StandardTypes.VARCHAR) public static Slice currentTimeZone(ConnectorSession session) { return utf8Slice(session.getTimeZoneKey().getId()); }
@Description("current timestamp with time zone") @ScalarFunction(value = "current_timestamp", alias = "now") @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) public static long currentTimestamp(ConnectorSession session) { return packDateTimeWithZone(session.getStartTime(), session.getTimeZoneKey()); }
@Override protected Long visitTimeLiteral(TimeLiteral node, ConnectorSession session) { if (session.isLegacyTimestamp()) { return parseTimeLiteral(session.getTimeZoneKey(), node.getValue()); } else { return parseTimeLiteral(node.getValue()); } }
@ScalarOperator(ADD) @SqlType(StandardTypes.TIME) public static long timePlusIntervalDayToSecond(ConnectorSession session, @SqlType(StandardTypes.TIME) long left, @SqlType(StandardTypes.INTERVAL_DAY_TO_SECOND) long right) { return modulo24Hour(getChronology(session.getTimeZoneKey()), left + right); }
@ScalarOperator(ADD) @SqlType(StandardTypes.TIME) public static long intervalDayToSecondPlusTime(ConnectorSession session, @SqlType(StandardTypes.INTERVAL_DAY_TO_SECOND) long left, @SqlType(StandardTypes.TIME) long right) { return modulo24Hour(getChronology(session.getTimeZoneKey()), left + right); }
@ScalarOperator(ADD) @SqlType(StandardTypes.TIME) public static long timePlusIntervalDayToSecond(ConnectorSession session, @SqlType(StandardTypes.TIME) long left, @SqlType(StandardTypes.INTERVAL_DAY_TO_SECOND) long right) { return modulo24Hour(getChronology(session.getTimeZoneKey()), left + right); }
@Description("add the specified amount of time to the given time") @LiteralParameters("x") @ScalarFunction("date_add") @SqlType(StandardTypes.TIME) public static long addFieldValueTime(ConnectorSession session, @SqlType("varchar(x)") Slice unit, @SqlType(StandardTypes.BIGINT) long value, @SqlType(StandardTypes.TIME) long time) { if (session.isLegacyTimestamp()) { ISOChronology chronology = getChronology(session.getTimeZoneKey()); return modulo24Hour(chronology, getTimeField(chronology, unit).add(time, toIntExact(value))); } return modulo24Hour(getTimeField(UTC_CHRONOLOGY, unit).add(time, toIntExact(value))); }
@Description("minute of the hour of the given time") @ScalarFunction("minute") @SqlType(StandardTypes.BIGINT) public static long minuteFromTime(ConnectorSession session, @SqlType(StandardTypes.TIME) long time) { if (session.isLegacyTimestamp()) { return getChronology(session.getTimeZoneKey()).minuteOfHour().get(time); } else { return MINUTE_OF_HOUR.get(time); } }
@Description("month of the year of the given timestamp") @ScalarFunction("month") @SqlType(StandardTypes.BIGINT) public static long monthFromTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp) { if (session.isLegacyTimestamp()) { return getChronology(session.getTimeZoneKey()).monthOfYear().get(timestamp); } else { return MONTH_OF_YEAR.get(timestamp); } }
@Description("year of the given timestamp") @ScalarFunction("year") @SqlType(StandardTypes.BIGINT) public static long yearFromTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp) { if (session.isLegacyTimestamp()) { return getChronology(session.getTimeZoneKey()).year().get(timestamp); } else { return YEAR.get(timestamp); } }
@Description("current timestamp without time zone") @ScalarFunction("localtimestamp") @SqlType(StandardTypes.TIMESTAMP) public static long localTimestamp(ConnectorSession session) { if (session.isLegacyTimestamp()) { return session.getStartTime(); } ISOChronology localChronology = getChronology(session.getTimeZoneKey()); return localChronology.getZone().convertUTCToLocal(session.getStartTime()); }
@Description("minute of the hour of the given time") @ScalarFunction("minute") @SqlType(StandardTypes.BIGINT) public static long minuteFromTime(ConnectorSession session, @SqlType(StandardTypes.TIME) long time) { if (session.isLegacyTimestamp()) { return getChronology(session.getTimeZoneKey()).minuteOfHour().get(time); } else { return MINUTE_OF_HOUR.get(time); } }
@Description("hour of the day of the given timestamp") @ScalarFunction("hour") @SqlType(StandardTypes.BIGINT) public static long hourFromTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp) { if (session.isLegacyTimestamp()) { return getChronology(session.getTimeZoneKey()).hourOfDay().get(timestamp); } else { return HOUR_OF_DAY.get(timestamp); } }
@Description("day of the month of the given timestamp") @ScalarFunction(value = "day", alias = "day_of_month") @SqlType(StandardTypes.BIGINT) public static long dayFromTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp) { if (session.isLegacyTimestamp()) { return getChronology(session.getTimeZoneKey()).dayOfMonth().get(timestamp); } else { return DAY_OF_MONTH.get(timestamp); } }
@Description("week of the year of the given timestamp") @ScalarFunction(value = "week", alias = "week_of_year") @SqlType(StandardTypes.BIGINT) public static long weekFromTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp) { if (session.isLegacyTimestamp()) { return getChronology(session.getTimeZoneKey()).weekOfWeekyear().get(timestamp); } else { return WEEK_OF_YEAR.get(timestamp); } }
@Description("current time without time zone") @ScalarFunction("localtime") @SqlType(StandardTypes.TIME) public static long localTime(ConnectorSession session) { if (session.isLegacyTimestamp()) { return UTC_CHRONOLOGY.millisOfDay().get(session.getStartTime()); } ISOChronology localChronology = getChronology(session.getTimeZoneKey()); return localChronology.millisOfDay().get(session.getStartTime()); }
@ScalarFunction("from_iso8601_timestamp") @LiteralParameters("x") @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) public static long fromISO8601Timestamp(ConnectorSession session, @SqlType("varchar(x)") Slice iso8601DateTime) { DateTimeFormatter formatter = ISODateTimeFormat.dateTimeParser() .withChronology(getChronology(session.getTimeZoneKey())) .withOffsetParsed(); return packDateTimeWithZone(parseDateTimeHelper(formatter, iso8601DateTime.toStringUtf8())); }