From dd7a8849ca157e9cfefd1e071ba3cb05339c69da Mon Sep 17 00:00:00 2001 From: baoyachi Date: Tue, 19 Jul 2022 10:53:46 +0800 Subject: [PATCH 1/3] fix #104 --- src/data_time.rs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/data_time.rs b/src/data_time.rs index 4c33252..de885cf 100644 --- a/src/data_time.rs +++ b/src/data_time.rs @@ -42,6 +42,13 @@ impl DateTime { Self::local_now().unwrap_or_else(|_| DateTime::Utc(OffsetDateTime::now_utc())) } + pub fn offset_datetime() -> OffsetDateTime { + let date_time = Self::new(); + match date_time { + DateTime::Local(time) | DateTime::Utc(time) => time, + } + } + pub fn local_now() -> Result> { let time_zone_local = TimeZone::local()?; // expensive call, should be cached @@ -73,15 +80,20 @@ impl DateTime { } impl Format for DateTime { + fn human_format(&self) -> String { + match self { + DateTime::Local(dt) | DateTime::Utc(dt) => dt.human_format(), + } + } +} + +impl Format for OffsetDateTime { fn human_format(&self) -> String { let fmt = format_description::parse( "[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour \ sign:mandatory]:[offset_minute]", - ) - .unwrap(); - match self { - DateTime::Local(dt) | DateTime::Utc(dt) => dt.format(&fmt).unwrap(), - } + ).unwrap(); + self.format(&fmt).unwrap() } } From 8552641f7bf2e1a1032eefcc94c3467426d93069 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Tue, 19 Jul 2022 10:56:43 +0800 Subject: [PATCH 2/3] fix #105 --- Cargo.toml | 2 +- src/data_time.rs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4a55cae..3445e9b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "shadow-rs" -version = "0.14.0" +version = "0.14.1" authors = ["baoyachi "] edition = "2021" description = "A build-time information stored in your rust project" diff --git a/src/data_time.rs b/src/data_time.rs index de885cf..cd12aa5 100644 --- a/src/data_time.rs +++ b/src/data_time.rs @@ -92,7 +92,8 @@ impl Format for OffsetDateTime { let fmt = format_description::parse( "[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour \ sign:mandatory]:[offset_minute]", - ).unwrap(); + ) + .unwrap(); self.format(&fmt).unwrap() } } From 143fcacf596bd8a5acbe518117987f7fed69cc81 Mon Sep 17 00:00:00 2001 From: baoyachi Date: Tue, 19 Jul 2022 11:02:13 +0800 Subject: [PATCH 3/3] fix #105 --- Cargo.toml | 2 +- example_shadow/src/main.rs | 2 +- src/data_time.rs | 8 ++++---- src/lib.rs | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3445e9b..b2df766 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "shadow-rs" -version = "0.14.1" +version = "0.15.0" authors = ["baoyachi "] edition = "2021" description = "A build-time information stored in your rust project" diff --git a/example_shadow/src/main.rs b/example_shadow/src/main.rs index 4285154..0e244de 100644 --- a/example_shadow/src/main.rs +++ b/example_shadow/src/main.rs @@ -5,7 +5,7 @@ use shadow_rs::{shadow, Format}; shadow!(build); fn main() { - let local_time = shadow_rs::DateTime::new().human_format(); + let local_time = shadow_rs::DateTime::now().human_format(); println!("{}", local_time); Command::new("example_shadow") diff --git a/src/data_time.rs b/src/data_time.rs index cd12aa5..124030e 100644 --- a/src/data_time.rs +++ b/src/data_time.rs @@ -18,7 +18,7 @@ pub fn now_data_time() -> DateTime { // 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 => DateTime::new(), + None => DateTime::now(), Some(timestamp) => { let epoch = timestamp .into_string() @@ -33,17 +33,17 @@ pub fn now_data_time() -> DateTime { impl Default for DateTime { fn default() -> Self { - Self::new() + Self::now() } } impl DateTime { - pub fn new() -> Self { + pub fn now() -> Self { Self::local_now().unwrap_or_else(|_| DateTime::Utc(OffsetDateTime::now_utc())) } pub fn offset_datetime() -> OffsetDateTime { - let date_time = Self::new(); + let date_time = Self::now(); match date_time { DateTime::Local(time) | DateTime::Utc(time) => time, } diff --git a/src/lib.rs b/src/lib.rs index 632f21d..dae9bd7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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:{}"#, - DateTime::new().human_format() + DateTime::now().human_format() ); writeln!(&self.f, "{}\n\n", desc)?; Ok(())