Skip to content

Commit

Permalink
Merge pull request #106 from baoyachi/issue_105
Browse files Browse the repository at this point in the history
Set time crate OffsetDateTime human_format
  • Loading branch information
baoyachi authored Jul 19, 2022
2 parents 9333df9 + 143fcac commit 1b65c24
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "shadow-rs"
version = "0.14.0"
version = "0.15.0"
authors = ["baoyachi <[email protected]>"]
edition = "2021"
description = "A build-time information stored in your rust project"
Expand Down
2 changes: 1 addition & 1 deletion example_shadow/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
25 changes: 19 additions & 6 deletions src/data_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -33,15 +33,22 @@ 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::now();
match date_time {
DateTime::Local(time) | DateTime::Utc(time) => time,
}
}

pub fn local_now() -> Result<Self, Box<dyn Error>> {
let time_zone_local = TimeZone::local()?; // expensive call, should be cached

Expand Down Expand Up @@ -73,15 +80,21 @@ 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(),
}
self.format(&fmt).unwrap()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down

0 comments on commit 1b65c24

Please sign in to comment.