Skip to content
This repository was archived by the owner on Apr 20, 2026. It is now read-only.

Commit 15f08e1

Browse files
committed
Apply local UTC offset in Pebble RTC
Add pbl_rtc_tz_offset() to return a cached local-time offset (seconds). It honors the TZ_OFFSET_SEC environment variable and falls back to the host's localtime detection. pbl_rtc_get_time() now returns UTC plus that offset so the firmware sees a local wall-clock unix timestamp.
1 parent 5dff85f commit 15f08e1

1 file changed

Lines changed: 36 additions & 1 deletion

File tree

pebble/hw/misc/pebble_rtc.c

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
#include "hw/irq.h"
2222
#include "hw/sysbus.h"
2323

24+
#include <time.h>
25+
#include <stdlib.h>
26+
2427
#define TYPE_PEBBLE_GENERIC_RTC "pebble-rtc"
2528
OBJECT_DECLARE_SIMPLE_TYPE(PblRtc, PEBBLE_GENERIC_RTC)
2629

@@ -56,9 +59,41 @@ static void pbl_rtc_update_irq(PblRtc *s)
5659
(s->ctrl & CTRL_ALARM_IRQ));
5760
}
5861

62+
/* Return the local-time UTC offset in seconds, cached on first call.
63+
*
64+
* Honors the TZ_OFFSET_SEC environment variable (seconds east of UTC) when set
65+
* — this matches the stm32 Pebble RTC and is how the browser/JS build injects
66+
* the user's timezone. When unset, falls back to the host's detected local
67+
* offset via localtime_r(). */
68+
static int64_t pbl_rtc_tz_offset(void)
69+
{
70+
static int64_t offset_sec;
71+
static bool cached;
72+
73+
if (!cached) {
74+
const char *env = getenv("TZ_OFFSET_SEC");
75+
if (env) {
76+
offset_sec = (int64_t)atoll(env);
77+
} else {
78+
time_t now = time(NULL);
79+
struct tm lt;
80+
if (localtime_r(&now, &lt)) {
81+
offset_sec = (int64_t)lt.tm_gmtoff;
82+
}
83+
}
84+
cached = true;
85+
}
86+
return offset_sec;
87+
}
88+
89+
/* Return "local time as a unix timestamp". Pebble firmware stores wall-clock
90+
* time in this form, so the value read here is shifted by the local UTC
91+
* offset — a guest that applies no further timezone conversion will still
92+
* display the user's local wall-clock time. */
5993
static uint64_t pbl_rtc_get_time(void)
6094
{
61-
return (uint64_t)qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
95+
uint64_t utc = (uint64_t)qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
96+
return utc + pbl_rtc_tz_offset();
6297
}
6398

6499
static uint64_t pbl_rtc_read(void *opaque, hwaddr offset, unsigned size)

0 commit comments

Comments
 (0)