Skip to content

Commit

Permalink
Fixed case when you select time but not timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
Lojjs committed Feb 27, 2018
1 parent 9ef6d2f commit f2a5c87
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 55 deletions.
Expand Up @@ -169,6 +169,7 @@ public DateTimeValue buildInternal()
boolean selectingDate = fields.containsKey( Field.date );
boolean selectingTime = fields.containsKey( Field.time );
boolean selectingDateTime = fields.containsKey( Field.datetime );
boolean selectingTimeZone;
ZonedDateTime result;
if ( selectingDateTime )
{
Expand All @@ -178,12 +179,16 @@ public DateTimeValue buildInternal()
throw new IllegalArgumentException( String.format( "Cannot construct date time from: %s", dtField ) );
}
TemporalValue dt = (TemporalValue) dtField;
OffsetTime timePart = dt.getTimePart( defaultZone );
result = ZonedDateTime.of( dt.getDatePart(), timePart.toLocalTime(), timePart.getOffset() );
LocalTime timePart = dt.getTimePart( defaultZone ).toLocalTime();
ZoneId zoneId = dt.getZoneId( defaultZone );
result = ZonedDateTime.of( dt.getDatePart(), timePart, zoneId );
selectingTimeZone = dt.hasTimeZone();
}
else if ( selectingTime || selectingDate )
{
OffsetTime time;

LocalTime time;
ZoneId zoneId;
if ( selectingTime )
{
AnyValue timeField = fields.get( Field.time );
Expand All @@ -192,11 +197,15 @@ else if ( selectingTime || selectingDate )
throw new IllegalArgumentException( String.format( "Cannot construct local time from: %s", timeField ) );
}
TemporalValue t = (TemporalValue) timeField;
time = t.getTimePart( defaultZone );
time = t.getTimePart( defaultZone ).toLocalTime();
zoneId = t.getZoneId( defaultZone );
selectingTimeZone = t.hasTimeZone();
}
else
{
time = TimeValue.defaultTime( timezone() );
time = LocalTimeValue.DEFAULT_LOCAL_TIME;
zoneId = timezone();
selectingTimeZone = false;
}
LocalDate date;
if ( selectingDate )
Expand All @@ -213,11 +222,12 @@ else if ( selectingTime || selectingDate )
{
date = DateValue.DEFAULT_CALENDER_DATE;
}
result = ZonedDateTime.of( date, time.toLocalTime(), time.getOffset() );
result = ZonedDateTime.of( date, time, zoneId );
}
else
{
result = defaulZonedDateTime;
selectingTimeZone = false;
}

if ( fields.containsKey( Field.week ) && !selectingDate && !selectingDateTime )
Expand All @@ -233,7 +243,7 @@ else if ( selectingTime || selectingDate )
result = assignAllFields( result );
if ( timezone != null )
{
if ( selectingTime || selectingDateTime )
if ( (selectingTime || selectingDateTime) && selectingTimeZone )
{
result = result.withZoneSameInstant( timezone() );
}
Expand Down Expand Up @@ -298,6 +308,18 @@ OffsetTime getTimePart( Supplier<ZoneId> defaultZone )
return OffsetTime.of( localTime, offset );
}

@Override
ZoneId getZoneId( Supplier<ZoneId> defaultZone )
{
return value.getZone();
}

@Override
public boolean hasTimeZone()
{
return true;
}

@Override
public boolean equals( Value other )
{
Expand Down
Expand Up @@ -157,6 +157,18 @@ OffsetTime getTimePart( Supplier<ZoneId> defaultZone )
throw new IllegalArgumentException( String.format( "Cannot get the time of: %s", this ) );
}

@Override
ZoneId getZoneId( Supplier<ZoneId> defaultZone )
{
throw new IllegalArgumentException( String.format( "Cannot get the time zone of: %s", this ) );
}

@Override
public boolean hasTimeZone()
{
return false;
}

@Override
public boolean equals( Value other )
{
Expand Down
Expand Up @@ -261,6 +261,18 @@ OffsetTime getTimePart( Supplier<ZoneId> defaultZone )
return OffsetTime.of(value.toLocalTime(), currentOffset);
}

@Override
ZoneId getZoneId( Supplier<ZoneId> defaultZone )
{
return defaultZone.get();
}

@Override
public boolean hasTimeZone()
{
return false;
}

@Override
public boolean equals( Value other )
{
Expand Down
Expand Up @@ -188,6 +188,18 @@ OffsetTime getTimePart( Supplier<ZoneId> defaultZone )
return OffsetTime.of( value, currentOffset );
}

@Override
ZoneId getZoneId( Supplier<ZoneId> defaultZone )
{
return defaultZone.get();
}

@Override
public boolean hasTimeZone()
{
return false;
}

@Override
public boolean equals( Value other )
{
Expand Down
Expand Up @@ -74,6 +74,10 @@ public abstract class TemporalValue<T extends Temporal, V extends TemporalValue<

abstract OffsetTime getTimePart( Supplier<ZoneId> defaultZone );

abstract ZoneId getZoneId( Supplier<ZoneId> defaultZone );

abstract boolean hasTimeZone();

abstract V replacement( T temporal );

@Override
Expand Down
Expand Up @@ -123,27 +123,31 @@ static TimeBuilder<TimeValue> builder( Supplier<ZoneId> defaultZone )
@Override
public TimeValue buildInternal()
{
boolean selecting = fields.containsKey( Field.time );
boolean selectingTime = fields.containsKey( Field.time );
boolean selectingTimeZone;
OffsetTime result;
if ( selecting )
if ( selectingTime )
{
AnyValue time = fields.get( Field.time );
if ( !(time instanceof TemporalValue) )
{
throw new IllegalArgumentException( String.format( "Cannot construct time from: %s", time ) );
}
result = ((TemporalValue) time).getTimePart( defaultZone );
TemporalValue t = (TemporalValue) time;
result = t.getTimePart( defaultZone );
selectingTimeZone = t.hasTimeZone();
}
else
{
result = defaultTime( timezone() );
selectingTimeZone = false;
}

result = assignAllFields( result );
if ( timezone != null )
{
ZoneOffset currentOffset = ZonedDateTime.ofInstant( Instant.now(), timezone() ).getOffset();
if ( selecting )
if ( selectingTime && selectingTimeZone )
{
result = result.withOffsetSameInstant( currentOffset );
}
Expand All @@ -169,7 +173,7 @@ protected TimeValue selectTime(
}

TemporalValue v = (TemporalValue) temporal;
OffsetTime time = v.getTimePart(defaultZone);
OffsetTime time = v.getTimePart( defaultZone );
if ( timezone != null )
{
ZoneOffset currentOffset = ZonedDateTime.ofInstant( Instant.now(), timezone() ).getOffset();
Expand Down Expand Up @@ -218,6 +222,18 @@ OffsetTime getTimePart( Supplier<ZoneId> defaultZone )
return value;
}

@Override
ZoneId getZoneId( Supplier<ZoneId> defaultZone )
{
return value.getOffset();
}

@Override
public boolean hasTimeZone()
{
return true;
}

@Override
public boolean equals( Value other )
{
Expand Down
Expand Up @@ -81,7 +81,7 @@ public void shouldParseDateTime()
}

@Ignore
public void shouldSupportLeadSeconds()
public void shouldSupportLeapSeconds()
{
// Leap second according to https://www.timeanddate.com/time/leap-seconds-future.html
assertEquals( datetime( 2016, 12, 31, 23, 59, 60, 0, UTC ), parse( "2016-12-31T23:59:60Z", orFail ) );
Expand Down Expand Up @@ -318,14 +318,6 @@ public void shouldRejectInvalidFieldCombinations()
.add( "month", 12 )
.add( "dayOfWeek", 5 )
.assertThrows( IllegalArgumentException.class, "Cannot assign dayOfWeek to calendar date." );
asserting( fromValues( builder( clock ) ) )
.add( "datetime", localDateTime( LocalDateTime.now( clock ) ) )
.add( "day", 12 )
.assertThrows( IllegalArgumentException.class, "Cannot assign day when selecting datetime." );
asserting( fromValues( builder( clock ) ) )
.add( "datetime", localDateTime( LocalDateTime.now( clock ) ) )
.add( "hour", 12 )
.assertThrows( IllegalArgumentException.class, "Cannot assign hour when selecting datetime." );
asserting( fromValues( builder( clock ) ) )
.add( "year", 2018 )
.add( "week", 12 )
Expand Down

0 comments on commit f2a5c87

Please sign in to comment.