diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListHealth.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListHealth.java index 6b8da3791ab6c..4956b764332ea 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListHealth.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListHealth.java @@ -16,8 +16,6 @@ */ package org.apache.camel.dsl.jbang.core.commands.process; -import java.time.ZonedDateTime; -import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; @@ -39,6 +37,7 @@ import org.apache.camel.dsl.jbang.core.common.ProcessHelper; import org.apache.camel.dsl.jbang.core.common.RowSort; import org.apache.camel.dsl.jbang.core.common.TerminalWidthHelper; +import org.apache.camel.dsl.jbang.run.common.CliTimes; import org.apache.camel.health.HealthCheckHelper; import org.apache.camel.util.StringHelper; import org.apache.camel.util.TimeUtils; @@ -142,21 +141,15 @@ public Integer doProcessWatchCall() throws Exception { // calc how long time since the check was invoked String time = d.getString("invocation.time"); if (time != null) { - ZonedDateTime zdt = ZonedDateTime.parse(time); - long delta = Math.abs(ZonedDateTime.now().until(zdt, ChronoUnit.MILLIS)); - row.sinceLast = TimeUtils.printAge(delta); + row.sinceLast = sinceNow(time); } time = d.getString("success.start.time"); if (time != null) { - ZonedDateTime zdt = ZonedDateTime.parse(time); - long delta = Math.abs(ZonedDateTime.now().until(zdt, ChronoUnit.MILLIS)); - row.sinceStartSuccess = TimeUtils.printAge(delta); + row.sinceStartSuccess = sinceNow(time); } time = d.getString("failure.start.time"); if (time != null) { - ZonedDateTime zdt = ZonedDateTime.parse(time); - long delta = Math.abs(ZonedDateTime.now().until(zdt, ChronoUnit.MILLIS)); - row.sinceStartFailure = TimeUtils.printAge(delta); + row.sinceStartFailure = sinceNow(time); } for (Map.Entry entry : d.entrySet()) { String k = entry.getKey(); @@ -324,6 +317,14 @@ protected String getSince(Row r) { return s1 + "/" + s2 + "/" + s3; } + /** + * How long ago (or ahead) a time of a health check is, such as {@code 2026-11-16T15:38:49.123+01:00[Europe/Paris]}: + * from its offset, as parsing the region would load the time zone rules (see {@link CliTimes}). + */ + static String sinceNow(String time) { + return TimeUtils.printAge(Math.abs(System.currentTimeMillis() - CliTimes.parseEpochMillis(time))); + } + private static class Row { String pid; String name; diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListSqlTrace.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListSqlTrace.java index ae9e07c6bcb0a..610dca60236ee 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListSqlTrace.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListSqlTrace.java @@ -16,9 +16,6 @@ */ package org.apache.camel.dsl.jbang.core.commands.process; -import java.time.Instant; -import java.time.LocalDateTime; -import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Arrays; @@ -36,6 +33,7 @@ import org.apache.camel.dsl.jbang.core.common.PidNameAgeCompletionCandidates; import org.apache.camel.dsl.jbang.core.common.ProcessHelper; import org.apache.camel.dsl.jbang.core.common.RowSort; +import org.apache.camel.dsl.jbang.run.common.CliTimes; import org.apache.camel.util.TimeUtils; import org.apache.camel.util.json.JsonArray; import org.apache.camel.util.json.JsonObject; @@ -156,8 +154,7 @@ protected void printTable(List rows) { private String formatTime(Row r) { if (r.timestamp > 0) { - return LocalDateTime.ofInstant(Instant.ofEpochMilli(r.timestamp), ZoneId.systemDefault()) - .format(TIME_FMT); + return CliTimes.localDateTime(r.timestamp).format(TIME_FMT); } return ""; } diff --git a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/common/CliTimes.java b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/common/CliTimes.java new file mode 100644 index 0000000000000..c073d67e7a569 --- /dev/null +++ b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/common/CliTimes.java @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.dsl.jbang.run.common; + +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; +import java.util.TimeZone; + +/** + * Local times for the output of the CLI, without the time zone rules of java.time. {@code ZoneId.systemDefault()}, and + * parsing a time with a region such as {@code [Europe/Paris]}, load those rules, and the JDK looks for their providers + * with a {@code ServiceLoader} in every JAR of the class path: about 50 ms with the 340 JARs of the distribution. The + * offset of the default {@link TimeZone} at a time is the same (both read the tzdb data of the JDK), and needs no + * lookup. + */ +public final class CliTimes { + + private CliTimes() { + } + + /** + * The offset of the time zone of this JVM at the given time. + */ + public static ZoneOffset offsetAt(long epochMillis) { + return ZoneOffset.ofTotalSeconds(TimeZone.getDefault().getOffset(epochMillis) / 1000); + } + + /** + * The local date and time of the given time, in the time zone of this JVM. + */ + public static LocalDateTime localDateTime(long epochMillis) { + return LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMillis), offsetAt(epochMillis)); + } + + /** + * The given time formatted in the time zone of this JVM. + */ + public static String format(DateTimeFormatter formatter, long epochMillis) { + return formatter.withZone(offsetAt(epochMillis)).format(Instant.ofEpochMilli(epochMillis)); + } + + /** + * The epoch millis of an ISO date-time with an offset, such as the text of a {@link ZonedDateTime}: + * {@code 2026-11-16T15:38:49.123+01:00[Europe/Copenhagen]}. The region in brackets is left out, as the offset gives + * the instant. + * + * @throws DateTimeParseException when the text is not such a date-time + */ + public static long parseEpochMillis(String text) { + int region = text.indexOf('['); + try { + return OffsetDateTime.parse(region > 0 ? text.substring(0, region) : text).toInstant().toEpochMilli(); + } catch (DateTimeParseException e) { + // not with an offset: java.time parses it with the zone rules + return ZonedDateTime.parse(text).toInstant().toEpochMilli(); + } + } +} diff --git a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/common/PreviousRun.java b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/common/PreviousRun.java index 10113a223cf33..1f56845ec137d 100644 --- a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/common/PreviousRun.java +++ b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/common/PreviousRun.java @@ -25,7 +25,6 @@ import java.nio.file.Path; import java.nio.file.StandardCopyOption; import java.time.Instant; -import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Comparator; @@ -78,8 +77,7 @@ public final class PreviousRun { private static final String INFO_SUFFIX = "-previous.json"; private static final Pattern PID_LOG = Pattern.compile("(\\d+)\\.log"); private static final int MAX_FAILURE = 500; - private static final DateTimeFormatter TIME - = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault()); + private static final DateTimeFormatter TIME = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); /** * A run that ended, and the log it kept. @@ -108,7 +106,7 @@ public String describe() { * When the run ended, in local time, such as 2026-10-19 10:32:01. */ public String endedAt() { - return TIME.format(Instant.ofEpochMilli(ended)); + return CliTimes.format(TIME, ended); } /** diff --git a/dsl/camel-jbang/camel-jbang-run/src/test/java/org/apache/camel/dsl/jbang/run/common/CliTimesTest.java b/dsl/camel-jbang/camel-jbang-run/src/test/java/org/apache/camel/dsl/jbang/run/common/CliTimesTest.java new file mode 100644 index 0000000000000..f3ae297dd84df --- /dev/null +++ b/dsl/camel-jbang/camel-jbang-run/src/test/java/org/apache/camel/dsl/jbang/run/common/CliTimesTest.java @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.dsl.jbang.run.common; + +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; +import java.util.List; +import java.util.TimeZone; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class CliTimesTest { + + private static final DateTimeFormatter FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + + // times around the changes of daylight saving time, and zones with half an hour + private static final List TIMES = List.of( + "2026-03-29T00:59:59Z", "2026-03-29T01:00:00Z", "2026-10-25T00:59:59Z", "2026-10-25T01:00:00Z", + "2026-03-08T06:59:59Z", "2026-03-08T07:00:00Z", "2026-11-01T05:59:59Z", "2026-11-01T06:00:00Z", + "2026-04-04T14:59:59Z", "2026-04-04T15:00:00Z", "2026-07-01T12:00:00Z", "1999-12-31T23:59:59Z"); + + @Test + void sameAsTheZoneRules() { + TimeZone original = TimeZone.getDefault(); + try { + for (String zone : List.of("Europe/Copenhagen", "America/New_York", "Asia/Kolkata", "Australia/Lord_Howe", + "UTC")) { + TimeZone.setDefault(TimeZone.getTimeZone(zone)); + ZoneId id = ZoneId.of(zone); + for (String time : TIMES) { + long millis = Instant.parse(time).toEpochMilli(); + assertThat(CliTimes.format(FORMAT, millis)).as(zone + " " + time) + .isEqualTo(FORMAT.withZone(id).format(Instant.ofEpochMilli(millis))); + assertThat(CliTimes.localDateTime(millis)).as(zone + " " + time) + .isEqualTo(LocalDateTime.ofInstant(Instant.ofEpochMilli(millis), id)); + } + } + } finally { + TimeZone.setDefault(original); + } + } + + @Test + void parseTheTextOfAZonedDateTime() { + ZonedDateTime time = ZonedDateTime.of(2026, 11, 16, 15, 38, 49, 123_000_000, ZoneId.of("Europe/Copenhagen")); + long expected = time.toInstant().toEpochMilli(); + + assertThat(CliTimes.parseEpochMillis(time.toString())).isEqualTo(expected); + assertThat(CliTimes.parseEpochMillis("2026-11-16T15:38:49.123+01:00[Europe/Copenhagen]")).isEqualTo(expected); + assertThat(CliTimes.parseEpochMillis("2026-11-16T15:38:49.123+01:00")).isEqualTo(expected); + assertThat(CliTimes.parseEpochMillis("2026-11-16T14:38:49.123Z")).isEqualTo(expected); + assertThat(CliTimes.parseEpochMillis("2026-11-16T14:38:49.123Z[UTC]")).isEqualTo(expected); + } + + @Test + void notADateTime() { + assertThatThrownBy(() -> CliTimes.parseEpochMillis("yesterday")).isInstanceOf(DateTimeParseException.class); + } +}