---
title: "Real-time clock"
canonical_url: https://developer.repebble.com/pebbleos/docs/samples/drivers/rtc/
last_updated: 2026-11-17
sdk_version: 4.44
---
# Real-time clock

Source: https://developer.repebble.com/pebbleos/docs/samples/drivers/rtc/

This sample reads the board’s RTC, sets it to a fixed date, waits two seconds to see it count on, then puts the time back, plus the time it spent on the sample date. An RTC counts UTC seconds since the epoch on its own, through resets; the kernel keeps the running wall clock, which the firmware’s clock service sets from the RTC at boot.

On a watch, the first line shows the time it last kept. The emulator starts its RTC at the host’s local time.

Run:

`pbl samples samples/drivers/rtc`

Boards:

every board

## Sources

src/main.c

```
#include \<stdio.h\>#include \<time.h\>#include \<pbl/drivers/rtc.h\>#include \<pbl/kernel/delay.h\>/\* 2026-01-01 00:00:00 UTC \*/#define SAMPLE\_TIME ((time\_t)1767225600)#define WAIT\_S 2static void prv\_log\_time(const char \*what, time\_t t) {struct tm tm;char buf[32];gmtime\_r(&t, &tm);strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tm);printf("rtc: %s %s UTC\n", what, buf);}int main(void) {time\_t saved, now;int ret;/\* The RTC counts UTC seconds since the epoch, through resets. \*/ret = pbl\_rtc\_get\_time(RTC, &saved);if (ret != 0) {fprintf(stderr, "rtc: read failed (%d)\n", ret);return ret;}prv\_log\_time("now", saved);ret = pbl\_rtc\_set\_time(RTC, SAMPLE\_TIME);if (ret != 0) {fprintf(stderr, "rtc: write failed (%d)\n", ret);return ret;}prv\_log\_time("set to", SAMPLE\_TIME);pbl\_sleep\_ms(WAIT\_S \* 1000);ret = pbl\_rtc\_get\_time(RTC, &now);if (ret != 0) {fprintf(stderr, "rtc: read failed (%d)\n", ret);return ret;}prv\_log\_time("read back", now);printf("rtc: %d s later it counted on: %s\n", WAIT\_S,now \>= SAMPLE\_TIME + WAIT\_S - 1 && now \<= SAMPLE\_TIME + WAIT\_S + 1 ? "yes" : "no");/\* Put the time back, plus the time it spent on the sample date. \*/ret = pbl\_rtc\_set\_time(RTC, saved + (now - SAMPLE\_TIME));if (ret != 0) {fprintf(stderr, "rtc: write failed (%d)\n", ret);return ret;}printf("rtc: restored\n");printf("Sample done\n");return 0;}
```

## Output

```
rtc: now \d+-\d+-\d+ \d+:\d+:\d+ UTC
rtc: set to 2026-01-01 00:00:00 UTC
rtc: read back 2026-01-01 00:00:0\d UTC
rtc: 2 s later it counted on: yes
rtc: restored
Sample done
```

Generated from PebbleOS [9e782207f](https://github.com/coredevices/pebbleos/commit/9e782207f53f8f1c02f7b13ea40cdc092d7bab3d). This page is maintained in the pebbleos repository: [docs/samples/drivers/rtc.md](https://github.com/coredevices/pebbleos/blob/9e782207f53f8f1c02f7b13ea40cdc092d7bab3d/docs/samples/drivers/rtc.md).
