get system boot time in minutes in rust

To get the system boot time in minutes, we first need to get the system uptime and subtract it from the current time. The chrono crate can be used to get the current time and sysinfo can be used to get system uptime.

Here is a sample code to get the system boot time in minutes:

main.rs
extern crate chrono;
extern crate sysinfo;

use chrono::{Duration, Utc};
use sysinfo::{System, SystemExt};

fn main() {
    let system = System::new_all();
    let boot_time = Utc::now() - Duration::seconds(system.uptime() as i64);
    let minutes_since_boot = ((Utc::now() - boot_time).num_seconds() / 60) as f32;
    println!("System has been running for {:.2} minutes.", minutes_since_boot);
}
397 chars
13 lines

First, we create a new System object from the sysinfo crate. Then, we get the current time using Utc::now() and subtract the system uptime in seconds from the current time to get the boot time. We then subtract the boot time from the current time to get the duration since boot, convert it to minutes, and print the result.

Note that the output will be a float with two decimal places, so we need to cast the result of the division to a float.

gistlibby LogSnag