Skip to content

Commit

Permalink
Validate time with time zone well-formedness
Browse files Browse the repository at this point in the history
  • Loading branch information
findepi committed Sep 17, 2021
1 parent ede1187 commit 2ef9875
Showing 1 changed file with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@

import static io.trino.spi.type.TimeType.MAX_PRECISION;
import static io.trino.spi.type.Timestamps.MINUTES_PER_HOUR;
import static io.trino.spi.type.Timestamps.PICOSECONDS_PER_DAY;
import static io.trino.spi.type.Timestamps.PICOSECONDS_PER_HOUR;
import static io.trino.spi.type.Timestamps.PICOSECONDS_PER_MINUTE;
import static io.trino.spi.type.Timestamps.PICOSECONDS_PER_SECOND;
import static io.trino.spi.type.Timestamps.POWERS_OF_TEN;
import static io.trino.spi.type.Timestamps.SECONDS_PER_MINUTE;
import static io.trino.spi.type.Timestamps.rescale;
import static java.lang.Math.abs;
import static java.lang.String.format;

Expand All @@ -35,6 +37,20 @@ public final class SqlTimeWithTimeZone

public static SqlTimeWithTimeZone newInstance(int precision, long picoseconds, int offsetMinutes)
{
if (precision < 0 || precision > 12) {
throw new IllegalArgumentException("Invalid precision: " + precision);
}
if (rescale(rescale(picoseconds, 12, precision), precision, 12) != picoseconds) {
throw new IllegalArgumentException(format("picoseconds contains data beyond specified precision (%s): %s", precision, picoseconds));
}
if (picoseconds < 0 || picoseconds >= PICOSECONDS_PER_DAY) {
throw new IllegalArgumentException("picoseconds is out of range: " + picoseconds);
}
// TIME WITH TIME ZONE's valid offsets are [-14:00, 14:00]
if (offsetMinutes < -14 * 60 || offsetMinutes > 14 * 60) {
throw new IllegalArgumentException("offsetMinutes is out of range: " + offsetMinutes);
}

return new SqlTimeWithTimeZone(precision, picoseconds, offsetMinutes);
}

Expand Down

0 comments on commit 2ef9875

Please sign in to comment.