1 unstable release

Uses old Rust 2015

0.2.0 Feb 20, 2018

#805 in Unix APIs

Download history 237/week @ 2024-11-16 284/week @ 2024-11-23 300/week @ 2024-11-30 246/week @ 2024-12-07 277/week @ 2024-12-14 117/week @ 2024-12-21 131/week @ 2024-12-28 195/week @ 2025-01-04 264/week @ 2025-01-11 270/week @ 2025-01-18 326/week @ 2025-01-25 210/week @ 2025-02-01 305/week @ 2025-02-08 222/week @ 2025-02-15 235/week @ 2025-02-22 238/week @ 2025-03-01

1,028 downloads per month

MIT license

8KB
115 lines

hwclock-rs

The hwclock module provides a thin wrapper around kernel structs and ioctls to retrieve the current time from the hardware clock and convert it from and to a valid chrono data structure.

Example:

extern crate chrono;
extern crate hwclock;

fn main() {
   use hwclock::HwClockDev;

   let rtc = HwClockDev::open("/dev/rtc0").expect("could not open rtc clock");

   println!("{:?}", rtc);

   let time = rtc.get_time().expect("could not read rtc clock");
   println!("{:?}", time);

   println!("Setting clock ahead 30 seconds");
   let mut ct: chrono::NaiveDateTime = time.into();
   ct += chrono::Duration::seconds(30);

   // convert back to RtcTime and set it
   let ntime = ct.into();
   rtc.set_time(&ntime).expect("could not set rtc clock");

   println!("Rereading...");
   let time2 = rtc.get_time().expect("could not read rtc clock");

   println!("{:?}", time2);
}

lib.rs:

Hardware clock handling for linux

The hwclock module provides a thin wrapper around kernel structs and ioctls to retrieve the current time from the hardware clock and convert it from and to a valid chrono data structure.

extern crate chrono;
extern crate hwclock;

fn main() {
    use hwclock::HwClockDev;

    let rtc = HwClockDev::open("/dev/rtc0").expect("could not open rtc clock");

    println!("{:?}", rtc);

    let time = rtc.get_time().expect("could not read rtc clock");
    println!("{:?}", time);

    println!("Setting clock ahead 30 seconds");
    let mut ct: chrono::NaiveDateTime = time.into();
    ct += chrono::Duration::seconds(30);

    // convert back to RtcTime and set it
    let ntime = ct.into();
    rtc.set_time(&ntime).expect("could not set rtc clock");

    println!("Rereading...");
    let time2 = rtc.get_time().expect("could not read rtc clock");

    println!("{:?}", time2);
}

Dependencies

~3MB
~60K SLoC