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));
}
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));
}
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);
}
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);
}
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));
}
Aggregations