Skip to content

Print ISO standard format in IsoDuration#toString() #483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@
public class InternalIsoDuration implements IsoDuration
{
private static final List<TemporalUnit> SUPPORTED_UNITS = unmodifiableList( asList( MONTHS, DAYS, SECONDS, NANOS ) );
private static final InternalIsoDuration ZERO = new InternalIsoDuration( 0, 0, 0, 0 );
public static final long NANOS_PER_SECOND = 1_000_000_000L;

private final long months;
private final long days;
private final long seconds;
private final long nanoseconds;
private final int nanoseconds;

public InternalIsoDuration( Period period )
{
Expand All @@ -54,14 +56,19 @@ public InternalIsoDuration( Duration duration )
this( 0, 0, duration.getSeconds(), duration.getNano() );
}

public InternalIsoDuration( long months, long days, long seconds, long nanoseconds )
public InternalIsoDuration( long months, long days, long seconds, int nanoseconds )
{
this.months = months;
this.days = days;
this.seconds = seconds;
this.nanoseconds = nanoseconds;
}

public InternalIsoDuration( Period period, Duration duration )
{
this( period.toTotalMonths(), period.getDays(), duration.getSeconds(), duration.getNano() );
}

@Override
public long months()
{
Expand All @@ -81,7 +88,7 @@ public long seconds()
}

@Override
public long nanoseconds()
public int nanoseconds()
{
return nanoseconds;
}
Expand Down Expand Up @@ -188,11 +195,65 @@ public int hashCode()
@Override
public String toString()
{
return "Duration{" +
"months=" + months +
", days=" + days +
", seconds=" + seconds +
", nanoseconds=" + nanoseconds +
'}';
// print the duration in iso standard format.
if ( this.equals( ZERO ) )
{
return "PT0S"; // no need to allocate a string builder if we know the result
}
StringBuilder str = new StringBuilder().append( "P" );
append( str, months / 12, 'Y' );
append( str, months % 12, 'M' );
append( str, days / 7, 'W' );
append( str, days % 7, 'D' );
if ( seconds != 0 || nanoseconds != 0 )
{
str.append( 'T' );
long s = seconds % 3600;
append( str, seconds / 3600, 'H' );
append( str, s / 60, 'M' );
s %= 60;
if ( s != 0 )
{
str.append( s );
if ( nanoseconds != 0 )
{
nanos( str );
}
str.append( 'S' );
}
else if ( nanoseconds != 0 )
{
if ( nanoseconds < 0 )
{
str.append( '-' );
}
str.append( '0' );
nanos( str );
str.append( 'S' );
}
}
if ( str.length() == 1 )
{ // this was all zeros (but not ZERO for some reason), ensure well formed output:
str.append( "T0S" );
}
return str.toString();
}

private static void append( StringBuilder str, long quantity, char unit )
{
if ( quantity != 0 )
{
str.append( quantity ).append( unit );
}
}

private void nanos( StringBuilder str )
{
str.append( '.' );
int n = nanoseconds < 0 ? -nanoseconds : nanoseconds;
for ( int mod = (int)NANOS_PER_SECOND; mod > 1 && n > 0; n %= mod )
{
str.append( n / (mod /= 10) );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ private Value unpackDuration() throws IOException
long months = unpacker.unpackLong();
long days = unpacker.unpackLong();
long seconds = unpacker.unpackLong();
long nanoseconds = unpacker.unpackLong();
int nanoseconds = Math.toIntExact( unpacker.unpackLong() );
return isoDuration( months, days, seconds, nanoseconds );
}

Expand Down
2 changes: 1 addition & 1 deletion driver/src/main/java/org/neo4j/driver/v1/Values.java
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public static Value value( Duration duration )
return value( new InternalIsoDuration( duration ) );
}

public static Value isoDuration( long months, long days, long seconds, long nanoseconds )
public static Value isoDuration( long months, long days, long seconds, int nanoseconds )
{
return value( new InternalIsoDuration( months, days, seconds, nanoseconds ) );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ public interface IsoDuration extends TemporalAmount
*
* @return number of nanoseconds.
*/
long nanoseconds();
int nanoseconds();
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
import static java.time.temporal.ChronoUnit.SECONDS;
import static java.time.temporal.ChronoUnit.YEARS;
import static java.util.Arrays.asList;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

public class InternalIsoDurationTest
Expand Down Expand Up @@ -151,7 +153,19 @@ public void shouldCreateFromDuration()
assertEquals( duration.getNano(), isoDuration.nanoseconds() );
}

private static IsoDuration newDuration( long months, long days, long seconds, long nanoseconds )
@Test
public void toStringShouldPrintInIsoStandardFormat() throws Throwable
{
assertThat( new InternalIsoDuration( 0, 0, 0, 0 ).toString(), equalTo( "PT0S" ) );
assertThat( new InternalIsoDuration( Period.parse( "P356D" ) ).toString(), equalTo( "P50W6D" ) );
assertThat( new InternalIsoDuration( Duration.parse( "PT45S" ) ).toString(), equalTo( "PT45S" ) );

assertThat( new InternalIsoDuration( Period.parse( "P14D" ), Duration.parse( "PT16H12M" ) ).toString(), equalTo( "P2WT16H12M" ) );
assertThat( new InternalIsoDuration( Period.parse( "P5M1D" ), Duration.parse( "PT12H" ) ).toString(), equalTo( "P5M1DT12H" ) );
assertThat( new InternalIsoDuration( Period.parse( "P2W3D" ), Duration.parse( "PT12H" ) ).toString(), equalTo( "P2W3DT12H" ) );
}

private static IsoDuration newDuration( long months, long days, long seconds, int nanoseconds )
{
return new InternalIsoDuration( months, days, seconds, nanoseconds );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void shouldNotSupportAsLong()
}
}

private static IsoDuration newDuration( long months, long days, long seconds, long nanoseconds )
private static IsoDuration newDuration( long months, long days, long seconds, int nanoseconds )
{
return new InternalIsoDuration( months, days, seconds, nanoseconds );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ private <T> void testSendAndReceiveValue( T value, Function<Value,T> converter )
assertEquals( value, converter.apply( record.get( 0 ) ) );
}

private static IsoDuration newDuration( long months, long days, long seconds, long nanoseconds )
private static IsoDuration newDuration( long months, long days, long seconds, int nanoseconds )
{
return isoDuration( months, days, seconds, nanoseconds ).asIsoDuration();
}
Expand Down