compound_duration
Convert seconds/nanoseconds to human-readable compound duration format.
Features
- Zero dependencies - Lightweight and fast
- Simple API - Just call the function with your duration
- Flexible input - Accepts any type convertible to
u64 - High performance - No allocations except for the output string
- Well tested - Comprehensive test suite
- 32-bit safe - Works correctly on both 32-bit and 64-bit architectures
Installation
Add this to your Cargo.toml:
[]
= "2.0"
Usage
Basic Examples
use ;
// Format seconds to days/hours/minutes/seconds
assert_eq!;
assert_eq!;
assert_eq!;
// Format seconds with weeks
assert_eq!;
assert_eq!;
// Format nanoseconds with sub-second precision
assert_eq!;
assert_eq!;
assert_eq!;
With std::time::Instant
use ;
use Instant;
let start = now;
// ... do some work ...
let elapsed = start.elapsed;
// Format elapsed time in seconds
println!;
// Format with nanosecond precision
println!;
With std::time::Duration
use format_dhms;
use Duration;
let duration = from_secs;
println!; // "1h1m1s"
Using Time Constants
use ;
// Calculate duration programmatically
let uptime = 2 * DAY + 3 * HOUR + 45 * MINUTE + 30;
println!; // "2d3h45m30s"
// Using constants for clarity
let backup_interval = 7 * DAY;
println!; // "7d"
Real-World Examples
use ;
use ;
// 1. HTTP Request Timeout
let timeout = from_secs;
println!; // "30s"
// 2. Cache Expiry
let cache_ttl = 3600;
println!; // "1h"
// 3. Session Duration
let session = 24 * 3600;
println!; // "1d"
// 4. Benchmark Results
let start = now;
// ... expensive operation ...
let elapsed = start.elapsed;
println!;
// 5. Uptime Display
let uptime_seconds = 1_234_567;
println!; // "2w14h56m7s"
// 6. Rate Limiting
let rate_window = 60;
println!; // "1m"
Different Input Types
use format_dhms;
// Works with various integer types
let seconds_u32: u32 = 3600;
let seconds_u64: u64 = 7200;
let seconds_i32: i32 = 1800;
let seconds_usize: usize = 900;
println!; // "1h"
println!; // "2h"
println!; // "30m"
println!; // "15m"
API Reference
format_dhms
Convert seconds to compound duration (days, hours, minutes, seconds).
Examples:
0→"0s"7259→"2h59s"86400→"1d"6_000_000→"69d10h40m"
format_wdhms
Convert seconds to compound duration (weeks, days, hours, minutes, seconds).
Examples:
604_800→"1w"6_000_000→"9w6d10h40m"4_294_967_295→"7101w3d6h28m15s"
format_ns
Convert nanoseconds to compound duration (days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds).
Examples:
1_000_000_000→"1s"3_000_129_723→"3s129µs723ns"1_500_000→"1ms500µs"
Constants
The library exports useful time constants:
pub const NS: u64 = 1; // Nanosecond
pub const US: u64 = 1_000; // Microsecond
pub const MS: u64 = 1_000_000; // Millisecond
pub const NANOS: u64 = 1_000_000_000; // Nanoseconds per second
pub const SECOND: u64 = 1;
pub const MINUTE: u64 = 60;
pub const HOUR: u64 = 3_600;
pub const DAY: u64 = 86_400;
pub const WEEK: u64 = 604_800;
Format Examples
| Input (seconds) | format_dhms |
format_wdhms |
|---|---|---|
| 0 | 0s |
0s |
| 30 | 30s |
30s |
| 61 | 1m1s |
1m1s |
| 3600 | 1h |
1h |
| 7259 | 2h59s |
2h59s |
| 86400 | 1d |
1d |
| 604800 | 7d |
1w |
| 6000000 | 69d10h40m |
9w6d10h40m |
Version 2.0 Breaking Changes
Version 2.0 introduces breaking changes to fix critical issues on 32-bit architectures:
- Updated to Rust 2024 edition (latest, won't be deprecated soon)
- All internal constants changed from
usizetou64 - Function signatures changed to use generic
TryInto<u64>instead of complex trait bounds
Migration Guide
Most code will work without changes. If you were explicitly casting to usize:
// Old (v1.x)
format_dhms;
// New (v2.0) - no cast needed!
format_dhms;
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.