@@ -14,6 +14,7 @@ use std::io::Write;
1414use std:: time:: { SystemTime , UNIX_EPOCH } ;
1515
1616use crate :: error:: { UResult , USimpleError } ;
17+ use crate :: show_error;
1718
1819/// Format the given date according to this time format style.
1920fn format_zoned < W : Write > ( out : & mut W , zoned : Zoned , fmt : & str ) -> UResult < ( ) > {
@@ -25,7 +26,12 @@ fn format_zoned<W: Write>(out: &mut W, zoned: Zoned, fmt: &str) -> UResult<()> {
2526}
2627
2728/// Format a `SystemTime` according to given fmt, and append to vector out.
28- pub fn format_system_time < W : Write > ( out : & mut W , time : SystemTime , fmt : & str ) -> UResult < ( ) > {
29+ pub fn format_system_time < W : Write > (
30+ out : & mut W ,
31+ time : SystemTime ,
32+ fmt : & str ,
33+ show_error : bool ,
34+ ) -> UResult < ( ) > {
2935 let zoned: Result < Zoned , _ > = time. try_into ( ) ;
3036 match zoned {
3137 Ok ( zoned) => format_zoned ( out, zoned, fmt) ,
@@ -36,13 +42,16 @@ pub fn format_system_time<W: Write>(out: &mut W, time: SystemTime, fmt: &str) ->
3642 // but it still far enough in the future/past to be unlikely to matter:
3743 // jiff: Year between -9999 to 9999 (UTC) [-377705023201..=253402207200]
3844 // GNU: Year fits in signed 32 bits (timezone dependent)
39- let ts = if time > UNIX_EPOCH {
40- time. duration_since ( UNIX_EPOCH ) . unwrap ( ) . as_secs ( )
45+ let ts: i64 = if time > UNIX_EPOCH {
46+ time. duration_since ( UNIX_EPOCH ) . unwrap ( ) . as_secs ( ) as i64
4147 } else {
42- out. write_all ( b"-" ) ?; // Add negative sign
43- UNIX_EPOCH . duration_since ( time) . unwrap ( ) . as_secs ( )
48+ -( UNIX_EPOCH . duration_since ( time) . unwrap ( ) . as_secs ( ) as i64 )
4449 } ;
45- out. write_all ( ts. to_string ( ) . as_bytes ( ) ) ?;
50+ let str = ts. to_string ( ) ;
51+ if show_error {
52+ show_error ! ( "time '{str}' is out of range" ) ;
53+ }
54+ out. write_all ( str. as_bytes ( ) ) ?;
4655 Ok ( ( ) )
4756 }
4857 }
@@ -60,11 +69,12 @@ mod tests {
6069
6170 let time = UNIX_EPOCH ;
6271 let mut out = Vec :: new ( ) ;
63- format_system_time ( & mut out, time, "%Y-%m-%d %H:%M" ) . expect ( "Formatting error." ) ;
72+ format_system_time ( & mut out, time, "%Y-%m-%d %H:%M" , false ) . expect ( "Formatting error." ) ;
6473 assert_eq ! ( String :: from_utf8( out) . unwrap( ) , "1970-01-01 00:00" ) ;
6574
6675 let mut out = Vec :: new ( ) ;
67- format_system_time ( & mut out, time, "%Y-%m-%d %H:%M:%S.%N %z" ) . expect ( "Formatting error." ) ;
76+ format_system_time ( & mut out, time, "%Y-%m-%d %H:%M:%S.%N %z" , false )
77+ . expect ( "Formatting error." ) ;
6878 assert_eq ! (
6979 String :: from_utf8( out) . unwrap( ) ,
7080 "1970-01-01 00:00:00.000000000 +0000"
@@ -76,12 +86,12 @@ mod tests {
7686 fn test_large_system_time ( ) {
7787 let time = UNIX_EPOCH + Duration :: from_secs ( 67_768_036_191_763_200 ) ;
7888 let mut out = Vec :: new ( ) ;
79- format_system_time ( & mut out, time, "%Y-%m-%d %H:%M" ) . expect ( "Formatting error." ) ;
89+ format_system_time ( & mut out, time, "%Y-%m-%d %H:%M" , false ) . expect ( "Formatting error." ) ;
8090 assert_eq ! ( String :: from_utf8( out) . unwrap( ) , "67768036191763200" ) ;
8191
8292 let time = UNIX_EPOCH - Duration :: from_secs ( 67_768_040_922_076_800 ) ;
8393 let mut out = Vec :: new ( ) ;
84- format_system_time ( & mut out, time, "%Y-%m-%d %H:%M" ) . expect ( "Formatting error." ) ;
94+ format_system_time ( & mut out, time, "%Y-%m-%d %H:%M" , false ) . expect ( "Formatting error." ) ;
8595 assert_eq ! ( String :: from_utf8( out) . unwrap( ) , "-67768040922076800" ) ;
8696 }
8797}
0 commit comments