/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.trino.spi.type;

import com.fasterxml.jackson.annotation.JsonValue;

import java.util.Objects;

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_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 java.lang.Math.abs;
import static java.lang.String.format;

public final class SqlTimeWithTimeZone
{
    private final int precision;
    private final long picos;
    private final int offsetMinutes;

    public static SqlTimeWithTimeZone newInstance(int precision, long picoseconds, int offsetMinutes)
    {
        return new SqlTimeWithTimeZone(precision, picoseconds, offsetMinutes);
    }

    private SqlTimeWithTimeZone(int precision, long picos, int offsetMinutes)
    {
        this.precision = precision;
        this.picos = picos;
        this.offsetMinutes = offsetMinutes;
    }

    public long getPicos()
    {
        return picos;
    }

    public int getOffsetMinutes()
    {
        return offsetMinutes;
    }

    @Override
    public boolean equals(Object o)
    {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        SqlTimeWithTimeZone other = (SqlTimeWithTimeZone) o;
        return precision == other.precision &&
                picos == other.picos &&
                offsetMinutes == other.offsetMinutes;
    }

    @Override
    public int hashCode()
    {
        return Objects.hash(precision, picos, offsetMinutes);
    }

    @JsonValue
    @Override
    public String toString()
    {
        StringBuilder builder = new StringBuilder();
        builder.append(format(
                "%02d:%02d:%02d",
                picos / PICOSECONDS_PER_HOUR,
                (picos / PICOSECONDS_PER_MINUTE) % MINUTES_PER_HOUR,
                (picos / PICOSECONDS_PER_SECOND) % SECONDS_PER_MINUTE));

        if (precision > 0) {
            long scaledFraction = (picos % PICOSECONDS_PER_SECOND) / POWERS_OF_TEN[MAX_PRECISION - precision];
            builder.append(".");
            builder.append(format("%0" + precision + "d", scaledFraction));
        }
        builder.append(format("%s%02d:%02d", offsetMinutes >= 0 ? '+' : '-', abs(offsetMinutes / 60), abs(offsetMinutes % 60)));

        return builder.toString();
    }
}

@@ -771,32 +771,96 @@
 NUTES_PER_HOUR;%0A
+import static io.trino.spi.type.Timestamps.PICOSECONDS_PER_DAY;%0A
 import static io
@@ -900,32 +900,32 @@
 CONDS_PER_HOUR;%0A
-
 import static io
@@ -1155,32 +1155,84 @@
 NDS_PER_MINUTE;%0A
+import static io.trino.spi.type.Timestamps.rescale;%0A
 import static ja
@@ -1525,38 +1525,841 @@
  offsetMinutes)%0A
-
     %7B%0A
+        if (precision %3C 0 %7C%7C precision %3E 12) %7B%0A            throw new IllegalArgumentException(%22Invalid precision: %22 + precision);%0A        %7D%0A        if (rescale(rescale(picoseconds, 12, precision), precision, 12) != picoseconds) %7B%0A            throw new IllegalArgumentException(format(%22picoseconds contains data beyond specified precision (%25s): %25s%22, precision, picoseconds));%0A        %7D%0A        if (picoseconds %3C 0 %7C%7C picoseconds %3E= PICOSECONDS_PER_DAY) %7B%0A            throw new IllegalArgumentException(%22picoseconds is out of range: %22 + picoseconds);%0A        %7D%0A        // TIME WITH TIME ZONE's valid offsets are %5B-14:00, 14:00%5D%0A        if (offsetMinutes %3C -14 * 60 %7C%7C offsetMinutes %3E 14 * 60) %7B%0A            throw new IllegalArgumentException(%22offsetMinutes is out of range: %22 + offsetMinutes);%0A        %7D%0A%0A
         return n

Validate time with time zone well-formedness