system-uptime

Crates.iosystem-uptime
lib.rssystem-uptime
version0.1.2
created_at2025-11-21 20:25:56.91525+00
updated_at2025-11-22 08:26:52.928158+00
descriptionA cross-platform Rust library for retrieving operating system uptime.
homepagehttps://github.com/greyshaman/system-uptime
repositoryhttps://github.com/greyshaman/system-uptime
max_upload_size
id1944206
size212,651
Sergey Reshetnikov (greyshaman)

documentation

README

system-uptime

system-uptime

Crates.io Docs.rs License: MIT

A cross-platform Rust library for retrieving operating system uptime.

Supported OS

  • Windows (via GetTickCount64)
  • Linux and Android (via /proc/uptime)
  • macOS, iOS, FreeBSD (via sysctl with KERN_BOOTTIME)

Installation

Add to your Cargo.toml:

[dependencies]
system-uptime = "0.1.2"

or

$ cargo add system-uptime

Usage

Basic Example

use system_uptime::get_os_uptime;

fn main() {
    match get_os_uptime() {
        Ok(uptime_ms) => println!("System has been running for {} milliseconds", uptime_ms),
        Err(e) => eprintln!("Error: {}", e),
    }
}

Getting Uptime as Duration

use system_uptime::get_os_uptime_duration;

fn main() {
    match get_os_uptime_duration() {
        Ok(duration) => {
            println!("Total uptime: {:?}", duration);
            println!("Seconds: {}", duration.as_secs());
            println!("Milliseconds: {}", duration.as_millis());
        }
        Err(e) => eprintln!("Error: {}", e),
    }
}

Formatting Uptime

use system_uptime::get_os_uptime_duration;
use std::time::Duration;

fn format_uptime(duration: Duration) -> String {
    let total_seconds = duration.as_secs();
    let days = total_seconds / 86400;
    let hours = (total_seconds % 86400) / 3600;
    let minutes = (total_seconds % 3600) / 60;
    let seconds = total_seconds % 60;

    if days > 0 {
        format!("{}d {}h {}m {}s", days, hours, minutes, seconds)
    } else if hours > 0 {
        format!("{}h {}m {}s", hours, minutes, seconds)
    } else if minutes > 0 {
        format!("{}m {}s", minutes, seconds)
    } else {
        format!("{}s", seconds)
    }
}

fn main() {
    if let Ok(duration) = get_os_uptime_duration() {
        println!("Uptime: {}", format_uptime(duration));
    }
}

API

get_os_uptime() -> Result<u64, Box<dyn Error>>

Returns system uptime in milliseconds.

Returns:

  • Ok(u64) - number of milliseconds since system boot

  • Err(Box<dyn Error>) - error retrieving uptime

get_os_uptime_duration() -> Result<Duration, Box<dyn Error>>

Returns system uptime as std::time::Duration.

Precision

  • Windows: precision ~10-16 milliseconds

  • Linux/Android: precision ~10 milliseconds (from /proc/uptime)

  • macOS/BSD: precision ~1 second (via sysctl)

Example Output

// On a system running for 2.5 hours:
System has been running for 9000000 milliseconds
Total uptime: 2.5h
Formatted: 2h 30m 0s

Dependencies

  • libc - for Unix-like systems

  • winapi - for Windows (automatically included only on Windows)

License

MIT License - see LICENSE file.

Contributing

Issues and Pull Requests are welcome!

Commit count: 0

cargo fmt