@@ -2,7 +2,7 @@ use std::cmp::min;
22use std:: net:: IpAddr ;
33
44use crate :: utils:: types:: timestamp:: Timestamp ;
5- use chrono :: { Local , TimeZone } ;
5+ use jiff :: tz :: TimeZone ;
66
77/// Application version number (to be displayed in gui footer)
88pub const APP_VERSION : & str = env ! ( "CARGO_PKG_VERSION" ) ;
@@ -105,9 +105,10 @@ pub fn get_formatted_num_seconds(num_seconds: u128) -> String {
105105pub fn get_formatted_timestamp ( t : Timestamp ) -> String {
106106 let date_opt = t
107107 . to_usecs ( )
108- . and_then ( |usecs| Local . timestamp_micros ( usecs) . latest ( ) ) ;
108+ . and_then ( |usecs| jiff:: Timestamp :: from_microsecond ( usecs) . ok ( ) )
109+ . map ( |ts| TimeZone :: system ( ) . to_datetime ( ts) ) ;
109110 if let Some ( date) = date_opt {
110- date. format ( "%Y/%m/%d %H:%M:%S" ) . to_string ( )
111+ date. strftime ( "%Y/%m/%d %H:%M:%S" ) . to_string ( )
111112 } else {
112113 "?" . to_string ( )
113114 }
@@ -191,6 +192,31 @@ mod tests {
191192 ) ;
192193 }
193194
195+ #[ test]
196+ fn test_get_formatted_timestamp ( ) {
197+ // 2023/11/14 22:13:20 UTC: the rendered day is 14 or 15 depending on the system time zone
198+ let formatted = get_formatted_timestamp ( Timestamp :: new ( 1_700_000_000 , 123_456 ) ) ;
199+ assert ! ( formatted. starts_with( "2023/11/1" ) , "{formatted}" ) ;
200+ assert ! (
201+ formatted. chars( ) . enumerate( ) . all( |( i, c) | match i {
202+ 4 | 7 => c == '/' ,
203+ 10 => c == ' ' ,
204+ 13 | 16 => c == ':' ,
205+ _ => c. is_ascii_digit( ) ,
206+ } ) && formatted. len( ) == 19 ,
207+ "{formatted}"
208+ ) ;
209+
210+ // microseconds overflow
211+ assert_eq ! ( get_formatted_timestamp( Timestamp :: new( i64 :: MAX , 0 ) ) , "?" ) ;
212+ assert_eq ! ( get_formatted_timestamp( Timestamp :: new( i64 :: MIN , 0 ) ) , "?" ) ;
213+ // out of the range of dates that can be represented
214+ assert_eq ! (
215+ get_formatted_timestamp( Timestamp :: new( 1_800_000_000_000 , 0 ) ) ,
216+ "?"
217+ ) ;
218+ }
219+
194220 #[ cfg( windows) ]
195221 #[ test]
196222 fn test_logs_file_path ( ) {
0 commit comments