@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); }
@ScalarFunction("from_unixtime") @LiteralParameters("x") @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) public static long fromUnixTime(@SqlType(StandardTypes.DOUBLE) double unixTime, @SqlType("varchar(x)") Slice zoneId) { return packDateTimeWithZone(Math.round(unixTime * 1000), zoneId.toStringUtf8()); }
@ScalarFunction("from_unixtime") @LiteralParameters("x") @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) public static long fromUnixTime(@SqlType(StandardTypes.DOUBLE) double unixTime, @SqlType("varchar(x)") Slice zoneId) { return packDateTimeWithZone(Math.round(unixTime * 1000), zoneId.toStringUtf8()); }
@Test public void testDecode() { tester.assertDecodedAs("33701000", TIME, 33701000); tester.assertDecodedAs("\"33701000\"", TIME, 33701000); tester.assertDecodedAs("33701000", TIME_WITH_TIME_ZONE, packDateTimeWithZone(33701000, UTC_KEY)); tester.assertDecodedAs("\"33701000\"", TIME_WITH_TIME_ZONE, packDateTimeWithZone(33701000, UTC_KEY)); tester.assertDecodedAs("1519032101123", TIMESTAMP, 1519032101123L); tester.assertDecodedAs("\"1519032101123\"", TIMESTAMP, 1519032101123L); tester.assertDecodedAs("1519032101123", TIMESTAMP_WITH_TIME_ZONE, packDateTimeWithZone(1519032101123L, UTC_KEY)); tester.assertDecodedAs("\"1519032101123\"", TIMESTAMP_WITH_TIME_ZONE, packDateTimeWithZone(1519032101123L, UTC_KEY)); }
@Description("current timestamp with time zone") @ScalarFunction(value = "current_timestamp", alias = "now") @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) public static long currentTimestamp(ConnectorSession session) { return packDateTimeWithZone(session.getStartTime(), session.getTimeZoneKey()); }
@Test public void testDecode() { tester.assertDecodedAs("33701000", TIME, 33701000); tester.assertDecodedAs("\"33701000\"", TIME, 33701000); tester.assertDecodedAs("33701000", TIME_WITH_TIME_ZONE, packDateTimeWithZone(33701000, UTC_KEY)); tester.assertDecodedAs("\"33701000\"", TIME_WITH_TIME_ZONE, packDateTimeWithZone(33701000, UTC_KEY)); tester.assertDecodedAs("1519032101123", TIMESTAMP, 1519032101123L); tester.assertDecodedAs("\"1519032101123\"", TIMESTAMP, 1519032101123L); tester.assertDecodedAs("1519032101123", TIMESTAMP_WITH_TIME_ZONE, packDateTimeWithZone(1519032101123L, UTC_KEY)); tester.assertDecodedAs("\"1519032101123\"", TIMESTAMP_WITH_TIME_ZONE, packDateTimeWithZone(1519032101123L, UTC_KEY)); }
@Override protected Object getGreaterValue(Object value) { // time zone doesn't matter for ordering return packDateTimeWithZone(unpackMillisUtc((Long) value) + 10, getTimeZoneKeyForOffset(33)); } }
@Override protected Object getGreaterValue(Object value) { // time zone doesn't matter for ordering return packDateTimeWithZone(unpackMillisUtc((Long) value) + 10, getTimeZoneKeyForOffset(33)); } }
@Override protected Object getGreaterValue(Object value) { // time zone doesn't matter for ordering return packDateTimeWithZone(unpackMillisUtc((Long) value) + 10, getTimeZoneKeyForOffset(33)); } }
@Override protected Object getGreaterValue(Object value) { // time zone doesn't matter for ordering return packDateTimeWithZone(unpackMillisUtc((Long) value) + 10, getTimeZoneKeyForOffset(33)); } }
@Description("current timestamp with time zone") @ScalarFunction(value = "current_timestamp", alias = "now") @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) public static long currentTimestamp(ConnectorSession session) { return packDateTimeWithZone(session.getStartTime(), session.getTimeZoneKey()); }
@ScalarOperator(CAST) @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) public static long castToTimestampWithTimeZone(ConnectorSession session, @SqlType(StandardTypes.DATE) long value) { long utcMillis = TimeUnit.DAYS.toMillis(value); // date is encoded as milliseconds at midnight in UTC // convert to midnight in the session timezone ISOChronology chronology = getChronology(session.getTimeZoneKey()); long millis = utcMillis - chronology.getZone().getOffset(utcMillis); return packDateTimeWithZone(millis, session.getTimeZoneKey()); }
@Test public void testDecode() { tester.assertDecodedAs("\"Mon Feb 12 13:15:16 Z 2018\"", DATE, 17574); // TODO should it be supported really? tester.assertDecodedAs("\"Thu Jan 01 13:15:19 Z 1970\"", TIME, 47719000); // TODO should it be supported really? tester.assertDecodedAs("\"Thu Jan 01 13:15:19 Z 1970\"", TIME_WITH_TIME_ZONE, packDateTimeWithZone(47719000, UTC_KEY)); // TODO should it be supported really? tester.assertDecodedAs("\"Fri Feb 09 13:15:19 Z 2018\"", TIMESTAMP, 1518182119000L); tester.assertDecodedAs("\"Fri Feb 09 13:15:19 Z 2018\"", TIMESTAMP_WITH_TIME_ZONE, packDateTimeWithZone(1518182119000L, UTC_KEY)); tester.assertDecodedAs("\"Fri Feb 09 15:15:19 +02:00 2018\"", TIMESTAMP, 1518182119000L); tester.assertDecodedAs("\"Fri Feb 09 15:15:19 +02:00 2018\"", TIMESTAMP_WITH_TIME_ZONE, packDateTimeWithZone(1518182119000L, UTC_KEY)); }
@Test public void testDecode() { tester.assertDecodedAs("\"Mon Feb 12 13:15:16 Z 2018\"", DATE, 17574); // TODO should it be supported really? tester.assertDecodedAs("\"Thu Jan 01 13:15:19 Z 1970\"", TIME, 47719000); // TODO should it be supported really? tester.assertDecodedAs("\"Thu Jan 01 13:15:19 Z 1970\"", TIME_WITH_TIME_ZONE, packDateTimeWithZone(47719000, UTC_KEY)); // TODO should it be supported really? tester.assertDecodedAs("\"Fri Feb 09 13:15:19 Z 2018\"", TIMESTAMP, 1518182119000L); tester.assertDecodedAs("\"Fri Feb 09 13:15:19 Z 2018\"", TIMESTAMP_WITH_TIME_ZONE, packDateTimeWithZone(1518182119000L, UTC_KEY)); tester.assertDecodedAs("\"Fri Feb 09 15:15:19 +02:00 2018\"", TIMESTAMP, 1518182119000L); tester.assertDecodedAs("\"Fri Feb 09 15:15:19 +02:00 2018\"", TIMESTAMP_WITH_TIME_ZONE, packDateTimeWithZone(1518182119000L, UTC_KEY)); }
public static void assertTimeZone(String zoneId, DateTimeZone dateTimeZone) { long dateTimeWithTimeZone = packDateTimeWithZone(new DateTime(42, dateTimeZone)); assertEquals(packDateTimeWithZone((long) 42, dateTimeZone.toTimeZone().getID()), dateTimeWithTimeZone); DateTimeZone unpackedZone = unpackDateTimeZone(dateTimeWithTimeZone); assertDateTimeZoneEquals(zoneId, unpackedZone); }
public static void assertTimeZone(String zoneId, DateTimeZone dateTimeZone) { long dateTimeWithTimeZone = packDateTimeWithZone(new DateTime(42, dateTimeZone)); assertEquals(packDateTimeWithZone((long) 42, dateTimeZone.toTimeZone().getID()), dateTimeWithTimeZone); DateTimeZone unpackedZone = unpackDateTimeZone(dateTimeWithTimeZone); assertDateTimeZoneEquals(zoneId, unpackedZone); }
@ScalarOperator(CAST) @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) public static long castToTimestampWithTimeZone(ConnectorSession session, @SqlType(StandardTypes.DATE) long value) { long utcMillis = TimeUnit.DAYS.toMillis(value); // date is encoded as milliseconds at midnight in UTC // convert to midnight in the session timezone ISOChronology chronology = getChronology(session.getTimeZoneKey()); long millis = utcMillis - chronology.getZone().getOffset(utcMillis); return packDateTimeWithZone(millis, session.getTimeZoneKey()); }
@Test public void testDecode() { timestampTester.assertDecodedAs("\"02/2018/19 9:20:11\"", TIMESTAMP, 1519032011000L); timestampWithTimeZoneTester.assertDecodedAs("\"02/2018/19 11:20:11 +02:00\"", TIMESTAMP, 1519032011000L); timestampTester.assertDecodedAs("\"02/2018/19 9:20:11\"", TIMESTAMP_WITH_TIME_ZONE, packDateTimeWithZone(1519032011000L, UTC_KEY)); timestampWithTimeZoneTester.assertDecodedAs("\"02/2018/19 11:20:11 +02:00\"", TIMESTAMP_WITH_TIME_ZONE, packDateTimeWithZone(1519032011000L, UTC_KEY)); // TODO: extract TZ from pattern timeTester.assertDecodedAs("\"15:13:18\"", TIME, 47718000); timeJustHourTester.assertDecodedAs("\"15\"", TIME, 54000000); timeJustHourTester.assertDecodedAs("15", TIME, 54000000); timeTester.assertDecodedAs("\"15:13:18\"", TIME_WITH_TIME_ZONE, packDateTimeWithZone(47718000, UTC_KEY)); dateTester.assertDecodedAs("\"02/2018/11\"", DATE, 17573); }
@ScalarFunction(value = "at_timezone", hidden = true) @LiteralParameters("x") @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) public static long timestampAtTimeZone(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone, @SqlType("varchar(x)") Slice zoneId) { return packDateTimeWithZone(unpackMillisUtc(timestampWithTimeZone), zoneId.toStringUtf8()); }
@ScalarFunction(value = "at_timezone", hidden = true) @LiteralParameters("x") @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) public static long timestampAtTimeZone(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone, @SqlType("varchar(x)") Slice zoneId) { return packDateTimeWithZone(unpackMillisUtc(timestampWithTimeZone), zoneId.toStringUtf8()); }