@Override public Value decodeForTemporal( long[] valueBlocks, int offset ) { int secondOffset = (int) (valueBlocks[offset] >>> 32); long nanoOfDay = valueBlocks[1 + offset]; checkValidNanoOfDayWithOffset( nanoOfDay, secondOffset ); return TimeValue.time( nanoOfDay, ZoneOffset.ofTotalSeconds( secondOffset ) ); }
private static OffsetTime readRawTime( ByteBuffer bb, int offset ) { final long nanosOfDayUTC = bb.getLong( offset ); offset += Long.BYTES; final int offsetSeconds = bb.getInt( offset ); return OffsetTime.ofInstant( Instant.ofEpochSecond( 0, nanosOfDayUTC ), ZoneOffset.ofTotalSeconds( offsetSeconds ) ); }
private static ZoneId toZoneId( int z ) { // if lowest bit is set to 1 then it's a shifted zone id if ( (z & 1) != 0 ) { final String zoneId = TimeZones.map( (short) (z >> 1) ); return ZoneId.of( zoneId ); } // otherwise it's a shifted offset seconds value // preserve sign bit for negative offsets return ZoneOffset.ofTotalSeconds( z >> 1 ); }
@Override public String toString(final Object obj) { final Calendar calendar = (Calendar)obj; final Instant instant = Instant.ofEpochMilli(calendar.getTimeInMillis()); final int offsetInMillis = calendar.getTimeZone().getOffset(calendar.getTimeInMillis()); final OffsetDateTime offsetDateTime = OffsetDateTime.ofInstant(instant, ZoneOffset.ofTotalSeconds(offsetInMillis / 1000)); return STD_DATE_TIME.format(offsetDateTime); } }
@Override public Value asValue() { // We need to check validity upfront without throwing exceptions, because the PageCursor might give garbage bytes if ( TimeZones.validZoneOffset( zoneOffsetSeconds ) ) { return TimeValue.time( nanosOfDayUTC, ZoneOffset.ofTotalSeconds( zoneOffsetSeconds ) ); } return NO_VALUE; }
static OffsetTime asValueRaw( long long0, long long1 ) { if ( TimeZones.validZoneOffset( (int) long1 ) ) { return TimeValue.timeRaw( long0, ZoneOffset.ofTotalSeconds( (int) long1 ) ); } // TODO Getting here means that after a proper read this value is plain wrong... shouldn't something be thrown instead? Yes and same for TimeZones return null; }
@Override public Object read( ReadableClosableChannel from ) throws IOException { return OffsetTime.ofInstant( Instant.ofEpochSecond( 0, from.getLong() ), ZoneOffset.ofTotalSeconds( from.getInt() ) ); }
@Override public ZonedDateTime convert(String source) { ZonedDateTime dateTime; try { long epoch = Long.parseLong(source); dateTime = Instant.ofEpochMilli(epoch).atOffset(ZoneOffset.ofTotalSeconds(0)) .toZonedDateTime(); } catch (NumberFormatException e) { // try ZonedDateTime instead dateTime = ZonedDateTime.parse(source); } return dateTime; } }
static ZonedDateTime asValueRaw( long long0, long long1, long long2, long long3 ) { return TimeZones.validZoneId( (short) long2 ) ? DateTimeValue.datetimeRaw( long0, long1, ZoneId.of( TimeZones.map( (short) long2 ) ) ) : DateTimeValue.datetimeRaw( long0, long1, ZoneOffset.ofTotalSeconds( (int) long3 ) ); }
public static OffsetTime truncateOffsetToMinutes( OffsetTime value ) { int offsetMinutes = value.getOffset().getTotalSeconds() / 60; ZoneOffset truncatedOffset = ZoneOffset.ofTotalSeconds( offsetMinutes * 60 ); return value.withOffsetSameInstant( truncatedOffset ); }
@Override public Value asValue() { return TimeZones.validZoneId( zoneId ) ? DateTimeValue.datetime( epochSecondUTC, nanoOfSecond, ZoneId.of( TimeZones.map( zoneId ) ) ) : DateTimeValue.datetime( epochSecondUTC, nanoOfSecond, ZoneOffset.ofTotalSeconds( zoneOffsetSeconds ) ); }
@Override public Object read( ReadableClosableChannel from ) throws IOException { if ( from.get() == (byte) 0 ) { long epochSecondsUTC = from.getLong(); int nanos = from.getInt(); int offsetSeconds = from.getInt(); return ZonedDateTime.ofInstant( Instant.ofEpochSecond( epochSecondsUTC, nanos ), ZoneOffset.ofTotalSeconds( offsetSeconds ) ); } else { long epochSecondsUTC = from.getLong(); int nanos = from.getInt(); int zoneID = from.getInt(); String zone = TimeZones.map( (short) zoneID ); return ZonedDateTime.ofInstant( Instant.ofEpochSecond( epochSecondsUTC, nanos ), ZoneId.of( zone ) ); } }
dateTimes[i] = ZonedDateTime.ofInstant( Instant.ofEpochSecond( epochSecond, nanoOfSecond ), ZoneOffset.ofTotalSeconds( secondOffset ) );
private TimeValue unpackTime() throws IOException { long nanosOfDayLocal = unpackLong(); int offsetSeconds = unpackInteger(); return time( TemporalUtil.nanosOfDayToUTC( nanosOfDayLocal, offsetSeconds ), ZoneOffset.ofTotalSeconds( offsetSeconds ) ); }
@Override public Value decodeForTemporal( long[] valueBlocks, int offset ) { if ( storingZoneOffset( valueBlocks[offset] ) ) { int nanoOfSecond = (int) (valueBlocks[offset] >>> 33); checkValidNanoOfSecond( nanoOfSecond ); long epochSecond = valueBlocks[1 + offset]; int secondOffset = (int) valueBlocks[2 + offset]; return DateTimeValue.datetime( epochSecond, nanoOfSecond, ZoneOffset.ofTotalSeconds( secondOffset ) ); } else { int nanoOfSecond = (int) (valueBlocks[offset] >>> 33); checkValidNanoOfSecond( nanoOfSecond ); long epochSecond = valueBlocks[1 + offset]; short zoneNumber = (short) valueBlocks[2 + offset]; return DateTimeValue.datetime( epochSecond, nanoOfSecond, ZoneId.of( TimeZones.map( zoneNumber ) ) ); } }
protected Object convertTimestampWithZone(Column column, Field fieldDefn, Object data) { if (!(data instanceof DateTimeOffset)) { return super.convertTimestampWithZone(column, fieldDefn, data); } final DateTimeOffset dto = (DateTimeOffset)data; // Timestamp is provided in UTC time final Timestamp utc = dto.getTimestamp(); final ZoneOffset offset = ZoneOffset.ofTotalSeconds(dto.getMinutesOffset() * 60); return super.convertTimestampWithZone(column, fieldDefn, LocalDateTime.ofEpochSecond(utc.getTime() / 1000, utc.getNanos(), offset).atOffset(offset)); }
private DateTimeValue unpackDateTimeWithZoneOffset() throws IOException { long epochSecondLocal = unpackLong(); long nano = unpackLong(); int offsetSeconds = unpackInteger(); return datetime( newZonedDateTime( epochSecondLocal, nano, ZoneOffset.ofTotalSeconds( offsetSeconds ) ) ); }
@Override public ArrayValue decodeArray( Value dataValue ) { if ( dataValue instanceof LongArray ) { LongArray numbers = (LongArray) dataValue; OffsetTime[] times = new OffsetTime[(int) (numbers.length() / BLOCKS_TIME)]; for ( int i = 0; i < times.length; i++ ) { long nanoOfDay = numbers.longValue( i * BLOCKS_TIME ); int secondOffset = (int) numbers.longValue( i * BLOCKS_TIME + 1 ); checkValidNanoOfDay( nanoOfDay ); times[i] = OffsetTime.of( LocalTime.ofNanoOfDay( nanoOfDay ), ZoneOffset.ofTotalSeconds( secondOffset ) ); } return Values.timeArray( times ); } else { throw new InvalidRecordException( "Array with unexpected type. Actual:" + dataValue.getClass().getSimpleName() + ". Expected: LongArray." ); } } },
private ZoneOffset randomZoneOffset() { return ZoneOffset.ofTotalSeconds( random.nextInt( ZoneOffset.MIN.getTotalSeconds(), ZoneOffset.MAX.getTotalSeconds() ) ); }
/** * Best effort parsing of the given value */ public static LocalDateTime parseBest(String value) { TemporalAccessor parsed = getDateTimeFormatter().parse(value); LocalDate datePart; LocalTime timePart; ZoneOffset zoneOffset; long epochDay = 0, nanoSeconds = 0; int offsetSeconds = 0; // get different parsed parts if (parsed.isSupported(ChronoField.EPOCH_DAY)) { epochDay = parsed.getLong(ChronoField.EPOCH_DAY); } if (parsed.isSupported(ChronoField.NANO_OF_DAY)) { nanoSeconds = parsed.getLong(ChronoField.NANO_OF_DAY); } if (parsed.isSupported(ChronoField.OFFSET_SECONDS)) { offsetSeconds = parsed.get(ChronoField.OFFSET_SECONDS); } zoneOffset = ZoneOffset.ofTotalSeconds(offsetSeconds); datePart = LocalDate.ofEpochDay(epochDay); timePart = LocalTime.ofNanoOfDay(nanoSeconds); return OffsetDateTime.of(datePart, timePart, zoneOffset).toLocalDateTime(); }