Skip to content

Commit

Permalink
Set UTC zone for UNIX timestamp explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
pmwmedia committed Jul 7, 2019
1 parent 09f3b26 commit d500b46
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package org.tinylog.pattern;

import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -79,7 +81,7 @@ public void dateWithInvalidPattern() {
*/
@Test
public void timestampWithDefaultPattern() {
LocalDate date = LocalDate.of(1985, 06, 03);
ZonedDateTime date = LocalDate.of(1985, 06, 03).atStartOfDay(ZoneOffset.UTC);
assertThat(render("timestamp", LogEntryBuilder.empty().date(date).create())).isEqualTo("486604800");
}

Expand All @@ -89,7 +91,7 @@ public void timestampWithDefaultPattern() {
*/
@Test
public void timestampWithMillisecondsPattern() {
LocalDate date = LocalDate.of(1985, 06, 03);
ZonedDateTime date = LocalDate.of(1985, 06, 03).atStartOfDay(ZoneOffset.UTC);
assertThat(render("timestamp: milliseconds", LogEntryBuilder.empty().date(date).create())).isEqualTo("486604800000");
}

Expand All @@ -98,7 +100,7 @@ public void timestampWithMillisecondsPattern() {
*/
@Test
public void timestampWithUnknownPattern() {
LocalDate date = LocalDate.of(1985, 06, 03);
ZonedDateTime date = LocalDate.of(1985, 06, 03).atStartOfDay(ZoneOffset.UTC);
assertThat(render("timestamp: inval'd", LogEntryBuilder.empty().date(date).create())).isEqualTo("486604800");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

import org.junit.Test;
import org.tinylog.core.LogEntry;
Expand Down Expand Up @@ -76,7 +77,7 @@ public void applyTimestamp() throws SQLException {
LocalDateTime now = LocalDateTime.now();

PreparedStatement statement = mock(PreparedStatement.class);
token.apply(createLogEntry(now), statement, 1);
token.apply(LogEntryBuilder.empty().date(now).create(), statement, 1);
verify(statement).setTimestamp(1, Timestamp.valueOf(now));
}

Expand All @@ -103,7 +104,7 @@ private static String render(final Token token, final LocalDateTime timestamp) {
* @return Filled log entry
*/
private static LogEntry createLogEntry(final LocalDateTime date) {
return LogEntryBuilder.empty().date(date).create();
return LogEntryBuilder.empty().date(date.atZone(ZoneOffset.UTC)).create();
}

}
13 changes: 13 additions & 0 deletions tinylog-impl/src/test/java/org/tinylog/util/LogEntryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -160,6 +161,18 @@ public LogEntryBuilder date(final LocalDateTime date) {
return this;
}

/**
* Sets the data and time when this log entry was issued.
*
* @param date
* Date and time of issue
* @return Actual log entry builder
*/
public LogEntryBuilder date(final ZonedDateTime date) {
this.timestamp = new InstantTimestamp(date.toInstant());
return this;
}

/**
* Sets the thread that has issued this log entry.
*
Expand Down

0 comments on commit d500b46

Please sign in to comment.