| Crates.io | compound_duration |
| lib.rs | compound_duration |
| version | 2.0.0 |
| created_at | 2019-06-15 16:58:38.971591+00 |
| updated_at | 2025-11-05 12:53:59.525589+00 |
| description | Convert seconds to compound duration (week, days, hours, minutes, seconds) |
| homepage | https://github.com/nbari/compound_duration |
| repository | https://github.com/nbari/compound_duration |
| max_upload_size | |
| id | 141373 |
| size | 33,526 |
Convert seconds/nanoseconds to human-readable compound duration format.
u64Add this to your Cargo.toml:
[dependencies]
compound_duration = "2.0"
use compound_duration::{format_dhms, format_wdhms, format_ns};
// Format seconds to days/hours/minutes/seconds
assert_eq!(format_dhms(7259), "2h59s");
assert_eq!(format_dhms(86400), "1d");
assert_eq!(format_dhms(6_000_000), "69d10h40m");
// Format seconds with weeks
assert_eq!(format_wdhms(604_800), "1w");
assert_eq!(format_wdhms(6_000_000), "9w6d10h40m");
// Format nanoseconds with sub-second precision
assert_eq!(format_ns(3_000_129_723_u64), "3s129µs723ns");
assert_eq!(format_ns(1_000_000_000), "1s");
assert_eq!(format_ns(1_500_000), "1ms500µs");
use compound_duration::{format_dhms, format_ns};
use std::time::Instant;
let start = Instant::now();
// ... do some work ...
let elapsed = start.elapsed();
// Format elapsed time in seconds
println!("Elapsed: {}", format_dhms(elapsed.as_secs()));
// Format with nanosecond precision
println!("Precise: {}", format_ns(elapsed.as_nanos()));
use compound_duration::format_dhms;
use std::time::Duration;
let duration = Duration::from_secs(3661);
println!("{}", format_dhms(duration.as_secs())); // "1h1m1s"
use compound_duration::{format_dhms, DAY, HOUR, MINUTE};
// Calculate duration programmatically
let uptime = 2 * DAY + 3 * HOUR + 45 * MINUTE + 30;
println!("Server uptime: {}", format_dhms(uptime)); // "2d3h45m30s"
// Using constants for clarity
let backup_interval = 7 * DAY;
println!("Backup every: {}", format_dhms(backup_interval)); // "7d"
use compound_duration::{format_dhms, format_wdhms, format_ns};
use std::time::{Duration, Instant};
// 1. HTTP Request Timeout
let timeout = Duration::from_secs(30);
println!("Timeout: {}", format_dhms(timeout.as_secs())); // "30s"
// 2. Cache Expiry
let cache_ttl = 3600;
println!("Cache TTL: {}", format_dhms(cache_ttl)); // "1h"
// 3. Session Duration
let session = 24 * 3600;
println!("Session expires in: {}", format_dhms(session)); // "1d"
// 4. Benchmark Results
let start = Instant::now();
// ... expensive operation ...
let elapsed = start.elapsed();
println!("Operation took: {}", format_ns(elapsed.as_nanos()));
// 5. Uptime Display
let uptime_seconds = 1_234_567;
println!("System uptime: {}", format_wdhms(uptime_seconds)); // "2w14h56m7s"
// 6. Rate Limiting
let rate_window = 60;
println!("Rate limit window: {}", format_dhms(rate_window)); // "1m"
use compound_duration::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!("{}", format_dhms(seconds_u32)); // "1h"
println!("{}", format_dhms(seconds_u64)); // "2h"
println!("{}", format_dhms(seconds_i32)); // "30m"
println!("{}", format_dhms(seconds_usize)); // "15m"
format_dhmsConvert seconds to compound duration (days, hours, minutes, seconds).
pub fn format_dhms<T>(seconds: T) -> String
where
T: TryInto<u64>,
T::Error: std::fmt::Debug,
Examples:
0 → "0s"7259 → "2h59s"86400 → "1d"6_000_000 → "69d10h40m"format_wdhmsConvert seconds to compound duration (weeks, days, hours, minutes, seconds).
pub fn format_wdhms<T>(seconds: T) -> String
where
T: TryInto<u64>,
T::Error: std::fmt::Debug,
Examples:
604_800 → "1w"6_000_000 → "9w6d10h40m"4_294_967_295 → "7101w3d6h28m15s"format_nsConvert nanoseconds to compound duration (days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds).
pub fn format_ns<T>(nanos: T) -> String
where
T: TryInto<u64>,
T::Error: std::fmt::Debug,
Examples:
1_000_000_000 → "1s"3_000_129_723 → "3s129µs723ns"1_500_000 → "1ms500µs"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;
| 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 introduces breaking changes to fix critical issues on 32-bit architectures:
usize to u64TryInto<u64> instead of complex trait boundsMost code will work without changes. If you were explicitly casting to usize:
// Old (v1.x)
format_dhms(duration.as_secs() as usize);
// New (v2.0) - no cast needed!
format_dhms(duration.as_secs());
Contributions are welcome! Please feel free to submit a Pull Request.