From a07edc7cf457b0245798464f34abc8a61a8d6666 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Wed, 12 Jan 2022 12:17:22 +0200 Subject: [PATCH] gst-plugin-version-helper: Work around broken file times in crates from crates.io Fixes https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs/-/issues/177 --- version-helper/Cargo.toml | 2 +- version-helper/src/lib.rs | 16 +++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/version-helper/Cargo.toml b/version-helper/Cargo.toml index 6ab41bf1..a6fd362a 100644 --- a/version-helper/Cargo.toml +++ b/version-helper/Cargo.toml @@ -13,4 +13,4 @@ edition = "2021" rust-version = "1.56" [dependencies] -chrono = { version = "0.4.19", default-features = false, features = ["std"] } +chrono = { version = "0.4.19", default-features = false, features = ["std", "clock"] } diff --git a/version-helper/src/lib.rs b/version-helper/src/lib.rs index a9abe1bb..6698f6a8 100644 --- a/version-helper/src/lib.rs +++ b/version-helper/src/lib.rs @@ -32,7 +32,8 @@ mod git; -use chrono::TimeZone; +use chrono::{Datelike, TimeZone}; +use std::convert::TryInto; use std::time::SystemTime; use std::{env, fs, path}; @@ -67,15 +68,15 @@ pub fn info() { // If there is a git repository, extract the version information from there. // Otherwise assume this is a release and use Cargo.toml mtime as date. let (commit_id, commit_date) = git_info.unwrap_or_else(|| { - let date = cargo_mtime_date(crate_dir).expect("Failed to get Cargo.toml mtime"); - ("RELEASE".into(), date) + let date = cargo_mtime_date(crate_dir).unwrap_or_else(chrono::Utc::now); + ("RELEASE".into(), date.format("%Y-%m-%d").to_string()) }); println!("cargo:rustc-env=COMMIT_ID={}", commit_id); println!("cargo:rustc-env=BUILD_REL_DATE={}", commit_date); } -fn cargo_mtime_date(crate_dir: path::PathBuf) -> Option { +fn cargo_mtime_date(crate_dir: path::PathBuf) -> Option> { let mut cargo_toml = crate_dir; cargo_toml.push("Cargo.toml"); @@ -84,5 +85,10 @@ fn cargo_mtime_date(crate_dir: path::PathBuf) -> Option { let unix_time = mtime.duration_since(SystemTime::UNIX_EPOCH).ok()?; let dt = chrono::Utc.timestamp(unix_time.as_secs().try_into().ok()?, 0); - Some(dt.format("%Y-%m-%d").to_string()) + // FIXME: Work around https://github.com/rust-lang/cargo/issues/10285 + if dt.date().year() < 2015 { + return None; + } + + Some(dt) }