From 229adf8b2e4c02e6d00e08a0431210f1f13b02e7 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Mon, 18 Jul 2022 19:02:20 +0800 Subject: [PATCH 1/3] fix #103 --- example_shadow/src/main.rs | 5 ++++- src/{time.rs => data_time.rs} | 30 +++++++++++++++--------------- src/env.rs | 2 +- src/git.rs | 4 ++-- src/lib.rs | 8 ++++---- 5 files changed, 26 insertions(+), 23 deletions(-) rename src/{time.rs => data_time.rs} (77%) diff --git a/example_shadow/src/main.rs b/example_shadow/src/main.rs index 3e00664..4285154 100644 --- a/example_shadow/src/main.rs +++ b/example_shadow/src/main.rs @@ -1,10 +1,13 @@ use clap::Command; -use shadow_rs::shadow; +use shadow_rs::{shadow, Format}; shadow!(build); fn main() { + let local_time = shadow_rs::DateTime::new().human_format(); + println!("{}", local_time); + Command::new("example_shadow") .version(build::CLAP_LONG_VERSION) .get_matches(); //USAGE: ./example_shadow -V diff --git a/src/time.rs b/src/data_time.rs similarity index 77% rename from src/time.rs rename to src/data_time.rs index 05de6fc..da05c78 100644 --- a/src/time.rs +++ b/src/data_time.rs @@ -6,19 +6,19 @@ use time::UtcOffset; use time::{format_description, OffsetDateTime}; use tz::TimeZone; -pub enum BuildTime { +pub enum DateTime { Local(OffsetDateTime), Utc(OffsetDateTime), } -pub fn now_data_time() -> BuildTime { +pub fn now_data_time() -> DateTime { // Enable reproducibility for uses of `now_data_time` by respecting the // `SOURCE_DATE_EPOCH` env variable. // // https://reproducible-builds.org/docs/source-date-epoch/ println!("cargo:rerun-if-env-changed=SOURCE_DATE_EPOCH"); match std::env::var_os("SOURCE_DATE_EPOCH") { - None => BuildTime::new(), + None => DateTime::new(), Some(timestamp) => { let epoch = timestamp .into_string() @@ -26,17 +26,17 @@ pub fn now_data_time() -> BuildTime { .parse::() .expect("Input SOURCE_DATE_EPOCH could not be cast to a number"); // BuildTime::Utc(Utc.timestamp(epoch, 0)) - BuildTime::Utc(OffsetDateTime::from_unix_timestamp(epoch).unwrap()) + DateTime::Utc(OffsetDateTime::from_unix_timestamp(epoch).unwrap()) } } } -impl BuildTime { +impl DateTime { pub fn new() -> Self { - Self::local_now().unwrap_or_else(|_| BuildTime::Utc(OffsetDateTime::now_utc())) + Self::local_now().unwrap_or_else(|_| DateTime::Utc(OffsetDateTime::now_utc())) } - fn local_now() -> Result> { + pub fn local_now() -> Result> { let time_zone_local = TimeZone::local()?; // expensive call, should be cached let duration_since_epoch = SystemTime::now().duration_since(UNIX_EPOCH)?; @@ -45,28 +45,28 @@ impl BuildTime { let time_zone_offset = UtcOffset::from_whole_seconds(local_time_type.ut_offset())?; let local_date_time = (OffsetDateTime::UNIX_EPOCH + duration_since_epoch).to_offset(time_zone_offset); - Ok(BuildTime::Local(local_date_time)) + Ok(DateTime::Local(local_date_time)) } pub fn timestamp_2_utc(time_stamp: i64) -> Self { let time = OffsetDateTime::from_unix_timestamp(time_stamp).unwrap(); - BuildTime::Utc(time) + DateTime::Utc(time) } pub fn to_rfc2822(&self) -> String { match self { - BuildTime::Local(dt) | BuildTime::Utc(dt) => dt.format(&Rfc2822).unwrap(), + DateTime::Local(dt) | DateTime::Utc(dt) => dt.format(&Rfc2822).unwrap(), } } pub fn to_rfc3339(&self) -> String { match self { - BuildTime::Local(dt) | BuildTime::Utc(dt) => dt.format(&Rfc3339).unwrap(), + DateTime::Local(dt) | DateTime::Utc(dt) => dt.format(&Rfc3339).unwrap(), } } } -impl Format for BuildTime { +impl Format for DateTime { fn human_format(&self) -> String { let fmt = format_description::parse( "[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour \ @@ -74,7 +74,7 @@ impl Format for BuildTime { ) .unwrap(); match self { - BuildTime::Local(dt) | BuildTime::Utc(dt) => dt.format(&fmt).unwrap(), + DateTime::Local(dt) | DateTime::Utc(dt) => dt.format(&fmt).unwrap(), } } } @@ -92,7 +92,7 @@ mod tests { #[test] fn test_local_now_human_format() { - let time = BuildTime::local_now().unwrap().human_format(); + let time = DateTime::local_now().unwrap().human_format(); #[cfg(unix)] assert!(!std::fs::read("/etc/localtime").unwrap().is_empty()); @@ -102,7 +102,7 @@ mod tests { #[test] fn test_timestamp_2_utc() { - let time = BuildTime::timestamp_2_utc(1628080443); + let time = DateTime::timestamp_2_utc(1628080443); assert_eq!(time.to_rfc2822(), "Wed, 04 Aug 2021 12:34:03 +0000"); assert_eq!(time.to_rfc3339(), "2021-08-04T12:34:03Z"); assert_eq!(time.human_format(), "2021-08-04 12:34:03 +00:00"); diff --git a/src/env.rs b/src/env.rs index 7ac8965..f85b6d9 100644 --- a/src/env.rs +++ b/src/env.rs @@ -1,7 +1,7 @@ use crate::build::*; +use crate::data_time::now_data_time; use crate::env::dep_source_replace::filter_cargo_tree; use crate::err::SdResult; -use crate::time::now_data_time; use crate::Format; use is_debug::build_channel; use std::collections::BTreeMap; diff --git a/src/git.rs b/src/git.rs index be92c40..6651f6c 100644 --- a/src/git.rs +++ b/src/git.rs @@ -1,7 +1,7 @@ use crate::build::{ConstType, ConstVal, ShadowConst}; use crate::ci::CiType; +use crate::data_time::DateTime; use crate::err::*; -use crate::time::BuildTime; use crate::Format; use std::collections::BTreeMap; use std::io::{BufReader, Read}; @@ -106,7 +106,7 @@ impl Git { let commit = reference.peel_to_commit().map_err(ShadowError::new)?; let time_stamp = commit.time().seconds().to_string().parse::()?; - let date_time = BuildTime::timestamp_2_utc(time_stamp); + let date_time = DateTime::timestamp_2_utc(time_stamp); self.update_str(COMMIT_DATE, date_time.human_format()); self.update_str(COMMIT_DATE_2822, date_time.to_rfc2822()); diff --git a/src/lib.rs b/src/lib.rs index 8350b46..632f21d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -145,11 +145,11 @@ mod build; mod ci; +mod data_time; mod env; mod err; mod gen_const; mod git; -mod time; /// Get current project build mode. /// @@ -167,7 +167,7 @@ use env::*; use git::*; use crate::ci::CiType; -use crate::time::BuildTime; +pub use crate::data_time::DateTime; pub use const_format::*; use std::collections::BTreeMap; use std::env as std_env; @@ -183,7 +183,7 @@ use crate::gen_const::{ pub use err::{SdResult, ShadowError}; pub use git::{branch, git_clean, git_status_file, tag}; -trait Format { +pub trait Format { fn human_format(&self) -> String; } @@ -371,7 +371,7 @@ impl Shadow { // Author by:https://www.github.com/baoyachi // The build script repository:https://github.com/baoyachi/shadow-rs // Create time by:{}"#, - BuildTime::new().human_format() + DateTime::new().human_format() ); writeln!(&self.f, "{}\n\n", desc)?; Ok(()) From 233275cd1a42e268e3c0d583bf3f59dd6f211dae Mon Sep 17 00:00:00 2001 From: baoyachi Date: Mon, 18 Jul 2022 19:04:01 +0800 Subject: [PATCH 2/3] upgrade shadow-rs#0.14.0 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 0dc0489..4a55cae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "shadow-rs" -version = "0.13.1" +version = "0.14.0" authors = ["baoyachi "] edition = "2021" description = "A build-time information stored in your rust project" From dd6fe5e7c017ac0e23e9be25bfdc5703c3c2eb3a Mon Sep 17 00:00:00 2001 From: baoyachi Date: Mon, 18 Jul 2022 19:06:02 +0800 Subject: [PATCH 3/3] fix clippy warning --- src/data_time.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/data_time.rs b/src/data_time.rs index da05c78..4c33252 100644 --- a/src/data_time.rs +++ b/src/data_time.rs @@ -31,6 +31,12 @@ pub fn now_data_time() -> DateTime { } } +impl Default for DateTime { + fn default() -> Self { + Self::new() + } +} + impl DateTime { pub fn new() -> Self { Self::local_now().unwrap_or_else(|_| DateTime::Utc(OffsetDateTime::now_utc()))