1 unstable release

Uses old Rust 2015

0.2.0 Feb 20, 2018

#27 in #ioctl

Download history 157/week @ 2024-07-23 121/week @ 2024-07-30 128/week @ 2024-08-06 128/week @ 2024-08-13 136/week @ 2024-08-20 215/week @ 2024-08-27 178/week @ 2024-09-03 311/week @ 2024-09-10 190/week @ 2024-09-17 227/week @ 2024-09-24 196/week @ 2024-10-01 222/week @ 2024-10-08 125/week @ 2024-10-15 177/week @ 2024-10-22 234/week @ 2024-10-29 191/week @ 2024-11-05

754 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
~59K SLoC