# system-uptime
<center><img src="https://raw.githubusercontent.com/greyshaman/system-uptime/refs/heads/main/images/photo_system-uptime.jpg" width="50%" alt="system-uptime"></center>
[](https://crates.io/crates/system-uptime)
[](https://docs.rs/system-uptime)
[](https://opensource.org/licenses/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`)
- ✅ **Redox** (via `libredox::call::clock_gettime()`)
## Installation
Add to your `Cargo.toml`:
```toml
[dependencies]
system-uptime = "0.2.0"
```
or
```bash
$ cargo add system-uptime
```
## Usage
### Basic Example
```rust
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
```rust
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
```rust
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)
- __Redox-OS__ precision ~10 milliseconds
## Example Output
```text
// 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)
- `libredox` - for Redox-OS
## License
MIT License - see LICENSE file.
## Contributing
Issues and Pull Requests are welcome!