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); } }
public SqlTimestampWithTimeZone(long millisUtc, TimeZone timeZone) { this.millisUtc = millisUtc; this.timeZoneKey = TimeZoneKey.getTimeZoneKey(timeZone.getID()); }
public SqlTimeWithTimeZone(long millisUtc, TimeZone timeZone) { this.millisUtc = millisUtc; this.timeZoneKey = TimeZoneKey.getTimeZoneKey(timeZone.getID()); }
public static long packDateTimeWithZone(long millisUtc, String zoneId) { return packDateTimeWithZone(millisUtc, getTimeZoneKey(zoneId)); }
public void assertTimeZoneNotSupported(String zoneId) { try { TimeZoneKey.getTimeZoneKey(zoneId); fail("expect TimeZoneNotSupportedException"); } catch (TimeZoneNotSupportedException e) { // expected } } }
@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())); } } }
@Test public void testCastToTimeWithTimeZoneDSTIsNotAppliedWhenTimeCrossesDST() { // Australia/Sydney will switch DST a second after session start // For simplicity we have to use time zone that is going forward when entering DST zone with 1970-01-01 Session session = Session.builder(this.session) .setTimeZoneKey(getTimeZoneKey("Australia/Sydney")) .setStartTime(new DateTime(2017, 10, 1, 1, 59, 59, 999, getDateTimeZone(getTimeZoneKey("Australia/Sydney"))).getMillis()) .build(); try (FunctionAssertions localAssertions = new FunctionAssertions(session)) { localAssertions.assertFunctionString("cast(TIME '12:00:00.000' as time with time zone)", TIME_WITH_TIME_ZONE, "12:00:00.000 Australia/Sydney"); } }
@Test public void testCurrentDateTimezone() { TimeZoneKey kievTimeZoneKey = getTimeZoneKey("Europe/Kiev"); TimeZoneKey bahiaBanderasTimeZoneKey = getTimeZoneKey("America/Bahia_Banderas"); // The zone has 'gap' on 1970-01-01 TimeZoneKey montrealTimeZoneKey = getTimeZoneKey("America/Montreal"); long timeIncrement = TimeUnit.MINUTES.toMillis(53); // We expect UTC millis later on so we have to use UTC chronology for (long instant = ISOChronology.getInstanceUTC().getDateTimeMillis(2000, 6, 15, 0, 0, 0, 0); instant < ISOChronology.getInstanceUTC().getDateTimeMillis(2016, 6, 15, 0, 0, 0, 0); instant += timeIncrement) { assertCurrentDateAtInstant(kievTimeZoneKey, instant); assertCurrentDateAtInstant(bahiaBanderasTimeZoneKey, instant); assertCurrentDateAtInstant(montrealTimeZoneKey, instant); assertCurrentDateAtInstant(TIME_ZONE_KEY, instant); } }
protected TestTimeWithTimeZoneBase(boolean legacyTimestamp) { super(testSessionBuilder() .setSystemProperty("legacy_timestamp", String.valueOf(legacyTimestamp)) .setTimeZoneKey(getTimeZoneKey("+06:09")) .build()); }
@ScalarFunction(value = "at_timezone", hidden = true) @LiteralParameters("x") @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) public static long timeAtTimeZone(ConnectorSession session, @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long timeWithTimeZone, @SqlType("varchar(x)") Slice zoneId) { return timeAtTimeZone(session, timeWithTimeZone, getTimeZoneKey(zoneId.toStringUtf8())); }
public TestArrayReduceFunction() { super(testSessionBuilder().setTimeZoneKey(getTimeZoneKey("Pacific/Kiritimati")).build()); }
public TestArrayTransformFunction() { super(testSessionBuilder().setTimeZoneKey(getTimeZoneKey("Pacific/Kiritimati")).build()); }
public TestLambdaExpression() { this(testSessionBuilder().setTimeZoneKey(getTimeZoneKey("Pacific/Kiritimati")).build()); }
@Test public void testCastToTimeWithTimeZoneWithTZWithRulesChanged() { TimeZoneKey timeZoneThatChangedSince1970 = getTimeZoneKey("Asia/Kathmandu"); DateTimeZone dateTimeZoneThatChangedSince1970 = getDateTimeZone(timeZoneThatChangedSince1970); Session session = Session.builder(this.session) .setTimeZoneKey(timeZoneThatChangedSince1970) .build(); try (FunctionAssertions localAssertions = new FunctionAssertions(session)) { localAssertions.assertFunction( "cast(TIME '03:04:05.321' as time with time zone)", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 3, 4, 5, 321, dateTimeZoneThatChangedSince1970).getMillis(), dateTimeZoneThatChangedSince1970.toTimeZone())); } }
public static LocalQueryRunner createQueryRunner(Map<String, String> catalogProperties, Class<? extends AtopFactory> factoryClass) { Session session = testSessionBuilder() .setCatalog("atop") .setSchema("default") .setTimeZoneKey(TimeZoneKey.getTimeZoneKey(TimeZone.getDefault().getID())) .build(); LocalQueryRunner queryRunner = new LocalQueryRunner(session); try { AtopConnectorFactory connectorFactory = new AtopConnectorFactory(factoryClass, LocalAtopQueryRunner.class.getClassLoader()); ImmutableMap.Builder<String, String> properties = ImmutableMap.<String, String>builder() .putAll(catalogProperties) .put("atop.max-history-days", "1"); queryRunner.createCatalog("atop", connectorFactory, properties.build()); return queryRunner; } catch (Exception e) { closeAllSuppress(e, queryRunner); throw e; } } }
private static SqlTimeWithTimeZone toTimeWithTimeZone(OffsetTime offsetTime) { return new SqlTimeWithTimeZone( millisUtc(offsetTime), TimeZoneKey.getTimeZoneKey(offsetTime.getOffset().getId())); }
@Override public ConnectorPageSource createPageSource(Session session, Split split, List<ColumnHandle> columns) { assertInstanceOf(split.getConnectorSplit(), FunctionAssertions.TestSplit.class); FunctionAssertions.TestSplit testSplit = (FunctionAssertions.TestSplit) split.getConnectorSplit(); if (testSplit.isRecordSet()) { RecordSet records = InMemoryRecordSet.builder(ImmutableList.of(BIGINT, VARCHAR, DOUBLE, BOOLEAN, BIGINT, VARCHAR, VARCHAR, TIMESTAMP_WITH_TIME_ZONE, VARBINARY, INTEGER)) .addRow( 1234L, "hello", 12.34, true, new DateTime(2001, 8, 22, 3, 4, 5, 321, DateTimeZone.UTC).getMillis(), "%el%", null, packDateTimeWithZone(new DateTime(1970, 1, 1, 0, 1, 0, 999, DateTimeZone.UTC).getMillis(), TimeZoneKey.getTimeZoneKey("Z")), Slices.wrappedBuffer((byte) 0xab), 1234) .build(); return new RecordPageSource(records); } else { return new FixedPageSource(ImmutableList.of(SOURCE_PAGE)); } } }
private void testTimeRepresentationOnDate(DateTime date, String timeLiteral, Type expectedType, Object expected) { Session localSession = testSessionBuilder() .setTimeZoneKey(getTimeZoneKey("America/Los_Angeles")) .setStartTime(date.getMillis()) .setSystemProperty("legacy_timestamp", "true") .build(); try (FunctionAssertions localAssertions = new FunctionAssertions(localSession)) { localAssertions.assertFunction(timeLiteral, expectedType, expected); localAssertions.assertFunctionString(timeLiteral, expectedType, valueFromLiteral(timeLiteral)); } }
private void testTimeRepresentationOnDate(DateTime date, String timeLiteral, Type expectedType, Object expected) { Session localSession = testSessionBuilder() .setTimeZoneKey(getTimeZoneKey("America/Los_Angeles")) .setStartTime(date.getMillis()) .setSystemProperty("legacy_timestamp", "false") .build(); try (FunctionAssertions localAssertions = new FunctionAssertions(localSession)) { localAssertions.assertFunction(timeLiteral, expectedType, expected); localAssertions.assertFunctionString(timeLiteral, expectedType, valueFromLiteral(timeLiteral)); } }