Skip to content

Commit

Permalink
Fix ZonedDateTime index issue with negative offsets, offset now in mi…
Browse files Browse the repository at this point in the history
…nutes
  • Loading branch information
fickludd authored and Lojjs committed Mar 16, 2018
1 parent 4a34056 commit 903afd0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
Expand Up @@ -47,7 +47,7 @@ public ZonedDateTimeSchemaKey copyKey( ZonedDateTimeSchemaKey key, ZonedDateTime
into.epochSecondUTC = key.epochSecondUTC;
into.nanoOfSecond = key.nanoOfSecond;
into.zoneId = key.zoneId;
into.zoneOffsetSeconds = key.zoneOffsetSeconds;
into.zoneOffsetMinutes = key.zoneOffsetMinutes;
into.setEntityId( key.getEntityId() );
into.setCompareId( key.getCompareId() );
return into;
Expand All @@ -70,7 +70,7 @@ public void writeKey( PageCursor cursor, ZonedDateTimeSchemaKey key )
}
else
{
cursor.putInt( key.zoneOffsetSeconds );
cursor.putInt( key.zoneOffsetMinutes & ZONE_ID_MASK );
}
cursor.putLong( key.getEntityId() );
}
Expand All @@ -84,19 +84,19 @@ public void readKey( PageCursor cursor, ZonedDateTimeSchemaKey into, int keySize
if ( isZoneId( encodedZone ) )
{
into.zoneId = asZoneId( encodedZone );
into.zoneOffsetSeconds = 0;
into.zoneOffsetMinutes = 0;
}
else
{
into.zoneId = -1;
into.zoneOffsetSeconds = asZoneOffset( encodedZone );
into.zoneOffsetMinutes = asZoneOffset( encodedZone );
}
into.setEntityId( cursor.getLong() );
}

private int asZoneOffset( int encodedZone )
{
return encodedZone;
return (short) encodedZone;
}

private short asZoneId( int encodedZone )
Expand Down
Expand Up @@ -44,17 +44,17 @@ class ZonedDateTimeSchemaKey extends NativeSchemaKey<ZonedDateTimeSchemaKey>
Integer.BYTES + /* timeZone */
Long.BYTES; /* entityId */

int nanoOfSecond;
long epochSecondUTC;
int nanoOfSecond;
short zoneId;
int zoneOffsetSeconds;
int zoneOffsetMinutes;

@Override
public Value asValue()
{
return TimeZones.validZoneId( zoneId ) ?
DateTimeValue.datetime( epochSecondUTC, nanoOfSecond, ZoneId.of( TimeZones.map( zoneId ) ) ) :
DateTimeValue.datetime( epochSecondUTC, nanoOfSecond, ZoneOffset.ofTotalSeconds( zoneOffsetSeconds ) );
DateTimeValue.datetime( epochSecondUTC, nanoOfSecond, ZoneOffset.ofTotalSeconds( zoneOffsetMinutes * 60 ) );
}

@Override
Expand All @@ -63,7 +63,7 @@ public void initValueAsLowest()
epochSecondUTC = Long.MIN_VALUE;
nanoOfSecond = Integer.MIN_VALUE;
zoneId = Short.MIN_VALUE;
zoneOffsetSeconds = Integer.MIN_VALUE;
zoneOffsetMinutes = Integer.MIN_VALUE;
}

@Override
Expand All @@ -72,7 +72,7 @@ public void initValueAsHighest()
epochSecondUTC = Long.MAX_VALUE;
nanoOfSecond = Integer.MAX_VALUE;
zoneId = Short.MAX_VALUE;
zoneOffsetSeconds = Integer.MAX_VALUE;
zoneOffsetMinutes = Integer.MAX_VALUE;
}

@Override
Expand All @@ -96,15 +96,15 @@ public int compareValueTo( ZonedDateTimeSchemaKey other )
public String toString()
{
return format( "value=%s,entityId=%d,epochSecond=%d,nanoOfSecond=%d,zoneId=%d,zoneOffset=%d",
asValue(), getEntityId(), epochSecondUTC, nanoOfSecond, zoneId, zoneOffsetSeconds );
asValue(), getEntityId(), epochSecondUTC, nanoOfSecond, zoneId, zoneOffsetMinutes * 60 );
}

@Override
public void writeDateTime( long epochSecondUTC, int nano, int offsetSeconds )
{
this.epochSecondUTC = epochSecondUTC;
this.nanoOfSecond = nano;
this.zoneOffsetSeconds = offsetSeconds;
this.zoneOffsetMinutes = offsetSeconds / 60;
this.zoneId = -1;
}

Expand All @@ -114,7 +114,7 @@ public void writeDateTime( long epochSecondUTC, int nano, String zoneId )
this.epochSecondUTC = epochSecondUTC;
this.nanoOfSecond = nano;
this.zoneId = TimeZones.map( zoneId );
this.zoneOffsetSeconds = 0;
this.zoneOffsetMinutes = 0;
}

@Override
Expand All @@ -131,8 +131,8 @@ protected Value assertCorrectType( Value value )
// We need to check validity upfront without throwing exceptions, because the PageCursor might give garbage bytes
private boolean differentValidZoneOffset( ZonedDateTimeSchemaKey other )
{
return zoneOffsetSeconds != other.zoneOffsetSeconds &&
TimeZones.validZoneOffset( zoneOffsetSeconds ) && TimeZones.validZoneOffset( other.zoneOffsetSeconds );
return zoneOffsetMinutes != other.zoneOffsetMinutes &&
TimeZones.validZoneOffset( zoneOffsetMinutes * 60 ) && TimeZones.validZoneOffset( other.zoneOffsetMinutes * 60 );
}

// We need to check validity upfront without throwing exceptions, because the PageCursor might give garbage bytes
Expand Down
Expand Up @@ -116,6 +116,7 @@ public Compatibility( IndexProviderCompatibilityTestSuite testSuite, SchemaIndex
DateTimeValue.datetime( 2014, 3, 25, 12, 45, 14, 7474, "+05:00" ),
DateTimeValue.datetime( 2014, 3, 25, 12, 45, 14, 7475, "+05:00" ),
DateTimeValue.datetime( 2001, 1, 25, 11, 11, 30, 0, "Canada/East-Saskatchewan" ),
DateTimeValue.datetime( 2038, 1, 18, 9, 14, 7, 0, "-18:00" ),
DateTimeValue.datetime( 10000, 100, ZoneOffset.ofTotalSeconds( 3 ) ),
DateTimeValue.datetime( 10000, 101, ZoneOffset.ofTotalSeconds( -3 ) ),
DurationValue.duration( 10, 20, 30, 40 ),
Expand Down

0 comments on commit 903afd0

Please sign in to comment.