@Deprecated public static String printTimeWithoutTimeZone(TimeZoneKey timeZoneKey, long value) { return TIME_FORMATTER.withZone(getDateTimeZone(timeZoneKey)).print(value); }
public static DateTimeZone unpackDateTimeZone(long dateTimeWithTimeZone) { return getDateTimeZone(unpackZoneKey(dateTimeWithTimeZone)); }
/** * Parse a string (without a zone) as a value of TIME type, interpreted in {@code timeZoneKey} zone. * * @return stack representation of legacy TIME type */ @Deprecated public static long parseTimeWithoutTimeZone(TimeZoneKey timeZoneKey, String value) { return TIME_FORMATTER.withZone(getDateTimeZone(timeZoneKey)).parseMillis(value); }
private static long timeAtTimeZone(ConnectorSession session, long timeWithTimeZone, TimeZoneKey timeZoneKey) { DateTimeZone sourceTimeZone = getDateTimeZone(unpackZoneKey(timeWithTimeZone)); DateTimeZone targetTimeZone = getDateTimeZone(timeZoneKey); long millis = unpackMillisUtc(timeWithTimeZone); // STEP 1. Calculate source UTC millis in session start millis += valueToSessionTimeZoneOffsetDiff(session.getStartTime(), sourceTimeZone); // STEP 2. Calculate target UTC millis in 1970 millis -= valueToSessionTimeZoneOffsetDiff(session.getStartTime(), targetTimeZone); // STEP 3. Make sure that value + offset is in 0 - 23:59:59.999 long localMillis = millis + targetTimeZone.getOffset(0); // Loops up to 2 times in total while (localMillis > TimeUnit.DAYS.toMillis(1)) { millis -= TimeUnit.DAYS.toMillis(1); localMillis -= TimeUnit.DAYS.toMillis(1); } while (localMillis < 0) { millis += TimeUnit.DAYS.toMillis(1); localMillis += TimeUnit.DAYS.toMillis(1); } return packDateTimeWithZone(millis, timeZoneKey); }
@Test public void testFromUnixTimeWithOffset() { DateTime dateTime = new DateTime(2001, 1, 22, 3, 4, 5, 0, DATE_TIME_ZONE); double seconds = dateTime.getMillis() / 1000.0; int timeZoneHoursOffset = 1; int timezoneMinutesOffset = 10; DateTime expected = new DateTime(dateTime, getDateTimeZone(getTimeZoneKeyForOffset((timeZoneHoursOffset * 60L) + timezoneMinutesOffset))); assertFunction("from_unixtime(" + seconds + ", " + timeZoneHoursOffset + ", " + timezoneMinutesOffset + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(expected)); // test invalid minute offsets assertInvalidFunction("from_unixtime(0, 1, 10000)", INVALID_FUNCTION_ARGUMENT); assertInvalidFunction("from_unixtime(0, 10000, 0)", INVALID_FUNCTION_ARGUMENT); assertInvalidFunction("from_unixtime(0, -100, 100)", INVALID_FUNCTION_ARGUMENT); }
@Test public void testExtract() throws Exception { for (DateTime left : dateTimeValues) { for (Field field : Field.values()) { Long expected = null; Long millis = null; if (left != null) { millis = left.getMillis(); expected = callExtractFunction(TEST_SESSION.toConnectorSession(), millis, field); } DateTimeZone zone = getDateTimeZone(TEST_SESSION.getTimeZoneKey()); long zoneOffsetMinutes = millis != null ? MILLISECONDS.toMinutes(zone.getOffset(millis)) : 0; String expressionPattern = format( "extract(%s from from_unixtime(%%s / 1000.0E0, %s, %s))", field, zoneOffsetMinutes / 60, zoneOffsetMinutes % 60); assertExecute(generateExpression(expressionPattern, millis), BIGINT, expected); } } Futures.allAsList(futures).get(); }
DateTimeZone indexedZone = getDateTimeZone(TimeZoneKey.getTimeZoneKey(zoneId));
secondOfMinute, millisOfSecond, getDateTimeZone(session.getTimeZoneKey()), session.getTimeZoneKey(), session.toConnectorSession());
@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())); } }
@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"); } }
DateTimeZone europeWarsawTimeZone = getDateTimeZone(europeWarsawTimeZoneKey); Session europeWarsawSessionWinter = Session.builder(this.session) new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 20, 0, 0, 0, getDateTimeZone(getTimeZoneKey("America/New_York"))).getMillis(), getTimeZoneKey("America/New_York"))); new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 23, 0, 0, 0, getDateTimeZone(getTimeZoneKey("+13:00"))).getMillis(), getTimeZoneKey("+13:00"))); new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 20, 0, 0, 0, getDateTimeZone(getTimeZoneKey("-14:00"))).getMillis(), getTimeZoneKey("-14:00"))); new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 22, 59, 59, 999, getDateTimeZone(getTimeZoneKey("+13:00"))).getMillis(), getTimeZoneKey("+13:00"))); new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 19, 59, 59, 999, getDateTimeZone(getTimeZoneKey("-14:00"))).getMillis(), getTimeZoneKey("-14:00"))); 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)); new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 11, 0, 0, 0, getDateTimeZone(getTimeZoneKey("+02:00"))).getMillis(), getTimeZoneKey("+02:00")));
@Description("current time with time zone") @ScalarFunction @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) public static long currentTime(ConnectorSession session) { // We do all calculation in UTC, as session.getStartTime() is in UTC // and we need to have UTC millis for packDateTimeWithZone long millis = UTC_CHRONOLOGY.millisOfDay().get(session.getStartTime()); if (!session.isLegacyTimestamp()) { // However, those UTC millis are pointing to the correct UTC timestamp // Our TIME WITH TIME ZONE representation does use UTC 1970-01-01 representation // So we have to hack here in order to get valid representation // of TIME WITH TIME ZONE millis -= valueToSessionTimeZoneOffsetDiff(session.getStartTime(), getDateTimeZone(session.getTimeZoneKey())); } return packDateTimeWithZone(millis, session.getTimeZoneKey()); }
@Test public void testExtract() DateTime dateTime = new DateTime(2001, 8, 22, 3, 4, 5, 321, getDateTimeZone(TEST_SESSION.getTimeZoneKey())); double seconds = dateTime.getMillis() / 1000.0;
public static String printTimeWithoutTimeZone(TimeZoneKey timeZoneKey, long value) { return TIME_FORMATTER.withZone(getDateTimeZone(timeZoneKey)).print(value); }
public static long parseTimeWithoutTimeZone(TimeZoneKey timeZoneKey, String value) { return TIME_FORMATTER.withZone(getDateTimeZone(timeZoneKey)).parseMillis(value); }
public static DateTimeZone unpackDateTimeZone(long dateTimeWithTimeZone) { return getDateTimeZone(unpackZoneKey(dateTimeWithTimeZone)); }
@Test public void testFromUnixTimeWithOffset() { DateTime dateTime = new DateTime(2001, 1, 22, 3, 4, 5, 0, DATE_TIME_ZONE); double seconds = dateTime.getMillis() / 1000.0; int timeZoneHoursOffset = 1; int timezoneMinutesOffset = 10; DateTime expected = new DateTime(dateTime, getDateTimeZone(getTimeZoneKeyForOffset(timeZoneHoursOffset * 60 + timezoneMinutesOffset))); assertFunction("from_unixtime(" + seconds + ", " + timeZoneHoursOffset + ", " + timezoneMinutesOffset + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(expected)); }
@Test public void test() { TimeZoneKey.getTimeZoneKey("GMT-13:00"); TreeSet<String> jodaZones = new TreeSet<>(DateTimeZone.getAvailableIDs()); TreeSet<String> jdkZones = new TreeSet<>(Arrays.asList(TimeZone.getAvailableIDs())); for (String zoneId : new TreeSet<>(Sets.intersection(jodaZones, jdkZones))) { if (zoneId.toLowerCase(ENGLISH).startsWith("etc/") || zoneId.toLowerCase(ENGLISH).startsWith("gmt")) { continue; } DateTimeZone dateTimeZone = DateTimeZone.forID(zoneId); DateTimeZone indexedZone = getDateTimeZone(TimeZoneKey.getTimeZoneKey(zoneId)); assertDateTimeZoneEquals(zoneId, indexedZone); assertTimeZone(zoneId, dateTimeZone); } for (int offsetHours = -13; offsetHours < 14; offsetHours++) { for (int offsetMinutes = 0; offsetMinutes < 60; offsetMinutes++) { DateTimeZone dateTimeZone = DateTimeZone.forOffsetHoursMinutes(offsetHours, offsetMinutes); assertTimeZone(dateTimeZone.getID(), dateTimeZone); } } }