Skip to content

Commit 10d9b3d

Browse files
pgomulkafelixbarny
authored andcommitted
TimestampSerializer should not depend on Locale (#54)
Users are expected to run any custom locale which impacts how TimestampSerializer caches formatted date. To avoid that Locale.ROOT is used to ignore user's set locale
1 parent cef254b commit 10d9b3d

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

ecs-logging-core/src/main/java/co/elastic/logging/TimestampSerializer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import java.text.SimpleDateFormat;
2828
import java.util.Date;
29+
import java.util.Locale;
2930
import java.util.TimeZone;
3031

3132
/**
@@ -109,7 +110,7 @@ private static class CachedDate {
109110
private final long endOfCachedDate;
110111

111112
private CachedDate(long epochTimestamp) {
112-
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
113+
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ROOT);
113114
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
114115
cachedDateIso = dateFormat.format(new Date(epochTimestamp));
115116
startOfCachedDate = atStartOfDay(epochTimestamp);

ecs-logging-core/src/test/java/co/elastic/logging/TimestampSerializerTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.time.ZoneOffset;
3434
import java.time.format.DateTimeFormatter;
3535
import java.time.temporal.ChronoUnit;
36+
import java.util.Locale;
3637

3738
import static org.assertj.core.api.Assertions.assertThat;
3839

@@ -41,12 +42,25 @@ class TimestampSerializerTest {
4142
private TimestampSerializer dateSerializer;
4243
private DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").withZone(ZoneId.of("UTC"));
4344

44-
4545
@BeforeEach
4646
void setUp() {
4747
dateSerializer = new TimestampSerializer();
4848
}
4949

50+
@Test
51+
public void testSerializeWithCustomLocale() throws InterruptedException {
52+
Locale.setDefault(new Locale.Builder()
53+
.setLanguage("uz")
54+
.setRegion("UZ")
55+
.setScript("Cyrl")
56+
.build());
57+
58+
dateSerializer = new TimestampSerializer();
59+
60+
long timestamp = Instant.now().toEpochMilli();
61+
assertDateFormattingIsCorrect(Instant.ofEpochMilli(timestamp));
62+
}
63+
5064
@Test
5165
void testSerializeEpochTimestampAsIsoDateTime() {
5266
long timestamp = 0;

0 commit comments

Comments
 (0)