private static ZoneOffset toZoneOffset(TimeZoneKey timeZoneKey) { requireNonNull(timeZoneKey, "timeZoneKey is null"); if (Objects.equals("UTC", timeZoneKey.getId())) { return ZoneOffset.UTC; } checkArgument(timeZoneKey.getId().matches("[+-]\\d\\d:\\d\\d"), "Not a zone-offset timezone: %s", timeZoneKey); return ZoneOffset.of(timeZoneKey.getId()); }
public static TimeZoneKey unpackZoneKey(long dateTimeWithTimeZone) { return getTimeZoneKey((short) (dateTimeWithTimeZone & TIME_ZONE_MASK)); }
@Test public void testZoneKeyLookup() { for (TimeZoneKey timeZoneKey : TimeZoneKey.getTimeZoneKeys()) { assertSame(TimeZoneKey.getTimeZoneKey(timeZoneKey.getKey()), timeZoneKey); assertSame(TimeZoneKey.getTimeZoneKey(timeZoneKey.getId()), timeZoneKey); assertSame(TimeZoneKey.getTimeZoneKey(timeZoneKey.getId().toUpperCase(ENGLISH)), timeZoneKey); assertSame(TimeZoneKey.getTimeZoneKey(timeZoneKey.getId().toLowerCase(ENGLISH)), timeZoneKey); } }
@Test public void testMaxTimeZoneKey() { boolean foundMax = false; for (TimeZoneKey timeZoneKey : TimeZoneKey.getTimeZoneKeys()) { assertTrue(timeZoneKey.getKey() <= MAX_TIME_ZONE_KEY, timeZoneKey + " key is larger than max key " + MAX_TIME_ZONE_KEY); foundMax = foundMax || (timeZoneKey.getKey() == MAX_TIME_ZONE_KEY); } assertTrue(foundMax, "Did not find a time zone with the MAX_TIME_ZONE_KEY"); }
@Test public void testSetTimeZoneId() throws Exception { TimeZoneKey defaultZoneKey = TimeZoneKey.getTimeZoneKey(TimeZone.getDefault().getID()); DateTimeZone defaultZone = DateTimeZone.forTimeZone(TimeZone.getDefault()); String sql = "SELECT current_timezone() zone, TIMESTAMP '2001-02-03 3:04:05' ts"; try (Connection connection = createConnection()) { try (Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery(sql)) { assertTrue(rs.next()); assertEquals(rs.getString("zone"), defaultZoneKey.getId()); assertEquals(rs.getTimestamp("ts"), new Timestamp(new DateTime(2001, 2, 3, 3, 4, 5, defaultZone).getMillis())); } connection.unwrap(PrestoConnection.class).setTimeZoneId("UTC"); try (Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery(sql)) { assertTrue(rs.next()); assertEquals(rs.getString("zone"), "UTC"); assertEquals(rs.getTimestamp("ts"), new Timestamp(new DateTime(2001, 2, 3, 3, 4, 5, DateTimeZone.UTC).getMillis())); } } }
assertEquals(UTC_KEY.getKey(), 0); assertEquals(UTC_KEY.getId(), "UTC"); assertSame(TimeZoneKey.getTimeZoneKey((short) 0), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("UTC"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("Z"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("Zulu"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("zulu"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("ZULU"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("UT"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("UCT"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("Universal"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("GMT"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("GMT0"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("GMT+0"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("GMT-0"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("GMT+00:00"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("GMT-00:00"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("+00:00"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("-00:00"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("etc/utc"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("etc/gmt"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("etc/gmt+0"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("etc/gmt+00:00"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("etc/gmt-00:00"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("etc/ut"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("etc/UT"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("etc/UCT"), UTC_KEY);
@Test public void testZoneKeyData() { Hasher hasher = Hashing.murmur3_128().newHasher(); SortedSet<TimeZoneKey> timeZoneKeysSortedByKey = ImmutableSortedSet.copyOf(new Comparator<TimeZoneKey>() { @Override public int compare(TimeZoneKey left, TimeZoneKey right) { return Short.compare(left.getKey(), right.getKey()); } }, TimeZoneKey.getTimeZoneKeys()); for (TimeZoneKey timeZoneKey : timeZoneKeysSortedByKey) { hasher.putShort(timeZoneKey.getKey()); hasher.putString(timeZoneKey.getId(), StandardCharsets.UTF_8); } // Zone file should not (normally) be changed, so let's make this more difficult assertEquals(hasher.hash().asLong(), -4582158485614614451L, "zone-index.properties file contents changed!"); }
public static DateTimeZone getDateTimeZone(TimeZoneKey zoneKey) { return DATE_TIME_ZONES[zoneKey.getKey()]; }
public static long packDateTimeWithZone(long millisUtc, int offsetMinutes) { return packDateTimeWithZone(millisUtc, getTimeZoneKeyForOffset(offsetMinutes)); }
public static Predicate<String> ignoredZone() { return zoneId -> isUtcZoneId(zoneId) || zoneId.startsWith("Etc/"); } }
@Test public void testSetTimeZoneId() throws Exception { TimeZoneKey defaultZoneKey = TimeZoneKey.getTimeZoneKey(TimeZone.getDefault().getID()); DateTimeZone defaultZone = DateTimeZone.forTimeZone(TimeZone.getDefault()); String sql = "SELECT current_timezone() zone, TIMESTAMP '2001-02-03 3:04:05' ts"; try (Connection connection = createConnection()) { try (Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery(sql)) { assertTrue(rs.next()); assertEquals(rs.getString("zone"), defaultZoneKey.getId()); assertEquals(rs.getTimestamp("ts"), new Timestamp(new DateTime(2001, 2, 3, 3, 4, 5, defaultZone).getMillis())); } connection.unwrap(PrestoConnection.class).setTimeZoneId("UTC"); try (Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery(sql)) { assertTrue(rs.next()); assertEquals(rs.getString("zone"), "UTC"); assertEquals(rs.getTimestamp("ts"), new Timestamp(new DateTime(2001, 2, 3, 3, 4, 5, DateTimeZone.UTC).getMillis())); } } }
assertEquals(UTC_KEY.getKey(), 0); assertEquals(UTC_KEY.getId(), "UTC"); assertSame(TimeZoneKey.getTimeZoneKey((short) 0), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("UTC"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("Z"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("Zulu"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("zulu"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("ZULU"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("UT"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("UCT"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("Universal"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("GMT"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("GMT0"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("GMT+0"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("GMT-0"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("GMT+00:00"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("GMT-00:00"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("+00:00"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("-00:00"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("etc/utc"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("etc/gmt"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("etc/gmt+0"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("etc/gmt+00:00"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("etc/gmt-00:00"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("etc/ut"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("etc/UT"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("etc/UCT"), UTC_KEY);
@Test public void testZoneKeyData() { Hasher hasher = Hashing.murmur3_128().newHasher(); SortedSet<TimeZoneKey> timeZoneKeysSortedByKey = ImmutableSortedSet.copyOf(new Comparator<TimeZoneKey>() { @Override public int compare(TimeZoneKey left, TimeZoneKey right) { return Short.compare(left.getKey(), right.getKey()); } }, TimeZoneKey.getTimeZoneKeys()); for (TimeZoneKey timeZoneKey : timeZoneKeysSortedByKey) { hasher.putShort(timeZoneKey.getKey()); hasher.putString(timeZoneKey.getId(), StandardCharsets.UTF_8); } // Zone file should not (normally) be changed, so let's make this more difficult assertEquals(hasher.hash().asLong(), -4582158485614614451L, "zone-index.properties file contents changed!"); }
public static ISOChronology getChronology(TimeZoneKey zoneKey) { return CHRONOLOGIES[zoneKey.getKey()]; }
@ScalarFunction("from_unixtime") @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) public static long fromUnixTime(@SqlType(StandardTypes.DOUBLE) double unixTime, @SqlType(StandardTypes.BIGINT) long hoursOffset, @SqlType(StandardTypes.BIGINT) long minutesOffset) { TimeZoneKey timeZoneKey; try { timeZoneKey = getTimeZoneKeyForOffset(toIntExact(hoursOffset * 60 + minutesOffset)); } catch (IllegalArgumentException e) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e); } return packDateTimeWithZone(Math.round(unixTime * 1000), timeZoneKey); }
@Test public void testMaxTimeZoneKey() { boolean foundMax = false; for (TimeZoneKey timeZoneKey : TimeZoneKey.getTimeZoneKeys()) { assertTrue(timeZoneKey.getKey() <= MAX_TIME_ZONE_KEY, timeZoneKey + " key is larger than max key " + MAX_TIME_ZONE_KEY); foundMax = foundMax || (timeZoneKey.getKey() == MAX_TIME_ZONE_KEY); } assertTrue(foundMax, "Did not find a time zone with the MAX_TIME_ZONE_KEY"); }
public static Predicate<String> ignoredZone() { return zoneId -> isUtcZoneId(zoneId) || zoneId.startsWith("Etc/"); } }
public SqlTimeWithTimeZone(long millisUtc, TimeZone timeZone) { this.millisUtc = millisUtc; this.timeZoneKey = TimeZoneKey.getTimeZoneKey(timeZone.getID()); }
private static ZoneOffset toZoneOffset(TimeZoneKey timeZoneKey) { requireNonNull(timeZoneKey, "timeZoneKey is null"); if (Objects.equals("UTC", timeZoneKey.getId())) { return ZoneOffset.UTC; } checkArgument(timeZoneKey.getId().matches("[+-]\\d\\d:\\d\\d"), "Not a zone-offset timezone: %s", timeZoneKey); return ZoneOffset.of(timeZoneKey.getId()); }
@Test public void testDate() { // Note: there is identical test for PostgreSQL ZoneId jvmZone = ZoneId.systemDefault(); checkState(jvmZone.getId().equals("America/Bahia_Banderas"), "This test assumes certain JVM time zone"); LocalDate dateOfLocalTimeChangeForwardAtMidnightInJvmZone = LocalDate.of(1970, 1, 1); verify(jvmZone.getRules().getValidOffsets(dateOfLocalTimeChangeForwardAtMidnightInJvmZone.atStartOfDay()).isEmpty()); ZoneId someZone = ZoneId.of("Europe/Vilnius"); LocalDate dateOfLocalTimeChangeForwardAtMidnightInSomeZone = LocalDate.of(1983, 4, 1); verify(someZone.getRules().getValidOffsets(dateOfLocalTimeChangeForwardAtMidnightInSomeZone.atStartOfDay()).isEmpty()); LocalDate dateOfLocalTimeChangeBackwardAtMidnightInSomeZone = LocalDate.of(1983, 10, 1); verify(someZone.getRules().getValidOffsets(dateOfLocalTimeChangeBackwardAtMidnightInSomeZone.atStartOfDay().minusMinutes(1)).size() == 2); DataTypeTest testCases = DataTypeTest.create() .addRoundTrip(dateDataType(), LocalDate.of(1952, 4, 3)) // before epoch .addRoundTrip(dateDataType(), LocalDate.of(1970, 1, 1)) .addRoundTrip(dateDataType(), LocalDate.of(1970, 2, 3)) .addRoundTrip(dateDataType(), LocalDate.of(2017, 7, 1)) // summer on northern hemisphere (possible DST) .addRoundTrip(dateDataType(), LocalDate.of(2017, 1, 1)) // winter on northern hemisphere (possible DST on southern hemisphere) .addRoundTrip(dateDataType(), dateOfLocalTimeChangeForwardAtMidnightInJvmZone) .addRoundTrip(dateDataType(), dateOfLocalTimeChangeForwardAtMidnightInSomeZone) .addRoundTrip(dateDataType(), dateOfLocalTimeChangeBackwardAtMidnightInSomeZone); for (String timeZoneId : ImmutableList.of(UTC_KEY.getId(), jvmZone.getId(), someZone.getId())) { Session session = Session.builder(getQueryRunner().getDefaultSession()) .setTimeZoneKey(TimeZoneKey.getTimeZoneKey(timeZoneId)) .build(); testCases.execute(getQueryRunner(), session, mysqlCreateAndInsert("tpch.test_date")); testCases.execute(getQueryRunner(), session, prestoCreateAsSelect("test_date")); } }