Search in sources :

Example 1 with LongTimeWithTimeZone

use of io.trino.spi.type.LongTimeWithTimeZone in project trino by trinodb.

the class TimestampWithTimeZoneToTimeWithTimeZoneCast method longToLong.

@LiteralParameters({ "sourcePrecision", "targetPrecision" })
@SqlType("time(targetPrecision) with time zone")
public static LongTimeWithTimeZone longToLong(@LiteralParameter("targetPrecision") long targetPrecision, @SqlType("timestamp(sourcePrecision) with time zone") LongTimestampWithTimeZone timestamp) {
    // source precision is > 3
    // target precision is > 9
    TimeZoneKey zoneKey = getTimeZoneKey(timestamp.getTimeZoneKey());
    long epochMillis = getChronology(zoneKey).getZone().convertUTCToLocal(timestamp.getEpochMillis());
    // combine epochMillis with picosOfMilli from the timestamp. We compute modulo 24 to avoid overflow when rescaling epocMilli to picoseconds
    long picos = rescale(floorMod(epochMillis, MILLISECONDS_PER_DAY), 3, 12) + timestamp.getPicosOfMilli();
    picos = round(picos, (int) (12 - targetPrecision));
    return new LongTimeWithTimeZone(floorMod(picos, PICOSECONDS_PER_DAY), DateTimes.getOffsetMinutes(Instant.ofEpochMilli(epochMillis), zoneKey));
}
Also used : LongTimeWithTimeZone(io.trino.spi.type.LongTimeWithTimeZone) TimeZoneKey.getTimeZoneKey(io.trino.spi.type.TimeZoneKey.getTimeZoneKey) TimeZoneKey(io.trino.spi.type.TimeZoneKey) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 2 with LongTimeWithTimeZone

use of io.trino.spi.type.LongTimeWithTimeZone in project trino by trinodb.

the class DateTimes method parseLongTimeWithTimeZone.

public static LongTimeWithTimeZone parseLongTimeWithTimeZone(String value) {
    Matcher matcher = TIME_PATTERN.matcher(value);
    if (!matcher.matches() || matcher.group("offsetHour") == null || matcher.group("offsetMinute") == null) {
        throw new IllegalArgumentException("Invalid time with time zone: " + value);
    }
    int hour = Integer.parseInt(matcher.group("hour"));
    int minute = Integer.parseInt(matcher.group("minute"));
    int second = matcher.group("second") == null ? 0 : Integer.parseInt(matcher.group("second"));
    int offsetSign = matcher.group("sign").equals("+") ? 1 : -1;
    int offsetHour = Integer.parseInt((matcher.group("offsetHour")));
    int offsetMinute = Integer.parseInt((matcher.group("offsetMinute")));
    if (hour > 23 || minute > 59 || second > 59 || !isValidOffset(offsetHour, offsetMinute)) {
        throw new IllegalArgumentException("Invalid time with time zone: " + value);
    }
    int precision = 0;
    String fraction = matcher.group("fraction");
    long fractionValue = 0;
    if (fraction != null) {
        precision = fraction.length();
        fractionValue = Long.parseLong(fraction);
    }
    long picos = (((hour * 60) + minute) * 60 + second) * PICOSECONDS_PER_SECOND + rescale(fractionValue, precision, 12);
    return new LongTimeWithTimeZone(picos, calculateOffsetMinutes(offsetSign, offsetHour, offsetMinute));
}
Also used : LongTimeWithTimeZone(io.trino.spi.type.LongTimeWithTimeZone) Matcher(java.util.regex.Matcher)

Example 3 with LongTimeWithTimeZone

use of io.trino.spi.type.LongTimeWithTimeZone in project trino by trinodb.

the class AtTimeZone method atTimeZone.

@LiteralParameters({ "x", "p" })
@SqlType("time(p) with time zone")
public static LongTimeWithTimeZone atTimeZone(ConnectorSession session, @SqlType("time(p) with time zone") LongTimeWithTimeZone time, @SqlType("varchar(x)") Slice zoneId) {
    TimeZoneKey zoneKey;
    try {
        zoneKey = getTimeZoneKey(zoneId.toStringUtf8());
    } catch (TimeZoneNotSupportedException e) {
        throw new TrinoException(INVALID_FUNCTION_ARGUMENT, format("'%s' is not a valid time zone", zoneId));
    }
    int offsetMinutes = getOffsetMinutes(session.getStart(), zoneKey);
    long picos = time.getPicoseconds() - (time.getOffsetMinutes() - offsetMinutes) * PICOSECONDS_PER_MINUTE;
    return new LongTimeWithTimeZone(floorMod(picos, PICOSECONDS_PER_DAY), offsetMinutes);
}
Also used : LongTimeWithTimeZone(io.trino.spi.type.LongTimeWithTimeZone) TrinoException(io.trino.spi.TrinoException) TimeZoneNotSupportedException(io.trino.spi.type.TimeZoneNotSupportedException) TimeZoneKey.getTimeZoneKey(io.trino.spi.type.TimeZoneKey.getTimeZoneKey) TimeZoneKey(io.trino.spi.type.TimeZoneKey) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 4 with LongTimeWithTimeZone

use of io.trino.spi.type.LongTimeWithTimeZone in project trino by trinodb.

the class CurrentTime method longTime.

@LiteralParameters("p")
@SqlType("time(p) with time zone")
public static LongTimeWithTimeZone longTime(@LiteralParameter("p") long precision, ConnectorSession session, // need a dummy value since the type inferencer can't bind type arguments exclusively from return type
@SqlNullable @SqlType("time(p) with time zone") LongTimeWithTimeZone dummy) {
    Instant start = session.getStart();
    ZoneId zoneId = session.getTimeZoneKey().getZoneId();
    long nanos = ZonedDateTime.ofInstant(start, zoneId).toLocalTime().toNanoOfDay();
    int offsetSeconds = zoneId.getRules().getOffset(start).getTotalSeconds();
    long picos = floorMod(round(nanos * PICOSECONDS_PER_NANOSECOND, (int) (12 - precision)), PICOSECONDS_PER_DAY);
    return new LongTimeWithTimeZone(picos, offsetSeconds / 60);
}
Also used : LongTimeWithTimeZone(io.trino.spi.type.LongTimeWithTimeZone) ZoneId(java.time.ZoneId) Instant(java.time.Instant) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 5 with LongTimeWithTimeZone

use of io.trino.spi.type.LongTimeWithTimeZone in project trino by trinodb.

the class TimestampWithTimeZoneToTimeWithTimeZoneCast method shortToLong.

@LiteralParameters({ "sourcePrecision", "targetPrecision" })
@SqlType("time(targetPrecision) with time zone")
public static LongTimeWithTimeZone shortToLong(@SqlType("timestamp(sourcePrecision) with time zone") long timestamp) {
    // source precision is <= 3
    // target precision is > 9
    TimeZoneKey zoneKey = unpackZoneKey(timestamp);
    long epochMillis = getChronology(zoneKey).getZone().convertUTCToLocal(unpackMillisUtc(timestamp));
    long millis = floorMod(epochMillis, MILLISECONDS_PER_DAY);
    return new LongTimeWithTimeZone(millis * PICOSECONDS_PER_MILLISECOND, DateTimes.getOffsetMinutes(Instant.ofEpochMilli(epochMillis), zoneKey));
}
Also used : LongTimeWithTimeZone(io.trino.spi.type.LongTimeWithTimeZone) TimeZoneKey.getTimeZoneKey(io.trino.spi.type.TimeZoneKey.getTimeZoneKey) TimeZoneKey(io.trino.spi.type.TimeZoneKey) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Aggregations

LongTimeWithTimeZone (io.trino.spi.type.LongTimeWithTimeZone)6 LiteralParameters (io.trino.spi.function.LiteralParameters)5 SqlType (io.trino.spi.function.SqlType)5 TimeZoneKey (io.trino.spi.type.TimeZoneKey)3 TimeZoneKey.getTimeZoneKey (io.trino.spi.type.TimeZoneKey.getTimeZoneKey)3 TrinoException (io.trino.spi.TrinoException)2 Matcher (java.util.regex.Matcher)2 TimeZoneNotSupportedException (io.trino.spi.type.TimeZoneNotSupportedException)1 Instant (java.time.Instant)1 ZoneId (java.time.ZoneId)1