@@ -2,7 +2,7 @@ use crate::cli::NumberSeparator;
2
2
use gix:: date:: Time ;
3
3
use num_format:: ToFormattedString ;
4
4
use owo_colors:: { DynColors , Style } ;
5
- use std:: time:: { Duration , SystemTime } ;
5
+ use std:: time:: SystemTime ;
6
6
use time:: { format_description:: well_known:: Rfc3339 , OffsetDateTime } ;
7
7
use time_humanize:: HumanTime ;
8
8
@@ -26,17 +26,13 @@ where
26
26
fn to_human_time ( time : Time ) -> String {
27
27
let since_epoch_duration = SystemTime :: now ( )
28
28
. duration_since ( SystemTime :: UNIX_EPOCH )
29
- . unwrap ( ) ;
30
-
31
- let ts = Duration :: from_secs ( match time. seconds . try_into ( ) {
32
- Ok ( s) => s,
33
- Err ( _) => return "<before UNIX epoch>" . into ( ) ,
34
- } ) ;
35
- let duration = since_epoch_duration. checked_sub ( ts) . expect (
36
- "Achievement unlocked: time travel! \
37
- Check your system clock and commit dates.",
38
- ) ;
39
- let ht = HumanTime :: from ( -( duration. as_secs ( ) as i64 ) ) ;
29
+ . expect ( "System time is before the Unix epoch" ) ;
30
+ // Calculate the distance from the current time. This handles
31
+ // future dates gracefully and will simply return something like `in 5 minutes`
32
+ let delta_in_seconds = time
33
+ . seconds
34
+ . saturating_sub ( since_epoch_duration. as_secs ( ) as i64 ) ;
35
+ let ht = HumanTime :: from ( delta_in_seconds) ;
40
36
ht. to_string ( )
41
37
}
42
38
@@ -107,24 +103,22 @@ mod tests {
107
103
}
108
104
109
105
#[ test]
110
- #[ should_panic(
111
- expected = "Achievement unlocked: time travel! Check your system clock and commit dates."
112
- ) ]
113
- fn should_panic_when_display_human_time_and_commit_date_in_the_future ( ) {
106
+ fn handle_display_human_time_and_commit_date_in_the_future ( ) {
114
107
let day = Duration :: from_secs ( 60 * 60 * 24 ) ;
115
108
let current_time = SystemTime :: now ( )
116
109
. duration_since ( SystemTime :: UNIX_EPOCH )
117
110
. unwrap ( ) ;
118
111
let tomorrow = current_time + day;
119
112
let time = Time :: new ( tomorrow. as_secs ( ) as gix:: date:: SecondsSinceUnixEpoch , 0 ) ;
120
- format_time ( time, false ) ;
113
+ let result = format_time ( time, false ) ;
114
+ assert_eq ! ( result, "in a day" ) ;
121
115
}
122
116
123
117
#[ test]
124
118
fn display_time_before_epoch ( ) {
125
119
let time = Time :: new ( gix:: date:: SecondsSinceUnixEpoch :: MIN , 0 ) ;
126
120
let result = to_human_time ( time) ;
127
- assert_eq ! ( result, "<before UNIX epoch>" ) ;
121
+ assert ! ( result. ends_with ( " years ago" ) ) ;
128
122
}
129
123
130
124
#[ rstest]
0 commit comments