private int getOffsetToAdd(long instant) { int offset = this.iZone.getOffset(instant); long sum = instant + offset; // If there is a sign change, but the two values have the same sign... if ((instant ^ sum) < 0 && (instant ^ offset) >= 0) { throw new ArithmeticException("Adding time zone offset caused overflow"); } return offset; }
private int getOffsetToAdd(long instant) { int offset = this.iZone.getOffset(instant); long sum = instant + offset; // If there is a sign change, but the two values have the same sign... if ((instant ^ sum) < 0 && (instant ^ offset) >= 0) { throw new ArithmeticException("Adding time zone offset caused overflow"); } return offset; }
private int getOffsetToAdd(long instant) { int offset = this.iZone.getOffset(instant); long sum = instant + offset; // If there is a sign change, but the two values have the same sign... if ((instant ^ sum) < 0 && (instant ^ offset) >= 0) { throw new ArithmeticException("Adding time zone offset caused overflow"); } return offset; }
private int getOffsetToAdd(long instant) { int offset = this.iZone.getOffset(instant); long sum = instant + offset; // If there is a sign change, but the two values have the same sign... if ((instant ^ sum) < 0 && (instant ^ offset) >= 0) { throw new ArithmeticException("Adding time zone offset caused overflow"); } return offset; }
/** * Gets the millisecond offset to add to UTC to get local time. * * @param instant instant to get the offset for, null means now * @return the millisecond offset to add to UTC to get local time */ public final int getOffset(ReadableInstant instant) { if (instant == null) { return getOffset(DateTimeUtils.currentTimeMillis()); } return getOffset(instant.getMillis()); }
public int getOffset(long millis) { if (iNextInfo == null || millis < iNextInfo.iPeriodStart) { if (iOffset == Integer.MIN_VALUE) { iOffset = iZoneRef.getOffset(iPeriodStart); } return iOffset; } return iNextInfo.getOffset(millis); }
/** * Gets the millisecond offset to add to UTC to get local time. * * @param instant instant to get the offset for, null means now * @return the millisecond offset to add to UTC to get local time */ public final int getOffset(ReadableInstant instant) { if (instant == null) { return getOffset(DateTimeUtils.currentTimeMillis()); } return getOffset(instant.getMillis()); }
public int getOffset(long millis) { if (iNextInfo == null || millis < iNextInfo.iPeriodStart) { if (iOffset == Integer.MIN_VALUE) { iOffset = iZoneRef.getOffset(iPeriodStart); } return iOffset; } return iNextInfo.getOffset(millis); }
@Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Result result = (Result) o; if (timestamp != null ? !(timestamp.isEqual(result.timestamp) && timestamp.getZone().getOffset(timestamp) == result.timestamp.getZone().getOffset(result.timestamp)) : result.timestamp != null) { return false; } if (value != null ? !value.equals(result.value) : result.value != null) { return false; } return true; }
public static int modulo24Hour(ISOChronology chronology, long millis) { return chronology.millisOfDay().get(millis) - chronology.getZone().getOffset(millis); }
public long getDateTimeMillis(long instant, int hourOfDay, int minuteOfHour, int secondOfMinute, int millisOfSecond) throws IllegalArgumentException { return localToUTC(getBase().getDateTimeMillis (instant + getZone().getOffset(instant), hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond)); }
@ScalarOperator(CAST) @SqlType(StandardTypes.TIMESTAMP) public static long castToTimestamp(ConnectorSession session, @SqlType(StandardTypes.DATE) long value) { if (session.isLegacyTimestamp()) { 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()); return utcMillis - chronology.getZone().getOffset(utcMillis); } else { return TimeUnit.DAYS.toMillis(value); } }
public long getDateTimeMillis(long instant, int hourOfDay, int minuteOfHour, int secondOfMinute, int millisOfSecond) throws IllegalArgumentException { return localToUTC(getBase().getDateTimeMillis (instant + getZone().getOffset(instant), hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond)); }
@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()); }
private void printTo(Appendable appendable, long instant, Chronology chrono) throws IOException { InternalPrinter printer = requirePrinter(); chrono = selectChronology(chrono); // Shift instant into local time (UTC) to avoid excessive offset // calculations when printing multiple fields in a composite printer. DateTimeZone zone = chrono.getZone(); int offset = zone.getOffset(instant); long adjustedInstant = instant + offset; if ((instant ^ adjustedInstant) < 0 && (instant ^ offset) >= 0) { // Time zone offset overflow, so revert to UTC. zone = DateTimeZone.UTC; offset = 0; adjustedInstant = instant; } printer.printTo(appendable, adjustedInstant, chrono.withUTC(), offset, zone, iLocale); }
@ScalarFunction("date") @ScalarOperator(CAST) @SqlType(StandardTypes.DATE) public static long castToDate(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long value) { // round down the current timestamp to days ISOChronology chronology = unpackChronology(value); long date = chronology.dayOfYear().roundFloor(unpackMillisUtc(value)); // date is currently midnight in timezone of the original value // convert to UTC long millis = date + chronology.getZone().getOffset(date); return TimeUnit.MILLISECONDS.toDays(millis); }
@ScalarOperator(CAST) @SqlType(StandardTypes.TIMESTAMP) public static long castToTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long value) { if (session.isLegacyTimestamp()) { return unpackMillisUtc(value); } else { // This is hack that we need to use as the timezone interpretation depends on date (not only on time) // TODO remove REFERENCE_TIMESTAMP_UTC when removing support for political time zones in TIME WIT TIME ZONE long currentMillisOfDay = ChronoField.MILLI_OF_DAY.getFrom(Instant.ofEpochMilli(REFERENCE_TIMESTAMP_UTC).atZone(ZoneOffset.UTC)); long timeMillisUtcInCurrentDay = REFERENCE_TIMESTAMP_UTC - currentMillisOfDay + unpackMillisUtc(value); ISOChronology chronology = getChronology(unpackZoneKey(value)); return unpackMillisUtc(value) + chronology.getZone().getOffset(timeMillisUtcInCurrentDay); } }
@ScalarFunction("date") @ScalarOperator(CAST) @SqlType(StandardTypes.DATE) public static long castToDate(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long value) { ISOChronology chronology; if (session.isLegacyTimestamp()) { // round down the current timestamp to days chronology = getChronology(session.getTimeZoneKey()); long date = chronology.dayOfYear().roundFloor(value); // date is currently midnight in timezone of the session // convert to UTC long millis = date + chronology.getZone().getOffset(date); return TimeUnit.MILLISECONDS.toDays(millis); } else { return TimeUnit.MILLISECONDS.toDays(value); } }
@ScalarOperator(CAST) @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) public static long castToTimeWithTimeZone(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long value) { if (session.isLegacyTimestamp()) { int millis = modulo24Hour(unpackChronology(value), unpackMillisUtc(value)); return packDateTimeWithZone(millis, unpackZoneKey(value)); } else { long millis = modulo24Hour(castToTimestamp(session, value)); ISOChronology localChronology = unpackChronology(value); // This cast does treat TIME as wall time in given TZ. This means that in order to get // its UTC representation we need to shift the value by the offset of TZ. // We use value offset in this place to be sure that we will have same hour represented // in TIME WITH TIME ZONE. Calculating real TZ offset will happen when really required. // This is done due to inadequate TIME WITH TIME ZONE representation. return packDateTimeWithZone(millis - localChronology.getZone().getOffset(millis), unpackZoneKey(value)); } }