Skip to content

Commit

Permalink
Merge pull request #168 from baoyachi/issue/149
Browse files Browse the repository at this point in the history
  • Loading branch information
baoyachi authored Jul 22, 2024
2 parents e9e45b3 + dfb8b24 commit ee12741
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 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.30.0"
version = "0.31.0"
authors = ["baoyachi <[email protected]>"]
edition = "2021"
description = "A build-time information stored in your rust project"
Expand Down
14 changes: 7 additions & 7 deletions src/date_time.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::Format;
use crate::{Format, SdResult, ShadowError};
use std::error::Error;
use time::format_description::well_known::{Rfc2822, Rfc3339};
#[cfg(feature = "tzdb")]
Expand Down Expand Up @@ -67,20 +67,20 @@ impl DateTime {
Ok(DateTime::Local(local_date_time))
}

pub fn timestamp_2_utc(time_stamp: i64) -> Self {
let time = OffsetDateTime::from_unix_timestamp(time_stamp).unwrap();
DateTime::Utc(time)
pub fn timestamp_2_utc(time_stamp: i64) -> SdResult<Self> {
let time = OffsetDateTime::from_unix_timestamp(time_stamp).map_err(ShadowError::new)?;
Ok(DateTime::Utc(time))
}

pub fn to_rfc2822(&self) -> String {
match self {
DateTime::Local(dt) | DateTime::Utc(dt) => dt.format(&Rfc2822).unwrap(),
DateTime::Local(dt) | DateTime::Utc(dt) => dt.format(&Rfc2822).unwrap_or_default(),
}
}

pub fn to_rfc3339(&self) -> String {
match self {
DateTime::Local(dt) | DateTime::Utc(dt) => dt.format(&Rfc3339).unwrap(),
DateTime::Local(dt) | DateTime::Utc(dt) => dt.format(&Rfc3339).unwrap_or_default(),
}
}
}
Expand Down Expand Up @@ -197,7 +197,7 @@ mod tests {

#[test]
fn test_timestamp_2_utc() {
let time = DateTime::timestamp_2_utc(1628080443);
let time = DateTime::timestamp_2_utc(1628080443).unwrap();
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");
Expand Down
26 changes: 14 additions & 12 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,11 @@ impl Git {
self.update_str(COMMIT_HASH, git_info.commit);

let time_stamp = git_info.date.parse::<i64>()?;
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());
self.update_str(COMMIT_DATE_3339, date_time.to_rfc3339());
if let Ok(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());
self.update_str(COMMIT_DATE_3339, date_time.to_rfc3339());
}

Ok(())
}
Expand Down Expand Up @@ -193,14 +194,6 @@ impl Git {

let commit = reference.peel_to_commit().map_err(ShadowError::new)?;

let time_stamp = commit.time().seconds().to_string().parse::<i64>()?;
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());

self.update_str(COMMIT_DATE_3339, date_time.to_rfc3339());

let author = commit.author();
if let Some(v) = author.email() {
self.update_str(COMMIT_EMAIL, v.to_string());
Expand All @@ -216,6 +209,15 @@ impl Git {
self.update_bool(GIT_CLEAN, false);
}
self.update_str(GIT_STATUS_FILE, status_file);

let time_stamp = commit.time().seconds().to_string().parse::<i64>()?;
if let Ok(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());

self.update_str(COMMIT_DATE_3339, date_time.to_rfc3339());
}
}
Ok(())
}
Expand Down

0 comments on commit ee12741

Please sign in to comment.