Skip to content

Commit

Permalink
Store numeric timestamp in database by timestamp token
Browse files Browse the repository at this point in the history
  • Loading branch information
pmwmedia committed Jul 9, 2019
1 parent f76178e commit ac758b8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 20 deletions.
22 changes: 14 additions & 8 deletions tinylog-impl/src/main/java/org/tinylog/pattern/TimestampToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,24 @@ public Collection<LogEntryValue> 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
}

/**
Expand All @@ -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());
}

/**
Expand Down

0 comments on commit ac758b8

Please sign in to comment.