Skip to content

Commit

Permalink
Fix toString without fraction digits
Browse files Browse the repository at this point in the history
  • Loading branch information
Morten Haraldsen committed Feb 6, 2024
1 parent fcd1960 commit be838dc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/ethlo/time/DateTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -444,12 +444,12 @@ private String toString(final DateTime date, final Field lastIncluded, final int
}

// Fractions
if (lastIncluded.ordinal() >= Field.NANO.ordinal())
if (fractionDigits > 0 && lastIncluded.ordinal() >= Field.NANO.ordinal())
{
buffer[19] = '.';
LimitedCharArrayIntegerUtil.toString(date.getNano(), buffer, 20, fractionDigits);
}
return finish(buffer, 20 + fractionDigits, tz);
return finish(buffer, 19 + (fractionDigits > 0 ? 1 : 0) + fractionDigits, tz);
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/com/ethlo/time/FormatterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,11 @@ void testFormat5()
final OffsetDateTime date = ITU.parseDateTime(s);
assertThat(ITU.formatUtcMilli(date)).isEqualTo("2017-02-21T02:27:39.123Z");
}

@Test
void testTostring()
{
final DateTime dateTime = DateTime.of(2000,12, 31, 22, 30, 0, 0,null, 0);
assertThat(dateTime.toString()).isEqualTo("2000-12-31T22:30:00");
}
}

0 comments on commit be838dc

Please sign in to comment.