@@ -24,20 +24,26 @@ where
2424}
2525
2626fn to_human_time ( time : Time ) -> String {
27- let since_epoch_duration = SystemTime :: now ( )
28- . duration_since ( SystemTime :: UNIX_EPOCH )
29- . unwrap ( ) ;
27+ let duration = match diff_gix_time ( SystemTime :: now ( ) , time) {
28+ Ok ( d) => d,
29+ Err ( s) => return s,
30+ } ;
31+ let ht = HumanTime :: from ( -( duration. as_secs ( ) as i64 ) ) ;
32+ ht. to_string ( )
33+ }
3034
35+ /// Gets the duration between `now` and `time`. Returns `Err` if this cannot be calculated.
36+ fn diff_gix_time ( now : SystemTime , time : Time ) -> Result < Duration , String > {
37+ let since_epoch_duration = now. duration_since ( SystemTime :: UNIX_EPOCH ) . unwrap ( ) ;
3138 let ts = Duration :: from_secs ( match time. seconds . try_into ( ) {
3239 Ok ( s) => s,
33- Err ( _) => return "<before UNIX epoch>" . into ( ) ,
40+ Err ( _) => return Err ( "<before UNIX epoch>" . into ( ) ) ,
3441 } ) ;
3542 let duration = since_epoch_duration. checked_sub ( ts) . expect (
3643 "Achievement unlocked: time travel! \
3744 Check your system clock and commit dates.",
3845 ) ;
39- let ht = HumanTime :: from ( -( duration. as_secs ( ) as i64 ) ) ;
40- ht. to_string ( )
46+ Ok ( duration)
4147}
4248
4349pub fn format_number < T : ToFormattedString + std:: fmt:: Display > (
@@ -120,6 +126,21 @@ mod tests {
120126 format_time ( time, false ) ;
121127 }
122128
129+ #[ test]
130+ fn test_timezone_awareness ( ) {
131+ let current_time = SystemTime :: now ( )
132+ . duration_since ( SystemTime :: UNIX_EPOCH )
133+ . unwrap ( ) ;
134+ let hour_offset: i32 = 1 ;
135+ let offset = 60 * 60 * hour_offset;
136+ let also_now = Time :: new (
137+ ( current_time. as_secs ( ) as gix:: date:: SecondsSinceUnixEpoch ) + i64:: from ( offset) ,
138+ -offset,
139+ ) ;
140+ let diff = diff_gix_time ( SystemTime :: now ( ) , also_now) . unwrap ( ) ;
141+ assert_eq ! ( diff. as_secs( ) , 0 ) ;
142+ }
143+
123144 #[ test]
124145 fn display_time_before_epoch ( ) {
125146 let time = Time :: new ( gix:: date:: SecondsSinceUnixEpoch :: MIN , 0 ) ;
0 commit comments