@Override int unsafeCompareTo( Value otherValue ) { TimeValue other = (TimeValue) otherValue; int compare = Long.compare( nanosOfDayUTC, other.nanosOfDayUTC ); if ( compare == 0 ) { compare = Integer.compare( value.getOffset().getTotalSeconds(), other.value.getOffset().getTotalSeconds() ); } return compare; }
@Override public void writeTime( OffsetTime offsetTime ) { buf.putLong( TemporalUtil.getNanosOfDayUTC( offsetTime ) ); buf.putInt( offsetTime.getOffset().getTotalSeconds() ); }
/** * Converts java.time.ZoneOffset to its corresponding org.joda.time.DateTimeZone. */ public static org.joda.time.DateTimeZone convertZoneOffsetToJodaDateTimeZone(final ZoneOffset zoneOffset) { return org.joda.time.DateTimeZone.forOffsetMillis(zoneOffset.getTotalSeconds() * 1000); }
private void appendTimeZone(StringBuilder sb, ZoneOffset offset) { int offsetSeconds = offset.getTotalSeconds(); appendTimeZone(sb, offsetSeconds); }
@Override public final void writeTime( OffsetTime offsetTime ) throws E { long nanosOfDayUTC = TemporalUtil.getNanosOfDayUTC( offsetTime ); int offsetSeconds = offsetTime.getOffset().getTotalSeconds(); writeTime( nanosOfDayUTC, offsetSeconds ); }
public static OffsetTime truncateOffsetToMinutes( OffsetTime value ) { int offsetMinutes = value.getOffset().getTotalSeconds() / 60; ZoneOffset truncatedOffset = ZoneOffset.ofTotalSeconds( offsetMinutes * 60 ); return value.withOffsetSameInstant( truncatedOffset ); }
private ZoneOffset randomZoneOffset() { return ZoneOffset.ofTotalSeconds( random.nextInt( ZoneOffset.MIN.getTotalSeconds(), ZoneOffset.MAX.getTotalSeconds() ) ); }
@Override public void writeDateTime( ZonedDateTime zonedDateTime ) { buf.putLong( zonedDateTime.toEpochSecond() ); buf.putInt( zonedDateTime.getNano() ); final ZoneId zone = zonedDateTime.getZone(); if ( zone instanceof ZoneOffset ) { final int offsetSeconds = ((ZoneOffset) zone).getTotalSeconds(); // lowest bit set to 0: it's a zone offset in seconds buf.putInt( offsetSeconds << 1 ); } else { // lowest bit set to 1: it's a zone id final int zoneId = (TimeZones.map( zone.getId() ) << 1) | 1; buf.putInt( zoneId ); } } }
public static long getNanosOfDayUTC( OffsetTime value ) { long secondsOfDayLocal = value.toLocalTime().toSecondOfDay(); long secondsOffset = value.getOffset().getTotalSeconds(); return (secondsOfDayLocal - secondsOffset) * NANOS_PER_SECOND + value.getNano(); } }
@Override public void write( Object value, FlushableChannel into ) throws IOException { OffsetTime ot = (OffsetTime) value; into.putLong( TemporalUtil.getNanosOfDayUTC( ot ) ); into.putInt( ot.getOffset().getTotalSeconds() ); } } );
public static byte[] encodeTimeArray( OffsetTime[] times ) { // We could store this in dateTimes.length * 1.5 if we wanted long[] data = new long[(int) (Math.ceil( times.length * BLOCKS_TIME ))]; int i; for ( i = 0; i < times.length; i++ ) { data[i * BLOCKS_TIME] = times[i].toLocalTime().toNanoOfDay(); data[i * BLOCKS_TIME + 1] = times[i].getOffset().getTotalSeconds(); } TemporalHeader header = new TemporalHeader( TemporalType.TEMPORAL_TIME.temporalType ); byte[] bytes = DynamicArrayStore.encodeFromNumbers( data, DynamicArrayStore.TEMPORAL_HEADER_SIZE ); header.writeArrayHeaderTo( bytes ); return bytes; }
@Override public void writeTime( OffsetTime offsetTime ) throws IOException { long nanosOfDayLocal = offsetTime.toLocalTime().toNanoOfDay(); int offsetSeconds = offsetTime.getOffset().getTotalSeconds(); packStructHeader( TIME_SIZE, TIME ); pack( nanosOfDayLocal ); pack( offsetSeconds ); }
@Override public final void writeDateTime( ZonedDateTime zonedDateTime ) throws E { long epochSecondUTC = zonedDateTime.toEpochSecond(); int nano = zonedDateTime.getNano(); ZoneId zone = zonedDateTime.getZone(); if ( zone instanceof ZoneOffset ) { int offsetSeconds = ((ZoneOffset) zone).getTotalSeconds(); writeDateTime( epochSecondUTC, nano, offsetSeconds ); } else { String zoneId = zone.getId(); writeDateTime( epochSecondUTC, nano, zoneId ); } }
@Test public void shouldPackLocalDateTimeWithTimeZoneOffset() { LocalDateTime localDateTime = LocalDateTime.of( 2015, 3, 23, 19, 15, 59, 10 ); ZoneOffset offset = ZoneOffset.ofHoursMinutes( -5, -15 ); ZonedDateTime zonedDateTime = ZonedDateTime.of( localDateTime, offset ); PackedOutputArray packedOutput = pack( datetime( zonedDateTime ) ); ByteBuffer buffer = ByteBuffer.wrap( packedOutput.bytes() ); buffer.getShort(); // skip struct header assertEquals( INT_32, buffer.get() ); assertEquals( localDateTime.toEpochSecond( UTC ), buffer.getInt() ); assertEquals( localDateTime.getNano(), buffer.get() ); assertEquals( INT_16, buffer.get() ); assertEquals( offset.getTotalSeconds(), buffer.getShort() ); }
private static EventDateTime getEventDateTime(CalendarEventModel.CalendarEventTime dateTime) { if (dateTime == null) { return null; } EventDateTime eventDateTime = new EventDateTime(); // google's APIs want millisecond from epoch, and the timezone offset in minutes. if (dateTime.isDateOnly()) { eventDateTime.setDate(new DateTime(true, dateTime.getDateTime().toEpochSecond() * 1000, dateTime.getDateTime().getOffset().getTotalSeconds() / 60)); } else { eventDateTime.setDateTime(new DateTime( dateTime.getDateTime().toEpochSecond() * 1000, dateTime.getDateTime().getOffset().getTotalSeconds() / 60)); } return eventDateTime; }
@Test void shouldGetNanosOfDayUTC() { LocalTime localTime = LocalTime.of( 14, 19, 18, 123999 ); ZoneOffset offset = ZoneOffset.ofHours( -12 ); OffsetTime time = OffsetTime.of( localTime, offset ); long nanosOfDayUTC = TemporalUtil.getNanosOfDayUTC( time ); long expectedNanosOfDayUTC = Duration.ofSeconds( localTime.toSecondOfDay() ) .minus( offset.getTotalSeconds(), SECONDS ) .toNanos(); assertEquals( expectedNanosOfDayUTC + localTime.getNano(), nanosOfDayUTC ); } }
@Test void shouldReportTooHighOffsetSecondDateTimeRecord() { // given PropertyRecord property = inUse( new PropertyRecord( 42 ) ); final int keyId = 6; add( inUse( new PropertyKeyTokenRecord( keyId ) ) ); final long[] longs = TemporalType.encodeDateTime( keyId, 1,1, ZoneOffset.MAX.getTotalSeconds() + 1 ); expectInvalidPropertyValue( property, longs ); }
@Test void shouldReportTooHighOffsetSecondTimeRecord() { // given PropertyRecord property = inUse( new PropertyRecord( 42 ) ); final int keyId = 6; add( inUse( new PropertyKeyTokenRecord( keyId ) ) ); final long[] longs = TemporalType.encodeTime( keyId, 1, ZoneOffset.MAX.getTotalSeconds() + 1 ); expectInvalidPropertyValue( property, longs ); }
@Override public void write( Object value, FlushableChannel into ) throws IOException { ZonedDateTime zonedDateTime = (ZonedDateTime) value; long epochSecondUTC = zonedDateTime.toEpochSecond(); int nano = zonedDateTime.getNano(); ZoneId zone = zonedDateTime.getZone(); if ( zone instanceof ZoneOffset ) { int offsetSeconds = ((ZoneOffset) zone).getTotalSeconds(); into.put( (byte) 0 ); into.putLong( epochSecondUTC ); into.putInt( nano ); into.putInt( offsetSeconds ); } else { String zoneId = zone.getId(); into.put( (byte) 1 ); into.putLong( epochSecondUTC ); into.putInt( nano ); into.putInt( TimeZones.map( zoneId ) ); } } } );
@Override public void writeDateTime( ZonedDateTime zonedDateTime ) throws IOException { long epochSecondLocal = zonedDateTime.toLocalDateTime().toEpochSecond( UTC ); int nano = zonedDateTime.getNano(); ZoneId zone = zonedDateTime.getZone(); if ( zone instanceof ZoneOffset ) { int offsetSeconds = ((ZoneOffset) zone).getTotalSeconds(); packStructHeader( DATE_TIME_WITH_ZONE_OFFSET_SIZE, DATE_TIME_WITH_ZONE_OFFSET ); pack( epochSecondLocal ); pack( nano ); pack( offsetSeconds ); } else { String zoneId = zone.getId(); packStructHeader( DATE_TIME_WITH_ZONE_NAME_SIZE, DATE_TIME_WITH_ZONE_NAME ); pack( epochSecondLocal ); pack( nano ); pack( zoneId ); } } }