| Crates.io | system-uptime |
| lib.rs | system-uptime |
| version | 0.1.2 |
| created_at | 2025-11-21 20:25:56.91525+00 |
| updated_at | 2025-11-22 08:26:52.928158+00 |
| description | A cross-platform Rust library for retrieving operating system uptime. |
| homepage | https://github.com/greyshaman/system-uptime |
| repository | https://github.com/greyshaman/system-uptime |
| max_upload_size | |
| id | 1944206 |
| size | 212,651 |

A cross-platform Rust library for retrieving operating system uptime.
GetTickCount64)/proc/uptime)sysctl with KERN_BOOTTIME)Add to your Cargo.toml:
[dependencies]
system-uptime = "0.1.2"
or
$ cargo add system-uptime
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),
}
}
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),
}
}
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));
}
}
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.
Windows: precision ~10-16 milliseconds
Linux/Android: precision ~10 milliseconds (from /proc/uptime)
macOS/BSD: precision ~1 second (via sysctl)
// On a system running for 2.5 hours:
System has been running for 9000000 milliseconds
Total uptime: 2.5h
Formatted: 2h 30m 0s
libc - for Unix-like systems
winapi - for Windows (automatically included only on Windows)
MIT License - see LICENSE file.
Issues and Pull Requests are welcome!