1010use jiff:: Zoned ;
1111use jiff:: fmt:: StdIoWrite ;
1212use jiff:: fmt:: strtime:: { BrokenDownTime , Config } ;
13+ use std:: io:: Write ;
1314use std:: time:: { SystemTime , UNIX_EPOCH } ;
1415
16+ use crate :: error:: { UResult , USimpleError } ;
17+
1518/// Format the given date according to this time format style.
16- fn format_zoned ( out : & mut Vec < u8 > , zoned : Zoned , fmt : & str ) -> Result < ( ) , jiff :: Error > {
19+ fn format_zoned < W : Write > ( out : & mut W , zoned : Zoned , fmt : & str ) -> UResult < ( ) > {
1720 let tm = BrokenDownTime :: from ( & zoned) ;
1821 let mut out = StdIoWrite ( out) ;
1922 let config = Config :: new ( ) . lenient ( true ) ;
2023 tm. format_with_config ( & config, fmt, & mut out)
24+ . map_err ( |x| USimpleError :: new ( 1 , x. to_string ( ) ) )
2125}
2226
2327/// Format a `SystemTime` according to given fmt, and append to vector out.
24- pub fn format_system_time (
25- out : & mut Vec < u8 > ,
26- time : SystemTime ,
27- fmt : & str ,
28- ) -> Result < ( ) , jiff:: Error > {
28+ pub fn format_system_time < W : Write > ( out : & mut W , time : SystemTime , fmt : & str ) -> UResult < ( ) > {
2929 let zoned: Result < Zoned , _ > = time. try_into ( ) ;
3030 match zoned {
3131 Ok ( zoned) => format_zoned ( out, zoned, fmt) ,
@@ -39,10 +39,10 @@ pub fn format_system_time(
3939 let ts = if time > UNIX_EPOCH {
4040 time. duration_since ( UNIX_EPOCH ) . unwrap ( ) . as_secs ( )
4141 } else {
42- out. extend ( b"-" ) ; // Add negative sign
42+ out. write_all ( b"-" ) ? ; // Add negative sign
4343 UNIX_EPOCH . duration_since ( time) . unwrap ( ) . as_secs ( )
4444 } ;
45- out. extend ( ts. to_string ( ) . as_bytes ( ) ) ;
45+ out. write_all ( ts. to_string ( ) . as_bytes ( ) ) ? ;
4646 Ok ( ( ) )
4747 }
4848 }
0 commit comments