use of io.trino.spi.type.TimeZoneKey in project trino by trinodb.
the class WithTimeZone method shortPrecision.
@LiteralParameters({ "x", "p" })
@SqlType("timestamp(p) with time zone")
public static long shortPrecision(@LiteralParameter("p") long precision, @SqlType("timestamp(p)") long timestamp, @SqlType("varchar(x)") Slice zoneId) {
verify(precision <= 3, "Expected precision <= 3");
TimeZoneKey toTimeZoneKey;
try {
toTimeZoneKey = getTimeZoneKey(zoneId.toStringUtf8());
} catch (TimeZoneNotSupportedException e) {
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, format("'%s' is not a valid time zone", zoneId.toStringUtf8()));
}
DateTimeZone toDateTimeZone = getDateTimeZone(toTimeZoneKey);
return packDateTimeWithZone(UTC.getMillisKeepLocal(toDateTimeZone, scaleEpochMicrosToMillis(timestamp)), toTimeZoneKey);
}
use of io.trino.spi.type.TimeZoneKey in project trino by trinodb.
the class TimestampWithTimeZoneToDateCast method cast.
@LiteralParameters("p")
@SqlType(StandardTypes.DATE)
public static long cast(@SqlType("timestamp(p) with time zone") long timestamp) {
long epochMillis = unpackMillisUtc(timestamp);
TimeZoneKey zoneKey = unpackZoneKey(timestamp);
return toDate(epochMillis, zoneKey);
}
use of io.trino.spi.type.TimeZoneKey in project trino by trinodb.
the class TimestampWithTimeZoneToTimeWithTimeZoneCast method longToShort.
@LiteralParameters({ "sourcePrecision", "targetPrecision" })
@SqlType("time(targetPrecision) with time zone")
public static long longToShort(@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));
picos = floorMod(picos, PICOSECONDS_PER_DAY);
long nanos = rescale(picos, 12, 9);
return packTimeWithTimeZone(nanos, DateTimes.getOffsetMinutes(Instant.ofEpochMilli(epochMillis), zoneKey));
}
use of io.trino.spi.type.TimeZoneKey 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.TimeZoneKey in project trino by trinodb.
the class TimestampWithTimeZoneToTimeWithTimeZoneCast method shortToShort.
@LiteralParameters({ "sourcePrecision", "targetPrecision" })
@SqlType("time(targetPrecision) with time zone")
public static long shortToShort(@LiteralParameter("targetPrecision") long targetPrecision, @SqlType("timestamp(sourcePrecision) with time zone") long packedTimestamp) {
// source precision is <= 3
// target precision is <= 9
TimeZoneKey zoneKey = unpackZoneKey(packedTimestamp);
long epochMillis = getChronology(zoneKey).getZone().convertUTCToLocal(unpackMillisUtc(packedTimestamp));
if (targetPrecision <= 3) {
epochMillis = round(epochMillis, (int) (3 - targetPrecision));
}
long nanos = rescale(floorMod(epochMillis, MILLISECONDS_PER_DAY), 3, 9);
return packTimeWithTimeZone(nanos, DateTimes.getOffsetMinutes(Instant.ofEpochMilli(epochMillis), zoneKey));
}
Aggregations