Search in sources :

Example 1 with TimeZoneKey.getTimeZoneKey

use of com.facebook.presto.common.type.TimeZoneKey.getTimeZoneKey in project presto by prestodb.

the class TestDateTimeFunctionsBase method testTimeWithTimeZoneAtTimeZone.

@Test
public void testTimeWithTimeZoneAtTimeZone() {
    // this test does use hidden at_timezone function as it is equivalent of using SQL syntax AT TIME ZONE
    // but our test framework doesn't support that syntax directly.
    Session oldKathmanduTimeZoneOffsetSession = Session.builder(this.session).setTimeZoneKey(TIME_ZONE_KEY).setStartTime(new DateTime(1980, 1, 1, 10, 0, 0, DATE_TIME_ZONE).getMillis()).build();
    TimeZoneKey europeWarsawTimeZoneKey = getTimeZoneKey("Europe/Warsaw");
    DateTimeZone europeWarsawTimeZone = getDateTimeZone(europeWarsawTimeZoneKey);
    Session europeWarsawSessionWinter = Session.builder(this.session).setTimeZoneKey(europeWarsawTimeZoneKey).setStartTime(new DateTime(2017, 1, 1, 10, 0, 0, europeWarsawTimeZone).getMillis()).build();
    try (FunctionAssertions europeWarsawAssertionsWinter = new FunctionAssertions(europeWarsawSessionWinter);
        FunctionAssertions oldKathmanduTimeZoneOffsetAssertions = new FunctionAssertions(oldKathmanduTimeZoneOffsetSession)) {
        long millisTenOClockWarsawWinter = new DateTime(1970, 1, 1, 9, 0, 0, 0, UTC_TIME_ZONE).getMillis();
        // Simple shift to UTC
        europeWarsawAssertionsWinter.assertFunction("at_timezone(TIME '10:00 Europe/Warsaw', 'UTC')", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(millisTenOClockWarsawWinter, UTC_KEY));
        // Simple shift to fixed TZ
        europeWarsawAssertionsWinter.assertFunction("at_timezone(TIME '10:00 Europe/Warsaw', '+00:45')", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(millisTenOClockWarsawWinter, getTimeZoneKey("+00:45")));
        // Simple shift to geographical TZ
        europeWarsawAssertionsWinter.assertFunction("at_timezone(TIME '10:00 Europe/Warsaw', 'America/New_York')", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(millisTenOClockWarsawWinter, getTimeZoneKey("America/New_York")));
        // No shift but different time zone
        europeWarsawAssertionsWinter.assertFunction("at_timezone(TIME '10:00 Europe/Warsaw', 'Europe/Berlin')", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(millisTenOClockWarsawWinter, getTimeZoneKey("Europe/Berlin")));
        // Noop on UTC
        assertFunction("at_timezone(TIME '10:00 UTC', 'UTC')", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 10, 0, 0, 0, UTC_TIME_ZONE).getMillis(), TimeZoneKey.UTC_KEY));
        // Noop on other TZ
        europeWarsawAssertionsWinter.assertFunction("at_timezone(TIME '10:00 Europe/Warsaw', 'Europe/Warsaw')", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(millisTenOClockWarsawWinter, europeWarsawTimeZoneKey));
        // Noop on other TZ on different session TZ
        assertFunction("at_timezone(TIME '10:00 Europe/Warsaw', 'Europe/Warsaw')", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(millisTenOClockWarsawWinter, europeWarsawTimeZoneKey));
        // Shift through days back
        europeWarsawAssertionsWinter.assertFunction("at_timezone(TIME '2:00 Europe/Warsaw', 'America/New_York')", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 20, 0, 0, 0, getDateTimeZone(getTimeZoneKey("America/New_York"))).getMillis(), getTimeZoneKey("America/New_York")));
        // Shift through days forward
        europeWarsawAssertionsWinter.assertFunction("at_timezone(TIME '22:00 America/New_York', 'Europe/Warsaw')", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 4, 0, 0, 0, europeWarsawTimeZone).getMillis(), europeWarsawTimeZoneKey));
        // Shift backward on min value
        europeWarsawAssertionsWinter.assertFunction("at_timezone(TIME '00:00 +14:00', '+13:00')", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 23, 0, 0, 0, getDateTimeZone(getTimeZoneKey("+13:00"))).getMillis(), getTimeZoneKey("+13:00")));
        // Shift backward on min value
        europeWarsawAssertionsWinter.assertFunction("at_timezone(TIME '00:00 +14:00', '-14:00')", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 20, 0, 0, 0, getDateTimeZone(getTimeZoneKey("-14:00"))).getMillis(), getTimeZoneKey("-14:00")));
        // Shift backward on max value
        europeWarsawAssertionsWinter.assertFunction("at_timezone(TIME '23:59:59.999 +14:00', '+13:00')", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 22, 59, 59, 999, getDateTimeZone(getTimeZoneKey("+13:00"))).getMillis(), getTimeZoneKey("+13:00")));
        // Shift forward on max value
        europeWarsawAssertionsWinter.assertFunction("at_timezone(TIME '23:59:59.999 +14:00', '-14:00')", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 19, 59, 59, 999, getDateTimeZone(getTimeZoneKey("-14:00"))).getMillis(), getTimeZoneKey("-14:00")));
        // Asia/Kathmandu used +5:30 TZ until 1986 and than switched to +5:45
        // This test checks if we do use offset of time zone valid currently and not the historical one
        assertFunction("at_timezone(TIME '10:00 Asia/Kathmandu', 'UTC')", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 4, 15, 0, 0, UTC_TIME_ZONE).getMillis(), TimeZoneKey.UTC_KEY));
        // Noop when time zone doesn't change
        TimeZoneKey kabul = TimeZoneKey.getTimeZoneKey("Asia/Kabul");
        assertFunction("at_timezone(TIME '10:00 Asia/Kabul', 'Asia/Kabul')", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 10, 0, 0, 0, getDateTimeZone(kabul)).getMillis(), kabul));
        // This test checks if the TZ offset isn't calculated on other fixed point in time by checking if
        // session started in 1980 would get historical Asia/Kathmandu offset.
        oldKathmanduTimeZoneOffsetAssertions.assertFunction("at_timezone(TIME '10:00 Asia/Kathmandu', 'UTC')", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 4, 30, 0, 0, UTC_TIME_ZONE).getMillis(), TimeZoneKey.UTC_KEY));
        // Check simple interval shift
        europeWarsawAssertionsWinter.assertFunction("at_timezone(TIME '10:00 +01:00', INTERVAL '2' HOUR)", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 11, 0, 0, 0, getDateTimeZone(getTimeZoneKey("+02:00"))).getMillis(), getTimeZoneKey("+02:00")));
        // Check to high interval shift
        europeWarsawAssertionsWinter.assertInvalidFunction("at_timezone(TIME '10:00 +01:00', INTERVAL '60' HOUR)", StandardErrorCode.INVALID_FUNCTION_ARGUMENT, "Invalid offset minutes 3600");
    }
}
Also used : SqlTimeWithTimeZone(com.facebook.presto.common.type.SqlTimeWithTimeZone) TimeZoneKey.getTimeZoneKey(com.facebook.presto.common.type.TimeZoneKey.getTimeZoneKey) TimeZoneKey(com.facebook.presto.common.type.TimeZoneKey) DateTime(org.joda.time.DateTime) DateTimeZone(org.joda.time.DateTimeZone) DateTimeZoneIndex.getDateTimeZone(com.facebook.presto.util.DateTimeZoneIndex.getDateTimeZone) TestingSession(com.facebook.presto.testing.TestingSession) Session(com.facebook.presto.Session) TestingConnectorSession(com.facebook.presto.testing.TestingConnectorSession) Test(org.testng.annotations.Test)

Aggregations

Session (com.facebook.presto.Session)1 SqlTimeWithTimeZone (com.facebook.presto.common.type.SqlTimeWithTimeZone)1 TimeZoneKey (com.facebook.presto.common.type.TimeZoneKey)1 TimeZoneKey.getTimeZoneKey (com.facebook.presto.common.type.TimeZoneKey.getTimeZoneKey)1 TestingConnectorSession (com.facebook.presto.testing.TestingConnectorSession)1 TestingSession (com.facebook.presto.testing.TestingSession)1 DateTimeZoneIndex.getDateTimeZone (com.facebook.presto.util.DateTimeZoneIndex.getDateTimeZone)1 DateTime (org.joda.time.DateTime)1 DateTimeZone (org.joda.time.DateTimeZone)1 Test (org.testng.annotations.Test)1