From bbadb2b1efa051b9ff4c26cfde19638ab2092c46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Rebelo?= Date: Sat, 26 Oct 2024 16:53:30 +0100 Subject: [PATCH] Garmin: Fix crash on timezones without DST --- .../messages/CurrentTimeRequestMessage.java | 34 ++++++++++++++++--- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/CurrentTimeRequestMessage.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/CurrentTimeRequestMessage.java index c54dfb06e..f04a708a6 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/CurrentTimeRequestMessage.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/garmin/messages/CurrentTimeRequestMessage.java @@ -34,11 +34,35 @@ public class CurrentTimeRequestMessage extends GFDIMessage { final int garminTimestamp = GarminTimeUtils.unixTimeToGarminTimestamp((int) now.getEpochSecond()); final ZoneOffsetTransition nextTransitionStart = zoneRules.nextTransition(now); - final int nextTransitionStartsTs = (int) nextTransitionStart.toEpochSecond(); - final int nextTransitionEndsTs = (int) zoneRules.nextTransition(nextTransitionStart.getInstant()).toEpochSecond(); + final int nextTransitionEndsGarminTs; + final int nextTransitionStartsGarminTs; + if (nextTransitionStart != null) { + final int nextTransitionStartsTs = (int) nextTransitionStart.toEpochSecond(); + nextTransitionStartsGarminTs = GarminTimeUtils.unixTimeToGarminTimestamp(nextTransitionStartsTs); - LOG.info("Processing current time request #{}: time={}, DST={}, ofs={}", referenceID, garminTimestamp, dstOffset, timeZoneOffset); + final ZoneOffsetTransition nextTransitionEnd = zoneRules.nextTransition(nextTransitionStart.getInstant()); + + if (nextTransitionEnd != null) { + final int nextTransitionEndsTs = (int) nextTransitionEnd.toEpochSecond(); + nextTransitionEndsGarminTs = GarminTimeUtils.unixTimeToGarminTimestamp(nextTransitionEndsTs); + } else { + nextTransitionEndsGarminTs = 0; + } + } else { + nextTransitionEndsGarminTs = 0; + nextTransitionStartsGarminTs = 0; + } + + LOG.info( + "Processing current time request #{}: time={}, DST={}, ofs={}, nextTransitionEnds={}, nextTransitionStarts={}", + referenceID, + garminTimestamp, + dstOffset, + timeZoneOffset, + nextTransitionEndsGarminTs, + nextTransitionStartsGarminTs + ); final MessageWriter writer = new MessageWriter(response); writer.writeShort(0); // packet size will be filled below @@ -48,8 +72,8 @@ public class CurrentTimeRequestMessage extends GFDIMessage { writer.writeInt(referenceID); writer.writeInt(garminTimestamp); writer.writeInt(timeZoneOffset); - writer.writeInt(GarminTimeUtils.unixTimeToGarminTimestamp(nextTransitionEndsTs)); - writer.writeInt(GarminTimeUtils.unixTimeToGarminTimestamp(nextTransitionStartsTs)); + writer.writeInt(nextTransitionEndsGarminTs); + writer.writeInt(nextTransitionStartsGarminTs); return true; } }