Skip to content

Simplified toString() for duration #490

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 1 commit into from
Apr 23, 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,8 +38,6 @@
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;
Expand Down Expand Up @@ -195,65 +193,6 @@ public int hashCode()
@Override
public String toString()
{
// 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) );
}
return String.format( "P%sM%sDT%s.%sS", months, days, seconds, String.format( "%09d", nanoseconds ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,17 @@ public void shouldCreateFromDuration()
@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( 0, 0, 0, 0 ).toString(), equalTo( "P0M0DT0.000000000S" ) );
assertThat( new InternalIsoDuration( 2, 45, 59, 11 ).toString(), equalTo( "P2M45DT59.000000011S" ) );
assertThat( new InternalIsoDuration( 4, -101, 1, 999 ).toString(), equalTo( "P4M-101DT1.000000999S" ) );
assertThat( new InternalIsoDuration( -1, 12, -19, 1 ).toString(), equalTo( "P-1M12DT-19.000000001S" ) );

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" ) );
assertThat( new InternalIsoDuration( Period.parse( "P356D" ) ).toString(), equalTo( "P0M356DT0.000000000S" ) );
assertThat( new InternalIsoDuration( Duration.parse( "PT45S" ) ).toString(), equalTo( "P0M0DT45.000000000S" ) );

assertThat( new InternalIsoDuration( Period.parse( "P14D" ), Duration.parse( "PT16H12M" ) ).toString(), equalTo( "P0M14DT58320.000000000S" ) );
assertThat( new InternalIsoDuration( Period.parse( "P5M1D" ), Duration.parse( "PT12H" ) ).toString(), equalTo( "P5M1DT43200.000000000S" ) );
assertThat( new InternalIsoDuration( Period.parse( "P2W3D" ), Duration.parse( "PT2H0.111222333S" ) ).toString(), equalTo( "P0M17DT7200.111222333S" ) );
}

private static IsoDuration newDuration( long months, long days, long seconds, int nanoseconds )
Expand Down