/*
 * 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.prestosql.util;

import io.prestosql.spi.type.TimeZoneKey;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.testng.annotations.Test;

import java.time.ZoneId;
import java.util.TreeSet;

import static io.prestosql.spi.type.DateTimeEncoding.packDateTimeWithZone;
import static io.prestosql.spi.type.TimeZoneKey.isUtcZoneId;
import static io.prestosql.util.DateTimeZoneIndex.getDateTimeZone;
import static io.prestosql.util.DateTimeZoneIndex.packDateTimeWithZone;
import static io.prestosql.util.DateTimeZoneIndex.unpackDateTimeZone;
import static org.testng.Assert.assertEquals;

public class TestTimeZoneUtils
{
    @Test
    public void testNamedZones()
    {
        TreeSet<String> jdkZones = new TreeSet<>(ZoneId.getAvailableZoneIds());
        for (String zoneId : jdkZones) {
            if (zoneId.startsWith("Etc/") || zoneId.startsWith("GMT") || zoneId.startsWith("SystemV/")) {
                continue;
            }

            DateTimeZone dateTimeZone = DateTimeZone.forID(zoneId);
            DateTimeZone indexedZone = getDateTimeZone(TimeZoneKey.getTimeZoneKey(zoneId));

            assertDateTimeZoneEquals(zoneId, indexedZone);
            assertTimeZone(zoneId, dateTimeZone);
        }
    }

    @Test
    public void testOffsets()
    {
        for (int offsetHours = -13; offsetHours < 14; offsetHours++) {
            for (int offsetMinutes = 0; offsetMinutes < 60; offsetMinutes++) {
                DateTimeZone dateTimeZone = DateTimeZone.forOffsetHoursMinutes(offsetHours, offsetMinutes);
                assertTimeZone(dateTimeZone.getID(), dateTimeZone);
            }
        }
    }

    public static void assertTimeZone(String zoneId, DateTimeZone dateTimeZone)
    {
        long dateTimeWithTimeZone = packDateTimeWithZone(new DateTime(42, dateTimeZone));
        assertEquals(packDateTimeWithZone(42L, dateTimeZone.toTimeZone().getID()), dateTimeWithTimeZone);
        DateTimeZone unpackedZone = unpackDateTimeZone(dateTimeWithTimeZone);
        assertDateTimeZoneEquals(zoneId, unpackedZone);
    }

    public static void assertDateTimeZoneEquals(String zoneId, DateTimeZone actualTimeZone)
    {
        DateTimeZone expectedDateTimeZone;
        if (isUtcZoneId(zoneId)) {
            expectedDateTimeZone = DateTimeZone.UTC;
        }
        else {
            expectedDateTimeZone = DateTimeZone.forID(zoneId);
        }

        assertEquals(actualTimeZone, expectedDateTimeZone);
    }
}

@@ -847,32 +847,170 @@
 teTimeWithZone;%0A
+import static io.prestosql.spi.type.DateTimeEncoding.unpackMillisUtc;%0Aimport static io.prestosql.spi.type.DateTimeEncoding.unpackZoneKey;%0A
 import static io
@@ -1050,24 +1050,24 @@
 sUtcZoneId;%0A
-
 import stati
@@ -1255,32 +1255,71 @@
 ckDateTimeZone;%0A
+import static java.lang.String.format;%0A
 import static or
@@ -1347,16 +1347,54 @@
 tEquals;
+%0Aimport static org.testng.Assert.fail;
 %0A%0Apublic
@@ -2526,35 +2526,31 @@
    long 
-dateTimeWithTimeZon
+packWithDateTim
 e = pack
@@ -2607,29 +2607,38 @@
         
-assertEquals(
+long packWithZoneId = 
 packDate
@@ -2693,107 +2693,629 @@
 D())
-, dateTimeWithTimeZone);%0A        DateTimeZone unpackedZone = unpackDateTimeZone(dateTimeWithTimeZon
+;%0A        if (packWithDateTime != packWithZoneId) %7B%0A            fail(format(%0A                    %22packWithDateTime and packWithZoneId differ for zone %5B%25s%5D / %5B%25s%5D: %25s %5B%25s %25s%5D and %25s %5B%25s %25s%5D%22,%0A                    zoneId,%0A                    dateTimeZone,%0A                    packWithDateTime,%0A                    unpackMillisUtc(packWithDateTime),%0A                    unpackZoneKey(packWithDateTime),%0A                    packWithZoneId,%0A                    unpackMillisUtc(packWithZoneId),%0A                    unpackZoneKey(packWithZoneId)));%0A        %7D%0A        DateTimeZone unpackedZone = unpackDateTimeZone(packWithDateTim
 e);%0A

Produce a better message when test fails