59 lines
2.2 KiB
Rust
59 lines
2.2 KiB
Rust
//! Which build this is: "v0.0.1-36bc2be" (build.rs), and when it was built.
|
|
|
|
use std::time::SystemTime;
|
|
|
|
/// "v0.0.1-36bc2be", "v0.0.1-36bc2be-dirty" (built with uncommitted changes), or "v0.0.1".
|
|
pub const LABEL: &str = env!("ANYBOT_VERSION");
|
|
|
|
/// When the program file was last written (= when this build was made; a newer one replacing it is
|
|
/// how an update is noticed).
|
|
pub fn exe_modified() -> Option<SystemTime> {
|
|
std::env::current_exe().ok().and_then(|p| std::fs::metadata(p).ok()).and_then(|m| m.modified().ok())
|
|
}
|
|
|
|
/// "2026-11-03 20:57" in local time, or "?" when unknown.
|
|
pub fn time_label(t: Option<SystemTime>) -> String {
|
|
t.map(|t| chrono::DateTime::<chrono::Local>::from(t).format("%Y-%m-%d %H:%M").to_string()).unwrap_or_else(|| "?".into())
|
|
}
|
|
|
|
/// Milliseconds since 1970, for the phone: it shows the time in its own zone (a server's container
|
|
/// runs in UTC, so its own "local time" would be hours off for the person reading it).
|
|
pub fn epoch_ms(t: Option<SystemTime>) -> Option<u64> {
|
|
t.and_then(|t| t.duration_since(SystemTime::UNIX_EPOCH).ok()).map(|d| d.as_millis() as u64)
|
|
}
|
|
|
|
/// "v0.0.1-36bc2be · 2026-11-11 14:20": the version and when this build was made.
|
|
pub fn full(built: Option<SystemTime>) -> String {
|
|
match built {
|
|
Some(_) => format!("{LABEL} · {}", time_label(built)),
|
|
None => LABEL.to_string(),
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn label_is_v_number_dash_commit() {
|
|
let number = env!("CARGO_PKG_VERSION");
|
|
assert!(LABEL.starts_with(&format!("v{number}")), "{LABEL}");
|
|
let rest = &LABEL[1 + number.len()..];
|
|
// A commit, maybe "-dirty"; or nothing outside git.
|
|
assert!(rest.is_empty() || rest.starts_with('-'), "{LABEL}");
|
|
}
|
|
|
|
#[test]
|
|
fn full_adds_the_build_time() {
|
|
assert_eq!(full(None), LABEL);
|
|
let s = full(Some(SystemTime::UNIX_EPOCH));
|
|
assert!(s.starts_with(&format!("{LABEL} · ")) && s.len() > LABEL.len() + 10, "{s}");
|
|
}
|
|
|
|
#[test]
|
|
fn epoch_ms_for_the_phone() {
|
|
assert_eq!(epoch_ms(None), None);
|
|
assert_eq!(epoch_ms(Some(SystemTime::UNIX_EPOCH + std::time::Duration::from_millis(1500))), Some(1500));
|
|
}
|
|
}
|