return ( (Time) value ).toLocalTime().atOffset( OffsetDateTime.now().getOffset() );
return timeWithUtcZoneFormat.parse(((String) value), LocalTime::from).atOffset(ZoneOffset.UTC);
@Override public void writeTime(TemporalAccessor value) throws IOException { gen.writeStartObject(); gen.writeFieldName(ExtendedType.TIME.serialized); super.writeTime(((LocalTime) value).atOffset(ZoneOffset.UTC)); // output time in local time zone gen.writeEndObject(); }
@Override public OffsetTime getOffsetTime() { return get().atOffset(ZoneOffset.UTC); } }
@Override public OffsetTime convert(Time in, Context context) throws Exception { if (in == null) return null; return in.toLocalTime().atOffset(offset); } }
@Override public OffsetTime convert(Time in, Context context) throws Exception { if (in == null) return null; return in.toLocalTime().atOffset(offset); } }
@Override public String generate(LocalTime value) { return value.atOffset(ZoneOffset.UTC).toString(); }
@Override protected Object decodeValue(Context context, Type type, Short typeLength, Integer typeModifier, ByteBuf buffer, Class<?> targetClass, Object targetContext) throws IOException { Calendar calendar = targetContext != null ? (Calendar) targetContext : Calendar.getInstance(); long micros = buffer.readLong(); int tzOffsetSecs = -buffer.readInt(); if (micros == Long.MAX_VALUE || micros == Long.MIN_VALUE) { return convertInfinityOutput(micros == Long.MAX_VALUE, type, targetClass); } ZoneOffset offset = ZoneOffset.ofTotalSeconds(tzOffsetSecs); OffsetTime time = LocalTime.ofNanoOfDay(MICROSECONDS.toNanos(micros)).atOffset(offset); return convertOutput(context, type, time, targetClass, calendar); }
/** * Get the column value as OffsetTime. * @param rs ResultSet * @param index column index * @param zoneId zoneId * @return column value * @throws SQLException in case of extraction failure */ protected static OffsetTime getAsOffsetTime(ResultSet rs, int index, ZoneId zoneId) throws SQLException { Time time = rs.getTime(index); if (time != null) { return time.toLocalTime().atOffset(zoneId.getRules().getOffset(Instant.now())); } return null; } }
@Override protected Object decodeValue(Context context, Type type, Short typeLength, Integer typeModifier, CharSequence buffer, Class<?> targetClass, Object targetContext) throws IOException, ParseException { Calendar calendar = targetContext != null ? (Calendar) targetContext : Calendar.getInstance(); if (buffer.equals(POS_INFINITY) || buffer.equals(NEG_INFINITY)) { return convertInfinityOutput(buffer.equals(POS_INFINITY), type, targetClass); } TemporalAccessor parsed = context.getTimeFormat().getParser().parse(buffer); OffsetTime time; if (parsed.isSupported(ChronoField.OFFSET_SECONDS)) { time = OffsetTime.from(parsed); } else { ZoneOffset offset = ZoneOffset.ofTotalSeconds((int) MILLISECONDS.toSeconds(calendar.getTimeZone().getRawOffset())); time = LocalTime.from(parsed).atOffset(offset); } return convertOutput(context, type, time, targetClass, calendar); }
private static OffsetTime getOffsetTime(Time time) { if (time != null) { return time.toLocalTime().atOffset(OffsetTime.now().getOffset()); } return null; } }
public static OffsetTime makeOffsetTime(String literal) { literal = DateTimeUtil.fixDateTimeFormat(literal); if (!DateTimeUtil.isTime(literal)) { return null; } if (DateTimeUtil.hasZone(literal) && DateTimeUtil.timeHasOffset(literal)) { return null; } if (DateTimeUtil.hasZone(literal)) { if (literal.contains("@")) { int zoneIndex = literal.indexOf("@"); String zoneId = literal.substring(literal.indexOf('@') + 1); ZoneId zone = ZoneId.of(zoneId); LocalTime localTime = LocalTime.parse(literal.substring(0, zoneIndex), FEEL_TIME_FORMAT); ZonedDateTime zdt = ZonedDateTime.of(LocalDate.now(zone), localTime, zone); ZoneOffset offset = zone.getRules().getStandardOffset(zdt.toInstant()); return localTime.atOffset(offset); } else { return OffsetTime.parse(literal); } } else if (DateTimeUtil.hasOffset(literal)) { return OffsetTime.parse(literal); } else { return OffsetTime.parse(literal + "Z"); } }
public static OffsetTime makeOffsetTime(String literal) { literal = DateTimeUtil.fixDateTimeFormat(literal); if (!DateTimeUtil.isTime(literal)) { return null; } if (DateTimeUtil.hasZone(literal) && DateTimeUtil.timeHasOffset(literal)) { return null; } if (DateTimeUtil.hasZone(literal)) { if (literal.contains("@")) { int zoneIndex = literal.indexOf("@"); String zoneId = literal.substring(literal.indexOf('@') + 1); ZoneId zone = ZoneId.of(zoneId); LocalTime localTime = LocalTime.parse(literal.substring(0, zoneIndex), FEEL_TIME_FORMAT); ZonedDateTime zdt = ZonedDateTime.of(LocalDate.now(zone), localTime, zone); ZoneOffset offset = zone.getRules().getStandardOffset(zdt.toInstant()); return localTime.atOffset(offset); } else { return OffsetTime.parse(literal); } } else if (DateTimeUtil.hasOffset(literal)) { return OffsetTime.parse(literal); } else { return OffsetTime.parse(literal + "Z"); } }
@Override public final OffsetTime decodeBinary(final BufferReader b) { final LocalTime time = LocalTime.ofNanoOfDay(b.readLong() * 1000); final ZoneOffset zone = ZoneOffset.ofTotalSeconds(-b.readInt()); return time.atOffset(zone); }
private static Object convertOutput(Context context, Type type, LocalTime time, Class<?> targetClass, Calendar targetCalendar) throws ConversionException { if (targetClass == LocalTime.class) { return time; } if (targetClass == OffsetTime.class) { ZoneOffset offset = ZoneOffset.ofTotalSeconds((int) MILLISECONDS.toSeconds(targetCalendar.getTimeZone().getRawOffset())); return time.atOffset(offset); } if (targetClass == String.class) { return context.getTimeFormat().getPrinter().format(time); } if (targetClass == Time.class) { LocalDate date = LocalDate.of(1970, 1, 1); OffsetDateTime dateTime = date.atTime(time).atZone(targetCalendar.getTimeZone().toZoneId()).toOffsetDateTime().withOffsetSameInstant(ZoneOffset.UTC); return new Time(dateTime.toInstant().toEpochMilli()); } if (targetClass == Timestamp.class) { LocalDate date = LocalDate.of(1970, 1, 1); ZonedDateTime dateTime = date.atTime(time).atZone(targetCalendar.getTimeZone().toZoneId()); return Timestamp.from(dateTime.toInstant()); } throw new ConversionException(type, targetClass); }
@Test public void defaultBinderBindsOffsetTime() throws SQLException { OffsetTime offsetTime = LocalTime.parse("01:02:03.000").atOffset(ZoneOffset.UTC); Binder binder = Binders.defaultBinder(); binder.bind(stmt, 1, offsetTime); verify(stmt).setTime(eq(1), eq(Time.valueOf(offsetTime.toLocalTime())), calendarWithTimeZone(TimeZone.getTimeZone(ZoneOffset.UTC))); }
private OffsetDateTime normalize(final Temporal temporal) { OffsetDateTime dateTime = asOffsetDateTime(temporal); if (this.startDate != null) { return TemporalConverter.applyByType(this.startDate, t -> dateTime.with(LocalTime.MIDNIGHT.atOffset(ZoneOffset.UTC)), t -> dateTime.with(t.toOffsetTime())); } return dateTime; }
@Test public void testConvertTime() throws Exception { Time time = new Time(System.currentTimeMillis()); OffsetTime offsetTime = converter.convert(time, null); assertEquals(time.toLocalTime().atOffset(offset), offsetTime); }
@Override public OffsetTime convert(Object o, Context context) throws Exception { if (o == null) { return null; } if (o instanceof Date) { final Instant instant = Instant.ofEpochMilli(((Date) o).getTime()); return instant.atOffset(zone.getRules().getOffset(instant)).toOffsetTime(); } if (o instanceof Instant) { final Instant instant = (Instant) o; return instant.atOffset(zone.getRules().getOffset(instant)).toOffsetTime(); } if (o instanceof OffsetTime) { return (OffsetTime) o; } if (o instanceof LocalDateTime) { return ((LocalDateTime)o).atZone(zone).toOffsetDateTime().toOffsetTime(); } if (o instanceof LocalTime) { return ((LocalTime)o).atOffset(zone.getRules().getOffset(Instant.now())); } if (o instanceof TemporalAccessor) { return OffsetTime.from((TemporalAccessor)o); } throw new IllegalArgumentException("Cannot convert " + o + " to OffsetTime"); }
@Override public OffsetTime convert(Object o, Context context) throws Exception { if (o == null) { return null; } if (o instanceof Date) { final Instant instant = Instant.ofEpochMilli(((Date) o).getTime()); return instant.atOffset(zone.getRules().getOffset(instant)).toOffsetTime(); } if (o instanceof Instant) { final Instant instant = (Instant) o; return instant.atOffset(zone.getRules().getOffset(instant)).toOffsetTime(); } if (o instanceof OffsetTime) { return (OffsetTime) o; } if (o instanceof LocalDateTime) { return ((LocalDateTime)o).atZone(zone).toOffsetDateTime().toOffsetTime(); } if (o instanceof LocalTime) { return ((LocalTime)o).atOffset(zone.getRules().getOffset(Instant.now())); } if (o instanceof TemporalAccessor) { return OffsetTime.from((TemporalAccessor)o); } throw new IllegalArgumentException("Cannot convert " + o + " to OffsetTime"); }