aboutsummaryrefslogtreecommitdiff
path: root/source/util/Clock.mc
diff options
context:
space:
mode:
Diffstat (limited to 'source/util/Clock.mc')
-rw-r--r--source/util/Clock.mc36
1 files changed, 36 insertions, 0 deletions
diff --git a/source/util/Clock.mc b/source/util/Clock.mc
new file mode 100644
index 0000000..4e2faf4
--- /dev/null
+++ b/source/util/Clock.mc
@@ -0,0 +1,36 @@
+// Author: cirakg & claude ai
+// Summary: UTC time for TOTP: the 2038-safe widening of the watch clock, the user's offset, and the trusted clock.
+//
+// Connect IQ stores time as a 32-bit signed integer, which turns negative on
+// 2038-01-19. Reading it as unsigned keeps it correct until 2106 (README 9).
+// The trusted clock (Time.CURRENT_TIME_RTC, API 3.0.10) is synced from GPS and
+// can't be changed by the user; the UI warns when it is unsynced or differs
+// from the normal clock.
+
+import Toybox.Lang;
+import Toybox.Time;
+
+module Clock {
+
+ const TWO_POW_32 = 4294967296l;
+
+ // A 32-bit clock value read as unsigned.
+ function toUnsigned(value as Number) as Long {
+ var v = value.toLong();
+ return v < 0 ? v + TWO_POW_32 : v;
+ }
+
+ // Current unix time (UTC seconds) plus the user's offset for clock skew.
+ function nowUnix(offsetSeconds as Number) as Long {
+ return toUnsigned(Time.now().value()) + offsetSeconds;
+ }
+
+ // The trusted clock's unix time, or null when it isn't synced yet.
+ function trustedUnix() as Long? {
+ try {
+ return toUnsigned(Time.getCurrentTime({:currentTimeType => Time.CURRENT_TIME_RTC}).value());
+ } catch (e instanceof Time.RealTimeClockNotValidException) {
+ return null;
+ }
+ }
+}