-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
54 lines (48 loc) · 2.21 KB
/
Copy pathbuild.rs
File metadata and controls
54 lines (48 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//! Stamps the binary with the short git commit and the build date, exposed to
//! the crate as the `RETSEND_GIT_COMMIT` / `RETSEND_BUILD_DATE` env vars (read
//! with `env!` in the About screen). The project URL comes straight from
//! Cargo's `CARGO_PKG_REPOSITORY`, so it isn't duplicated here.
use std::process::Command;
use std::time::{SystemTime, UNIX_EPOCH};
fn main() {
// Re-run when the checked-out commit moves: watch HEAD and the ref it
// points to (a commit rewrites the ref, not HEAD itself).
println!("cargo:rerun-if-changed=.git/HEAD");
if let Ok(head) = std::fs::read_to_string(".git/HEAD") {
if let Some(refname) = head.strip_prefix("ref: ").map(str::trim) {
println!("cargo:rerun-if-changed=.git/{refname}");
}
}
let commit = Command::new("git")
.args(["rev-parse", "--short", "HEAD"])
.output()
.ok()
.filter(|o| o.status.success())
.map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
.filter(|s| !s.is_empty())
.unwrap_or_else(|| "unknown".into());
println!("cargo:rustc-env=RETSEND_GIT_COMMIT={commit}");
println!("cargo:rustc-env=RETSEND_BUILD_DATE={}", build_date());
}
/// Today's UTC date as `YYYY-MM-DD`, dependency-free.
fn build_date() -> String {
let secs = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0);
let (y, m, d) = civil_from_days((secs / 86_400) as i64);
format!("{y:04}-{m:02}-{d:02}")
}
/// Days since the Unix epoch → (year, month, day) in UTC (Hinnant's algorithm).
fn civil_from_days(z: i64) -> (i64, u32, u32) {
let z = z + 719_468;
let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
let doe = (z - era * 146_097) as u64; // day-of-era, [0, 146096]
let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146_096) / 365; // [0, 399]
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); // day-of-year, [0, 365]
let mp = (5 * doy + 2) / 153; // month shifted so March = 0, [0, 11]
let d = (doy - (153 * mp + 2) / 5 + 1) as u32; // [1, 31]
let m = if mp < 10 { mp + 3 } else { mp - 9 } as u32; // [1, 12]
let y = yoe as i64 + era * 400 + if m <= 2 { 1 } else { 0 };
(y, m, d)
}