diff --git a/infinitime.go b/infinitime.go index d92b189..e07f831 100644 --- a/infinitime.go +++ b/infinitime.go @@ -698,7 +698,9 @@ func (i *Device) WatchMotion(ctx context.Context) (<-chan MotionValues, error) { return out, nil } -// SetTime sets the watch's time using the Current Time Service +// SetTime sets the watch's +// * time using the Current Time Service's current time characteristic +// * timezone information using the CTS's local time characteristic func (i *Device) SetTime(t time.Time) error { if err := i.checkStatus(i.currentTimeChar, CurrentTimeChar); err != nil { return err @@ -713,24 +715,38 @@ func (i *Device) SetTime(t time.Time) error { binary.Write(buf, binary.LittleEndian, uint8(t.Weekday())) binary.Write(buf, binary.LittleEndian, uint8((t.Nanosecond()/1000)/1e6*256)) binary.Write(buf, binary.LittleEndian, uint8(0b0001)) - return i.currentTimeChar.WriteValue(buf.Bytes(), nil) -} - -// SetTimezone sets the watch's timezone information using the Local Time Service -func (i *Device) SetTimezone(t time.Time) error { - if err := i.checkStatus(i.localTimeChar, LocalTimeChar); err != nil { + if err := i.currentTimeChar.WriteValue(buf.Bytes(), nil); err != nil { return err } + + if err := i.checkStatus(i.localTimeChar, LocalTimeChar); err != nil { + // this characteristic might not be there in older versions of infinitime + // so we fail silently in this case. + _, notAvailable := err.(ErrCharNotAvail) + if notAvailable { + log.Warn().Msg("No Local Time Characteristic detected. Old Version of Infinitime (pre 1.10.0)?") + return nil + } else { + return err + } + } _, offset := t.Zone() dst := 0 + + // Local time expects two values: the timezone offset and the dst offset, both + // expressed in quarters of an hour. + // Timezone offset is to be constant over DST, with dst offset holding the offset != 0 + // when DST is in effect. + // As there is no standard way in go to get the actual dst offset, we assume it to be 1h + // when DST is in effect if t.IsDST() { dst = 3600 offset -= 3600 } - buf := &bytes.Buffer{} - binary.Write(buf, binary.LittleEndian, uint8(offset / 3600 * 4)) - binary.Write(buf, binary.LittleEndian, uint8(dst / 3600 * 4)) - return i.localTimeChar.WriteValue(buf.Bytes(), nil) + bufTz := &bytes.Buffer{} + binary.Write(bufTz, binary.LittleEndian, uint8(offset / 3600 * 4)) + binary.Write(bufTz, binary.LittleEndian, uint8(dst / 3600 * 4)) + return i.localTimeChar.WriteValue(bufTz.Bytes(), nil) } // Notify sends a notification to InfiniTime via