Skip to content

Commit fb5c70d

Browse files
authored
feat: DH-20385: migrate to ZoneId (#7191)
1 parent a3c10c0 commit fb5c70d

10 files changed

Lines changed: 105 additions & 46 deletions

File tree

Base/src/main/java/io/deephaven/base/log/LogOutput.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
import java.net.SocketAddress;
1111
import java.nio.ByteBuffer;
1212
import java.time.LocalDateTime;
13+
import java.time.ZoneId;
1314
import java.util.Collection;
14-
import java.util.TimeZone;
1515

1616
// --------------------------------------------------------------------
1717
/**
@@ -24,7 +24,7 @@
2424
*/
2525
public interface LogOutput {
2626

27-
TimestampBuffer millisFormatter = new TimestampBuffer(TimeZone.getDefault());
27+
TimestampBuffer millisFormatter = new TimestampBuffer(ZoneId.systemDefault());
2828

2929
LogOutput start();
3030

@@ -309,7 +309,7 @@ interface ObjObjFormatter<T, U> {
309309
} else {
310310
// This involves some overhead, but a timezone is really useful in logs
311311
logOutput.append(millisFormatter
312-
.getTimestamp(localDateTime.atZone(TimeZone.getDefault().toZoneId()).toInstant().toEpochMilli()));
312+
.getTimestamp(localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()));
313313
}
314314
};
315315

Base/src/main/java/io/deephaven/base/text/TimestampBuffer.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.nio.ByteBuffer;
77
import java.nio.BufferOverflowException;
88
import java.time.Instant;
9+
import java.time.ZoneId;
910
import java.time.zone.ZoneOffsetTransition;
1011
import java.time.zone.ZoneRules;
1112
import java.util.TimeZone;
@@ -83,8 +84,17 @@ private void calculateDSTTransitions(final long nowMillis) {
8384
private ThreadLocal<TimestampBuffer.ThreadLocalState> threadLocals =
8485
ThreadLocal.withInitial(TimestampBuffer.ThreadLocalState::new);
8586

87+
public TimestampBuffer(final ZoneId zoneId) {
88+
zoneRules = zoneId.getRules();
89+
}
90+
91+
/**
92+
* @param tz the timezone
93+
* @deprecated use {@link #TimestampBuffer(ZoneId)}
94+
*/
95+
@Deprecated
8696
public TimestampBuffer(TimeZone tz) {
87-
zoneRules = tz.toZoneId().getRules();
97+
this(tz.toZoneId());
8898
}
8999

90100
@Deprecated

Base/src/main/java/io/deephaven/base/text/TimestampBufferMicros.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import java.nio.ByteBuffer;
77
import java.time.Instant;
8+
import java.time.ZoneId;
89
import java.time.zone.ZoneOffsetTransition;
910
import java.time.zone.ZoneRules;
1011
import java.util.TimeZone;
@@ -86,8 +87,17 @@ private void calculateDSTTransitions(final long nowMicros) {
8687

8788
private ThreadLocal<ThreadLocalState> threadLocals = ThreadLocal.withInitial(ThreadLocalState::new);
8889

90+
public TimestampBufferMicros(ZoneId zoneId) {
91+
zoneRules = zoneId.getRules();
92+
}
93+
94+
/**
95+
* @param tz the timezone
96+
* @deprecated use {@link #TimestampBufferMicros(ZoneId)}
97+
*/
98+
@Deprecated
8999
public TimestampBufferMicros(TimeZone tz) {
90-
zoneRules = tz.toZoneId().getRules();
100+
this(tz.toZoneId());
91101
}
92102

93103
@Deprecated

Base/src/test/java/io/deephaven/base/text/TestConvert.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
import junit.framework.TestCase;
77

88
import java.nio.ByteBuffer;
9-
import java.text.DateFormat;
10-
import java.text.ParseException;
11-
import java.text.SimpleDateFormat;
12-
import java.util.TimeZone;
9+
import java.time.Instant;
10+
import java.time.ZoneId;
11+
import java.time.format.DateTimeFormatter;
1312

1413
@SuppressWarnings("UnusedAssignment")
1514
public class TestConvert extends TestCase {
@@ -149,9 +148,9 @@ public void testAppendDoubleBuffer() {
149148

150149
// ################################################################
151150

152-
public void testAppendISO8601Millis() throws ParseException {
153-
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
154-
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
151+
public void testAppendISO8601Millis() {
152+
DateTimeFormatter dateFormat =
153+
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS").withZone(ZoneId.of("GMT"));
155154

156155
long[] testCases = {
157156
0L, 1L, 10L, 100L, 1000L, 10000L, 100000L, 1000000L, 10000000L, 100000000L, 1000000000L, 10000000000L,
@@ -160,7 +159,7 @@ public void testAppendISO8601Millis() throws ParseException {
160159
-10000000000L, -100000000000L, -1000000000000L, -10000000000000L,
161160
};
162161
for (long testCase : testCases) {
163-
checkAppendIso8601Millis(testCase, dateFormat.format(testCase));
162+
checkAppendIso8601Millis(testCase, dateFormat.format(Instant.ofEpochMilli(testCase)));
164163
}
165164

166165
checkAppendIso8601Millis(-62167132799999L, "0000-01-01T00:00:00.001");
@@ -170,9 +169,12 @@ public void testAppendISO8601Millis() throws ParseException {
170169
checkAppendIso8601Millis(253402300799999L, "9999-12-31T23:59:59.999");
171170
checkAppendIso8601Millis(253402300800000L, "9999-99-99T99:99:99.999");
172171

173-
checkAppendIso8601Millis(dateFormat.parse("2004-02-20T11:12:13.014").getTime(), "2004-02-20T11:12:13.014");
174-
checkAppendIso8601Millis(dateFormat.parse("2000-02-20T11:12:13.014").getTime(), "2000-02-20T11:12:13.014");
175-
checkAppendIso8601Millis(dateFormat.parse("1900-02-20T11:12:13.014").getTime(), "1900-02-20T11:12:13.014");
172+
checkAppendIso8601Millis(Instant.from(dateFormat.parse("2004-02-20T11:12:13.014")).toEpochMilli(),
173+
"2004-02-20T11:12:13.014");
174+
checkAppendIso8601Millis(Instant.from(dateFormat.parse("2000-02-20T11:12:13.014")).toEpochMilli(),
175+
"2000-02-20T11:12:13.014");
176+
checkAppendIso8601Millis(Instant.from(dateFormat.parse("1900-02-20T11:12:13.014")).toEpochMilli(),
177+
"1900-02-20T11:12:13.014");
176178

177179
{
178180
ByteBuffer byteBuffer = ByteBuffer.allocate(100);
@@ -188,9 +190,9 @@ private void checkAppendIso8601Millis(long millis, String expectedString) {
188190
assertBufferEqual(byteBuffer, 0, expectedString);
189191
}
190192

191-
public void testAppendISO8601Micros() throws ParseException {
192-
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
193-
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
193+
public void testAppendISO8601Micros() {
194+
DateTimeFormatter dateFormat =
195+
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS").withZone(ZoneId.of("GMT"));
194196

195197
long[] testCases = {
196198
0L,
@@ -225,7 +227,7 @@ public void testAppendISO8601Micros() throws ParseException {
225227
-10000000000000000L
226228
};
227229
for (long testCase : testCases) {
228-
checkAppendIso8601Micros(testCase, dateFormat.format(testCase / 1000) + "000");
230+
checkAppendIso8601Micros(testCase, dateFormat.format(Instant.ofEpochMilli(testCase / 1000)) + "000");
229231
}
230232

231233
checkAppendIso8601Micros(-62167132799999999L, "0000-01-01T00:00:00.000001");
@@ -235,11 +237,11 @@ public void testAppendISO8601Micros() throws ParseException {
235237
checkAppendIso8601Micros(253402300799999000L, "9999-12-31T23:59:59.999000");
236238
checkAppendIso8601Micros(253402300800000000L, "9999-99-99T99:99:99.999999");
237239

238-
checkAppendIso8601Micros(dateFormat.parse("2004-02-20T11:12:13.014").getTime() * 1000,
240+
checkAppendIso8601Micros(Instant.from(dateFormat.parse("2004-02-20T11:12:13.014")).toEpochMilli() * 1000,
239241
"2004-02-20T11:12:13.014000");
240-
checkAppendIso8601Micros(dateFormat.parse("2000-02-20T11:12:13.014").getTime() * 1000,
242+
checkAppendIso8601Micros(Instant.from(dateFormat.parse("2000-02-20T11:12:13.014")).toEpochMilli() * 1000,
241243
"2000-02-20T11:12:13.014000");
242-
checkAppendIso8601Micros(dateFormat.parse("1900-02-20T11:12:13.014").getTime() * 1000,
244+
checkAppendIso8601Micros(Instant.from(dateFormat.parse("1900-02-20T11:12:13.014")).toEpochMilli() * 1000,
243245
"1900-02-20T11:12:13.014000");
244246

245247
{

Configuration/src/main/java/io/deephaven/configuration/Configuration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,9 @@ private static String getRootPath() {
300300

301301
/**
302302
* @return the TimeZone the server is running in
303+
* @deprecated prefer {@link ZoneId#systemDefault()}
303304
*/
305+
@Deprecated
304306
public TimeZone getServerTimezone() {
305307
return TimeZone.getTimeZone(ZoneId.systemDefault());
306308
}

IO/src/main/java/io/deephaven/io/logger/LoggerImpl.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import io.deephaven.io.log.LogSink;
1111
import org.jetbrains.annotations.Nullable;
1212

13+
import java.time.ZoneId;
1314
import java.util.TimeZone;
1415

1516
public class LoggerImpl implements Logger {
@@ -18,7 +19,6 @@ public class LoggerImpl implements Logger {
1819
protected final LogEntryPool logEntryPool;
1920
protected final LogSink logSink;
2021
protected final String name;
21-
protected final TimeZone tz;
2222
protected final LoggerTimeSource timeSource;
2323
protected TimestampBufferMicros localTimestamp;
2424
protected LogLevel loggingLevel;
@@ -36,20 +36,28 @@ protected String initialValue() {
3636
};
3737

3838
public LoggerImpl(LogEntryPool logEntryPool, LogSink logSink, String prefix, LogLevel loggingLevel,
39-
LoggerTimeSource timeSource, TimeZone tz, boolean showLevel, boolean showThreadName) {
39+
LoggerTimeSource timeSource, ZoneId zoneId, boolean showLevel, boolean showThreadName) {
4040
this.logEntryPool = logEntryPool;
4141
this.logSink = logSink;
42-
4342
this.name = prefix;
4443
this.timeSource = timeSource;
45-
this.tz = tz;
46-
this.localTimestamp = tz == null ? null : new TimestampBufferMicros(tz);
44+
this.localTimestamp = zoneId == null ? null : new TimestampBufferMicros(zoneId);
4745
this.showLevel = showLevel;
4846
this.showThreadName = showThreadName;
49-
5047
this.loggingLevel = loggingLevel;
5148
}
5249

50+
/**
51+
* @deprecated use
52+
* {@link #LoggerImpl(LogEntryPool, LogSink, String, LogLevel, LoggerTimeSource, ZoneId, boolean, boolean)}
53+
*/
54+
@Deprecated
55+
public LoggerImpl(LogEntryPool logEntryPool, LogSink logSink, String prefix, LogLevel loggingLevel,
56+
LoggerTimeSource timeSource, TimeZone tz, boolean showLevel, boolean showThreadName) {
57+
this(logEntryPool, logSink, prefix, loggingLevel, timeSource, tz == null ? null : tz.toZoneId(), showLevel,
58+
showThreadName);
59+
}
60+
5361
@Override
5462
public void shutdown() {
5563
logSink.shutdown();
@@ -78,7 +86,7 @@ public LogEntry getEntry(LogLevel level, long currentTimeMicros, @Nullable Throw
7886
} else {
7987
LogEntry entry = logEntryPool.take().start(logSink, level, currentTimeMicros, t);
8088

81-
if (tz != null) {
89+
if (localTimestamp != null) {
8290
entry.append("[").appendTimestampMicros(entry.getTimestampMicros(), localTimestamp).append("] ");
8391
}
8492

@@ -94,7 +102,7 @@ public LogEntry getEntry(LogLevel level, long currentTimeMicros, @Nullable Throw
94102
entry.append("- ").append(THREAD_NAME.get()).append(" ");
95103
}
96104

97-
if (tz != null || showLevel || name != null || showThreadName) {
105+
if (localTimestamp != null || showLevel || name != null || showThreadName) {
98106
entry.append("- ");
99107
}
100108

IO/src/main/java/io/deephaven/io/logger/NullLoggerImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import org.jetbrains.annotations.Nullable;
1818

1919
import java.nio.ByteBuffer;
20-
import java.util.TimeZone;
20+
import java.time.ZoneId;
2121

2222
/**
2323
* Logger implementation that calls Interceptors on log entries, but doesn't write them to anything. Note that this
@@ -36,8 +36,8 @@ public long currentTimeMicros() {
3636
private NullLoggerImpl(@NotNull final LogEntryPool logEntryPool,
3737
@NotNull final LogLevel loggingLevel,
3838
@NotNull final LoggerTimeSource timeSource,
39-
final TimeZone tz) {
40-
super(logEntryPool, new NullLoggerImpl.Sink(logEntryPool), null, loggingLevel, timeSource, tz, true, false);
39+
final ZoneId zoneId) {
40+
super(logEntryPool, new NullLoggerImpl.Sink(logEntryPool), null, loggingLevel, timeSource, zoneId, true, false);
4141
}
4242

4343
public NullLoggerImpl(@NotNull final LogLevel loggingLevel) {

IO/src/main/java/io/deephaven/io/logger/ProcessStreamLoggerImpl.java

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,26 @@
1212
import java.io.IOException;
1313
import java.io.OutputStream;
1414
import java.io.UncheckedIOException;
15+
import java.time.ZoneId;
1516
import java.util.TimeZone;
1617

1718
/**
1819
* Preserve some of the simplicity of StreamLoggerImpl while also retaining the formatting functionality of LoggerImpl.
1920
*/
2021
public class ProcessStreamLoggerImpl extends LoggerImpl {
2122

23+
public static Logger makeLogger(@NotNull final LoggerTimeSource timeSource,
24+
@NotNull final ZoneId zoneId) {
25+
return makeLogger(System.out, LogLevel.INFO, 1024, 2048, 1024, timeSource, zoneId);
26+
}
27+
28+
/**
29+
* @deprecated prefer {@link #makeLogger(OutputStream, LogLevel, int, int, int, LoggerTimeSource, ZoneId)}
30+
*/
31+
@Deprecated
2232
public static Logger makeLogger(@NotNull final LoggerTimeSource timeSource,
2333
@NotNull final TimeZone tz) {
24-
return makeLogger(System.out, LogLevel.INFO, 1024, 2048, 1024, timeSource, tz);
34+
return makeLogger(System.out, LogLevel.INFO, 1024, 2048, 1024, timeSource, tz.toZoneId());
2535
}
2636

2737
@SuppressWarnings({"WeakerAccess", "SameParameterValue"})
@@ -31,18 +41,33 @@ public static Logger makeLogger(@NotNull final OutputStream outputStream,
3141
final int bufferCount,
3242
final int entryCount,
3343
@NotNull final LoggerTimeSource timeSource,
34-
@NotNull final TimeZone tz) {
44+
@NotNull final ZoneId zoneId) {
3545
final LogEntryPool logEntryPool =
3646
new LogEntryPoolImpl(entryCount, LogBufferPool.ofStrict(bufferCount, bufferSize));
37-
return new ProcessStreamLoggerImpl(logEntryPool, outputStream, loggingLevel, timeSource, tz);
47+
return new ProcessStreamLoggerImpl(logEntryPool, outputStream, loggingLevel, timeSource, zoneId);
48+
}
49+
50+
/**
51+
* @deprecated prefer {@link #makeLogger(OutputStream, LogLevel, int, int, int, LoggerTimeSource, ZoneId)}
52+
*/
53+
@SuppressWarnings({"WeakerAccess", "SameParameterValue"})
54+
@Deprecated
55+
public static Logger makeLogger(@NotNull final OutputStream outputStream,
56+
@NotNull final LogLevel loggingLevel,
57+
final int bufferSize,
58+
final int bufferCount,
59+
final int entryCount,
60+
@NotNull final LoggerTimeSource timeSource,
61+
@NotNull final TimeZone tz) {
62+
return makeLogger(outputStream, loggingLevel, bufferSize, bufferCount, entryCount, timeSource, tz.toZoneId());
3863
}
3964

4065
private ProcessStreamLoggerImpl(@NotNull final LogEntryPool logEntryPool,
4166
@NotNull final OutputStream outputStream,
4267
@NotNull final LogLevel loggingLevel,
4368
@NotNull final LoggerTimeSource timeSource,
44-
@NotNull final TimeZone tz) {
45-
super(logEntryPool, new Sink(outputStream, logEntryPool), null, loggingLevel, timeSource, tz, true, false);
69+
@NotNull final ZoneId zoneId) {
70+
super(logEntryPool, new Sink(outputStream, logEntryPool), null, loggingLevel, timeSource, zoneId, true, false);
4671
}
4772

4873
/**

Stats/src/main/java/io/deephaven/stats/StatsDriver.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
import io.deephaven.util.annotations.ReferentialIntegrity;
1616
import io.deephaven.util.thread.NamingThreadFactory;
1717

18+
import java.time.ZoneId;
1819
import java.util.Properties;
19-
import java.util.TimeZone;
2020
import java.util.concurrent.Executors;
2121
import java.util.concurrent.ScheduledExecutorService;
2222
import java.util.concurrent.ScheduledFuture;
@@ -124,9 +124,9 @@ public StatsDriver(Clock clock, StatsIntradayLogger intraday, boolean getFdStats
124124
}
125125
}
126126

127-
final TimeZone serverTimeZone = Configuration.getInstance().getServerTimezone();
128-
this.systemTimestamp = new TimestampBuffer(serverTimeZone);
129-
this.appTimestamp = new TimestampBuffer(serverTimeZone);
127+
final ZoneId zoneId = ZoneId.systemDefault();
128+
this.systemTimestamp = new TimestampBuffer(zoneId);
129+
this.appTimestamp = new TimestampBuffer(zoneId);
130130

131131
if (path == null) {
132132
this.entryPool = null;

log-factory/sinks/log-to-file/src/main/java/io/deephaven/internal/log/LoggerFactoryFile.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import io.deephaven.io.logger.LoggerImpl;
1717
import io.deephaven.io.logger.LoggerTimeSource;
1818
import io.deephaven.io.logger.NullLoggerImpl.NullLoggerTimeSource;
19+
20+
import java.time.ZoneId;
1921
import java.util.TimeZone;
2022

2123
@AutoService(LoggerFactory.class)
@@ -40,10 +42,10 @@ private static boolean showThreadName() {
4042
.getProperty("io.deephaven.internal.log.LoggerFactoryFile.showThreadName", "true"));
4143
}
4244

43-
private static TimeZone timeZone() {
45+
private static ZoneId zoneId() {
4446
final String timeZone =
4547
System.getProperty("io.deephaven.internal.log.LoggerFactoryFile.timeZone");
46-
return timeZone == null ? TimeZone.getDefault() : TimeZone.getTimeZone(timeZone);
48+
return timeZone == null ? ZoneId.systemDefault() : ZoneId.of(timeZone, ZoneId.SHORT_IDS);
4749
}
4850

4951
private static LogLevel level() {
@@ -64,7 +66,7 @@ public final Logger createInternal() {
6466
logEntryPool, append(), new LogOutputCsvImpl(bufferPool), header, null);
6567
final String prefix = null;
6668
final LoggerTimeSource timeSource = new NullLoggerTimeSource();
67-
return new LoggerImpl(logEntryPool, logSink, prefix, level(), timeSource, timeZone(),
69+
return new LoggerImpl(logEntryPool, logSink, prefix, level(), timeSource, zoneId(),
6870
showLevel(), showThreadName());
6971
}
7072
}

0 commit comments

Comments
 (0)