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
      • Idle states
      • Dining philosophers
      • Bounded buffer
      • Readers and writers
      • Sleeping barber
      • Priority inversion
      • Barrier
      • CRC
      • SHA-256
      • AES
      • Files
      • Settings
      • Translations
      • Input events
      • Loader
      • Log levels
      • Profiler
      • Shell commands
      • Task watchdog
      • Accelerometer
      • Ambient light
      • Backlight
      • Battery
      • Display
      • Flash
      • GPU
      • Magnetometer
      • Microphone
      • External RAM
      • Random numbers
      • Real-time clock
      • Speaker
      • Die temperature
      • Touchscreen
      • Vibration
      • Rust
      • 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

Time in Rust

The time sample written in Rust, on the pebbleos crate.

Durations are core::time::Duration, uptime is an Instant, and time::poll() waits for a condition with a time limit.

Run:

pbl samples samples/rust/kernel/time

Boards:

every board

Sources

src/lib.rs
#![no_std]

use core::time::Duration;

use pebbleos::println;
use pebbleos::thread;
use pebbleos::time::{self, Instant};

// 2026-01-01 00:00:00 UTC
const NEW_YEAR: i64 = 1767225600;

#[pebbleos::main]
fn main() {
    // Uptime only moves forward: use it to measure and to time out.
    let start = Instant::now();
    thread::sleep(Duration::from_millis(50));
    println!(
        "time: slept 50 ms, uptime moved by at least 50 ms: {}",
        yes_no(start.elapsed() >= Duration::from_millis(50))
    );

    // The wall clock follows the uptime, and jumps when it is set.
    time::set_realtime(NEW_YEAR);
    thread::sleep(Duration::from_secs(1));
    println!(
        "time: one second after it was set, the wall clock moved by {} s",
        time::realtime() - NEW_YEAR
    );

    // Waiting for a condition, with the time limit every such wait needs.
    let mut polls = 0;
    let ret = time::poll(Duration::from_millis(5), Duration::from_millis(100), || {
        polls += 1;
        polls >= 3
    });
    println!("time: device ready: {}", yes_no(ret.is_ok()));

    let ret = time::poll(Duration::from_millis(5), Duration::from_millis(20), || {
        false
    });
    println!("time: a condition that never holds returns {ret:?}");

    println!("Sample done");
}

fn yes_no(b: bool) -> &'static str {
    if b { "yes" } else { "no" }
}

Output

time: slept 50 ms, uptime moved by at least 50 ms: yes
time: one second after it was set, the wall clock moved by \d s
time: device ready: yes
time: a condition that never holds returns Err\(ETIMEDOUT\)
Sample done

Generated from PebbleOS 7753e801d. This page is maintained in the pebbleos repository: docs/samples/rust/kernel/time.md.

Overview

  • Sources
  • Output