Skip to content

Commit

Permalink
better panic message (#1010)
Browse files Browse the repository at this point in the history
  • Loading branch information
o2sh authored Apr 8, 2023
1 parent 6d448ce commit 727cf70
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/info/created.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::utils::gitoxide_time_to_formatted_time;
use super::utils::format_time;
use crate::info::{
utils::git::Commits,
utils::info_field::{InfoField, InfoType},
Expand All @@ -19,7 +19,7 @@ impl CreatedInfo {
}

fn get_creation_date(commits: &Commits, iso_time: bool) -> String {
gitoxide_time_to_formatted_time(commits.time_of_first_commit, iso_time)
format_time(commits.time_of_first_commit, iso_time)
}

#[typetag::serialize]
Expand Down
4 changes: 2 additions & 2 deletions src/info/last_change.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::utils::gitoxide_time_to_formatted_time;
use super::utils::format_time;
use crate::info::{
utils::git::Commits,
utils::info_field::{InfoField, InfoType},
Expand All @@ -20,7 +20,7 @@ impl LastChangeInfo {
}

fn get_date_of_last_commit(commits: &Commits, iso_time: bool) -> String {
gitoxide_time_to_formatted_time(commits.time_of_most_recent_commit, iso_time)
format_time(commits.time_of_most_recent_commit, iso_time)
}

#[typetag::serialize]
Expand Down
44 changes: 37 additions & 7 deletions src/info/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use std::time::{Duration, SystemTime};

use gix::actor::Time;
use time::{format_description::well_known::Rfc3339, OffsetDateTime};
use time_humanize::HumanTime;

pub mod git;
pub mod info_field;

pub fn gitoxide_time_to_formatted_time(time: Time, iso_time: bool) -> String {
pub fn format_time(time: Time, iso_time: bool) -> String {
if iso_time {
to_rfc3339(HumanTime::from(time.seconds_since_unix_epoch as i64))
} else {
let ht = HumanTime::from_duration_since_timestamp(time.seconds_since_unix_epoch as u64);
ht.to_string()
to_human_time(time)
}
}

Expand All @@ -21,6 +22,20 @@ where
dt.into().format(&Rfc3339).unwrap()
}

fn to_human_time(time: Time) -> String {
let since_epoch_duration = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap();

let ts = Duration::from_secs(time.seconds_since_unix_epoch as u64);
let duration = since_epoch_duration.checked_sub(ts).expect(
"Achievement unlocked: time travel! \
Check your system clock and commit dates.",
);
let ht = HumanTime::from(-(duration.as_secs() as i64));
ht.to_string()
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -33,7 +48,7 @@ mod tests {
.unwrap();

let time = Time::new(current_time.as_secs() as u32, 0);
let result = gitoxide_time_to_formatted_time(time, false);
let result = format_time(time, false);
assert_eq!(result, "now");
}

Expand All @@ -46,7 +61,7 @@ mod tests {
// NOTE 366 so that it's a year ago even with leap years.
let year_ago = current_time - (day * 366);
let time = Time::new(year_ago.as_secs() as u32, 0);
let result = gitoxide_time_to_formatted_time(time, false);
let result = format_time(time, false);
assert_eq!(result, "a year ago");
}

Expand All @@ -55,14 +70,29 @@ mod tests {
// Set "current" time to 11/18/2021 11:02:22
let time_sample = 1637233282;
let time = Time::new(time_sample, 0);
let result = gitoxide_time_to_formatted_time(time, true);
let result = format_time(time, true);
assert_eq!(result, "2021-11-18T11:01:22Z");
}

#[test]
fn display_time_as_iso_time_current_epoch() {
let time_sample = 0;
let time = Time::new(time_sample, 0);
let result = gitoxide_time_to_formatted_time(time, true);
let result = format_time(time, true);
assert_eq!(result, "1970-01-01T00:00:00Z");
}

#[test]
#[should_panic(
expected = "Achievement unlocked: time travel! Check your system clock and commit dates."
)]
fn should_panic_when_display_human_time_and_commit_date_in_the_future() {
let day = Duration::from_secs(60 * 60 * 24);
let current_time = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap();
let tomorrow = current_time + day;
let time = Time::new(tomorrow.as_secs() as u32, 0);
format_time(time, false);
}
}

0 comments on commit 727cf70

Please sign in to comment.