Skip to content

Commit

Permalink
Do not crash if the history timestamp is in the future
Browse files Browse the repository at this point in the history
Resolve #189

We were throwing an OutOfRangeError. This occurs when you attempt to
convert to a duration, and your input is <0. A value in the future would
have done this. This is especially noticeable during DST...
  • Loading branch information
ellie committed Dec 19, 2021
1 parent 8d21506 commit 9c9e9dc
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/command/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ impl State {
let duration: Vec<&str> = duration.split(' ').collect();

let ago = chrono::Utc::now().sub(h.timestamp);
let ago = humantime::format_duration(ago.to_std().unwrap()).to_string();

// Account for the chance that h.timestamp is "in the future"
// This would mean that "ago" is negative, and the unwrap here
// would fail.
// If the timestamp would otherwise be in the future, display
// the time ago as 0.
let ago = humantime::format_duration(ago.to_std().unwrap_or_else(|_|Duration::new(0, 0)))
.to_string();
let ago: Vec<&str> = ago.split(' ').collect();

(
Expand Down

0 comments on commit 9c9e9dc

Please sign in to comment.