1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-15 22:49:26 +01:00

Garmin: Add DST/Timezone support

This commit is contained in:
Daniele Gobbetti 2024-04-19 16:45:58 +02:00
parent 69934b2d7c
commit 2c70f1a616

View File

@ -1,7 +1,11 @@
package nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.messages; package nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.messages;
import java.util.Calendar; import org.threeten.bp.Instant;
import org.threeten.bp.ZoneId;
import org.threeten.bp.zone.ZoneOffsetTransition;
import org.threeten.bp.zone.ZoneRules;
import java.util.TimeZone; import java.util.TimeZone;
import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.GarminTimeUtils; import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.GarminTimeUtils;
@ -23,13 +27,17 @@ public class CurrentTimeRequestMessage extends GFDIMessage {
@Override @Override
protected boolean generateOutgoing() { protected boolean generateOutgoing() {
long now = System.currentTimeMillis();
final TimeZone timeZone = TimeZone.getDefault(); final Instant now = Instant.now();
final Calendar calendar = Calendar.getInstance(timeZone); final ZoneRules zoneRules = ZoneId.systemDefault().getRules();
calendar.setTimeInMillis(now); final int dstOffset = (int) zoneRules.getDaylightSavings(now).getSeconds();
int dstOffset = calendar.get(Calendar.DST_OFFSET) / 1000; final int timeZoneOffset = TimeZone.getDefault().getOffset(now.toEpochMilli()) / 1000;
int timeZoneOffset = timeZone.getOffset(now) / 1000; final int garminTimestamp = GarminTimeUtils.unixTimeToGarminTimestamp((int) now.getEpochSecond());
int garminTimestamp = GarminTimeUtils.javaMillisToGarminTimestamp(now); final ZoneOffsetTransition nextTransitionStart = zoneRules.nextTransition(now);
final int nextTransitionStartsTs = (int) nextTransitionStart.toEpochSecond();
final int nextTransitionEndsTs = (int) zoneRules.nextTransition(nextTransitionStart.getInstant()).toEpochSecond();
LOG.info("Processing current time request #{}: time={}, DST={}, ofs={}", referenceID, garminTimestamp, dstOffset, timeZoneOffset); LOG.info("Processing current time request #{}: time={}, DST={}, ofs={}", referenceID, garminTimestamp, dstOffset, timeZoneOffset);
@ -41,10 +49,8 @@ public class CurrentTimeRequestMessage extends GFDIMessage {
writer.writeInt(referenceID); writer.writeInt(referenceID);
writer.writeInt(garminTimestamp); writer.writeInt(garminTimestamp);
writer.writeInt(timeZoneOffset); writer.writeInt(timeZoneOffset);
// TODO: next DST start/end writer.writeInt(GarminTimeUtils.unixTimeToGarminTimestamp(nextTransitionEndsTs));
writer.writeInt(0); writer.writeInt(GarminTimeUtils.unixTimeToGarminTimestamp(nextTransitionStartsTs));
writer.writeInt(0);
return true; return true;
} }
} }