pebble
  • Tutorials
  • Get the SDK
  • Guides
  • Documentation
  • PebbleOS
  • Mobile App
  • Examples
  • Index 01
  • Community
  • Blog
  • More
Privacy
Cookies
Publish

PebbleOS

  • Overview
  • Start here
    • Getting started guide
    • Playground
  • Development
    • The pbl CLI
    • Build system
    • Samples
      • Threads
      • Mutexes
      • Semaphores
      • Message queues
      • Poll groups
      • Timers
      • Work queues
      • Delayed work
      • Heaps
      • Time
      • Dining philosophers
      • Bounded buffer
      • Readers and writers
      • Sleeping barber
      • Priority inversion
      • Barrier
      • Input events
      • Accelerometer
      • Ambient light
      • Backlight
      • Battery
      • Display
      • Flash
      • Magnetometer
      • Microphone
      • External RAM
      • Random numbers
      • Real-time clock
      • Speaker
      • Die temperature
      • Touchscreen
      • Vibration
      • Kernel benchmark
    • Emulator and simulator
    • Testing
    • Debugging
    • Conventions
  • System
    • Architecture overview
    • Kernel
    • Subsystems
    • Drivers
    • Services
    • Libraries
    • Third-party
    • Application SDK
  • Hardware
    • Boards
    • Programmers
  • Community
    • Contribution Guidelines
  • Reference
    • Reference
    • Kconfig options

Real-time clock

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      2

static 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. This page is maintained in the pebbleos repository: docs/samples/drivers/rtc.md.

Overview

  • Sources
  • Output