diff --git a/tinylog-impl/src/main/java/org/tinylog/pattern/TimestampToken.java b/tinylog-impl/src/main/java/org/tinylog/pattern/TimestampToken.java index b493154e3..b86d61387 100644 --- a/tinylog-impl/src/main/java/org/tinylog/pattern/TimestampToken.java +++ b/tinylog-impl/src/main/java/org/tinylog/pattern/TimestampToken.java @@ -51,18 +51,24 @@ public Collection getRequiredLogEntryValues() { @Override public void render(final LogEntry logEntry, final StringBuilder builder) { - long timestamp = logEntry.getTimestamp().toDate().getTime(); - - if (useMilliseconds) { - builder.append(timestamp); - } else { - builder.append(timestamp / SECONDS_DIVISOR); - } + builder.append(getTime(logEntry)); } @Override public void apply(final LogEntry logEntry, final PreparedStatement statement, final int index) throws SQLException { - statement.setTimestamp(index, logEntry.getTimestamp().toSqlTimestamp()); + statement.setLong(index, getTime(logEntry)); + } + + /** + * Gets the time of issue from a long entry. + * + * @param logEntry + * Log entry to get time of issue from + * @return Time of issue as Unix timestamp + */ + private long getTime(final LogEntry logEntry) { + long timestamp = logEntry.getTimestamp().toDate().getTime(); + return useMilliseconds ? timestamp : timestamp / SECONDS_DIVISOR; } } diff --git a/tinylog-impl/src/test/java/org/tinylog/pattern/TimestampTokenTest.java b/tinylog-impl/src/test/java/org/tinylog/pattern/TimestampTokenTest.java index 47a5c93a6..0dc1541aa 100644 --- a/tinylog-impl/src/test/java/org/tinylog/pattern/TimestampTokenTest.java +++ b/tinylog-impl/src/test/java/org/tinylog/pattern/TimestampTokenTest.java @@ -15,9 +15,9 @@ import java.sql.PreparedStatement; import java.sql.SQLException; -import java.sql.Timestamp; import java.time.LocalDateTime; import java.time.ZoneOffset; +import java.util.Date; import org.junit.Test; import org.tinylog.core.LogEntry; @@ -42,16 +42,26 @@ public void requiredLogEntryValues() { assertThat(token.getRequiredLogEntryValues()).containsOnly(LogEntryValue.DATE); } + /** + * Verifies that seconds pattern will be rendered correctly for a {@link StringBuilder}. + */ + @Test + public void renderSecondsPattern() { + TimestampToken token = new TimestampToken("seconds"); + + assertThat(render(token, LocalDateTime.of(2016, 6, 30, 12, 0, 0, 0))).isEqualTo("1467288000"); + assertThat(render(token, LocalDateTime.of(2016, 6, 30, 12, 15, 1, 987654321))).isEqualTo("1467288901"); + } + /** * Verifies that milliseconds pattern will be rendered correctly for a {@link StringBuilder}. */ @Test - public void renderMillisecondsTimestampPattern() { + public void renderMillisecondsPattern() { TimestampToken token = new TimestampToken("milliseconds"); - assertThat(render(token, LocalDateTime.of(2016, 01, 01, 00, 00))).isEqualTo("1451606400000"); - assertThat(render(token, LocalDateTime.of(2016, 01, 01, 12, 00))).isEqualTo("1451649600000"); - assertThat(render(token, LocalDateTime.of(2016, 01, 02, 00, 00))).isEqualTo("1451692800000"); + assertThat(render(token, LocalDateTime.of(2016, 6, 30, 12, 0, 0, 0))).isEqualTo("1467288000000"); + assertThat(render(token, LocalDateTime.of(2016, 6, 30, 12, 15, 1, 987654321))).isEqualTo("1467288901987"); } /** @@ -61,24 +71,40 @@ public void renderMillisecondsTimestampPattern() { public void renderDefaultPattern() { TimestampToken token = new TimestampToken(); - assertThat(render(token, LocalDateTime.of(2016, 06, 30, 12, 00))).isEqualTo("1467288000"); - assertThat(render(token, LocalDateTime.of(2016, 06, 30, 12, 15))).isEqualTo("1467288900"); + assertThat(render(token, LocalDateTime.of(2016, 6, 30, 12, 0, 0, 0))).isEqualTo("1467288000"); + assertThat(render(token, LocalDateTime.of(2016, 6, 30, 12, 15, 1, 987654321))).isEqualTo("1467288901"); } /** - * Verifies that the current time will be added as a {@link Timestamp} to a {@link PreparedStatement}. + * Verifies that seconds pattern will be added correctly to a {@link PreparedStatement}. * * @throws SQLException * Failed to add value to prepared SQL statement */ @Test - public void applyTimestamp() throws SQLException { - TimestampToken token = new TimestampToken(); + public void applySecondsTimestamp() throws SQLException { + TimestampToken token = new TimestampToken("seconds"); + LocalDateTime now = LocalDateTime.now(); + + PreparedStatement statement = mock(PreparedStatement.class); + token.apply(createLogEntry(now), statement, 1); + verify(statement).setLong(1, now.atZone(ZoneOffset.UTC).toEpochSecond()); + } + + /** + * Verifies that milliseconds pattern will be added correctly to a {@link PreparedStatement}. + * + * @throws SQLException + * Failed to add value to prepared SQL statement + */ + @Test + public void applyMillisecondsTimestamp() throws SQLException { + TimestampToken token = new TimestampToken("milliseconds"); LocalDateTime now = LocalDateTime.now(); PreparedStatement statement = mock(PreparedStatement.class); - token.apply(LogEntryBuilder.empty().date(now).create(), statement, 1); - verify(statement).setTimestamp(1, Timestamp.valueOf(now)); + token.apply(createLogEntry(now), statement, 1); + verify(statement).setLong(1, Date.from(now.atZone(ZoneOffset.UTC).toInstant()).getTime()); } /**