Standardize error messages, formatting, and code style. Refine --help text for consistency. Update comments for clarity and tone alignment.

This commit is contained in:
2026-08-13 17:50:06 +02:00
parent e36360e18c
commit 1b947fec24
7 changed files with 123 additions and 121 deletions

View File

@@ -13,40 +13,43 @@ pub fn now() -> i64 {
/// (public domain, http://howardhinnant.github.io/date_algorithms.html) -
/// avoids pulling in a whole calendar/timezone crate for what's otherwise a
/// handful of integer operations.
pub fn fmt_timestamp(unix_secs: i64) -> String {
let days = unix_secs.div_euclid(86_400);
pub fn fmt_timestamp(unix_secs: i64) -> String
{
let days = unix_secs.div_euclid(86_400);
let secs_of_day = unix_secs.rem_euclid(86_400);
let (h, m, s) = (secs_of_day / 3600, (secs_of_day / 60) % 60, secs_of_day % 60);
let (h, m, s) = (secs_of_day / 3600, (secs_of_day / 60) % 60, secs_of_day % 60);
let z = days + 719_468;
let z = days + 719_468;
let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
let doe = (z - era * 146_097) as i64; // [0, 146096]
let doe = (z - era * 146_097) as i64; // [0, 146096]
let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365; // [0, 399]
let y = yoe + era * 400;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); // [0, 365]
let mp = (5 * doy + 2) / 153; // [0, 11]
let d = doy - (153 * mp + 2) / 5 + 1; // [1, 31]
let mp = (5 * doy + 2) / 153; // [0, 11]
let d = doy - (153 * mp + 2) / 5 + 1; // [1, 31]
let m_num = if mp < 10 { mp + 3 } else { mp - 9 }; // [1, 12]
let y = if m_num <= 2 { y + 1 } else { y };
format!("{y:04}-{m_num:02}-{d:02} {h:02}:{m:02}:{s:02} UTC")
}
/// Compact `1d 02h 03m 04s`-style duration, dropping leading zero units.
pub fn fmt_duration(secs: i64) -> String {
let secs = secs.max(0);
pub fn fmt_duration(secs: i64) -> String
{
let secs = secs.max(0);
let (d, rem) = (secs / 86_400, secs % 86_400);
let (h, rem) = (rem / 3600, rem % 3600);
let (m, s) = (rem / 60, rem % 60);
let (m, s) = (rem / 60, rem % 60);
if d > 0 {
format!("{d}d {h:02}h {m:02}m {s:02}s")
} else if h > 0 {
format!("{h}h {m:02}m {s:02}s")
} else if m > 0 {
format!("{m}m {s:02}s")
} else {
format!("{s}s")
match (d, h, m) {
(d, _, _) if d > 0 => format!("{d}d {h:02}h {m:02}m {s:02}s"),
(_, h, _) if h > 0 => format!("{h}h {m:02}m {s:02}s"),
(_, _, m) if m > 0 => format!("{m}m {s:02}s"),
_ => format!("{s}s"),
}
}