@@ -2,6 +2,7 @@ use std::fmt;
22use std:: sync:: { Arc , Mutex } ;
33
44use anyhow:: { anyhow, Context , Result } ;
5+ use chrono:: { DateTime , Local } ;
56use payjoin:: bitcoin:: consensus:: encode:: serialize_hex;
67use payjoin:: bitcoin:: { Amount , FeeRate } ;
78use payjoin:: persist:: { OptionalTransitionOutcome , SessionPersister } ;
@@ -117,6 +118,57 @@ struct SessionHistoryRow<Status> {
117118 error_message : Option < String > ,
118119}
119120
121+ fn format_timestamps ( value : & str ) -> String {
122+ //Prefix and suffix are used as markers to identify the timestamp in the string
123+ const PREFIX : & str = "Time(Time(" ;
124+ const SUFFIX : & str = "))" ;
125+
126+ let to_local = |secs : u64 | -> String {
127+ let Ok ( secs) = i64:: try_from ( secs) else {
128+ return secs. to_string ( ) ;
129+ } ;
130+
131+ match DateTime :: from_timestamp ( secs, 0 ) {
132+ Some ( datetime) =>
133+ datetime. with_timezone ( & Local ) . format ( "%Y-%m-%d %H:%M:%S" ) . to_string ( ) ,
134+ None => secs. to_string ( ) ,
135+ }
136+ } ;
137+
138+ if let Ok ( secs) = value. parse :: < u64 > ( ) {
139+ return to_local ( secs) ;
140+ }
141+
142+ let mut formatted = String :: with_capacity ( value. len ( ) ) ;
143+ let mut remainder = value;
144+
145+ while let Some ( start) = remainder. find ( PREFIX ) {
146+ let ( before, from_prefix) = remainder. split_at ( start) ;
147+ formatted. push_str ( before) ;
148+ let after_prefix = & from_prefix[ PREFIX . len ( ) ..] ;
149+
150+ let Some ( end) = after_prefix. find ( SUFFIX ) else {
151+ formatted. push_str ( from_prefix) ;
152+ remainder = "" ;
153+ break ;
154+ } ;
155+
156+ let ( epoch_secs, after_match) = after_prefix. split_at ( end) ;
157+ if let Ok ( secs) = epoch_secs. parse :: < u64 > ( ) {
158+ formatted. push_str ( & format ! ( "Time({})" , to_local( secs) ) ) ;
159+ } else {
160+ formatted. push_str ( PREFIX ) ;
161+ formatted. push_str ( epoch_secs) ;
162+ formatted. push_str ( SUFFIX ) ;
163+ }
164+
165+ remainder = & after_match[ SUFFIX . len ( ) ..] ;
166+ }
167+
168+ formatted. push_str ( remainder) ;
169+ formatted
170+ }
171+
120172impl < Status : StatusText > fmt:: Display for SessionHistoryRow < Status > {
121173 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
122174 write ! (
@@ -126,10 +178,7 @@ impl<Status: StatusText> fmt::Display for SessionHistoryRow<Status> {
126178 self . role. as_str( ) ,
127179 match self . completed_at {
128180 None => "Not Completed" . to_string( ) ,
129- Some ( secs) => {
130- // TODO: human readable time
131- secs. to_string( )
132- }
181+ Some ( secs) => format_timestamps( & secs. to_string( ) ) ,
133182 } ,
134183 self . error_message. as_deref( ) . unwrap_or( self . status. status_text( ) )
135184 )
@@ -362,7 +411,7 @@ impl AppTrait for App {
362411 role : Role :: Sender ,
363412 status : SendSession :: Closed ( SenderSessionOutcome :: Failure ) ,
364413 completed_at : None ,
365- error_message : Some ( e. to_string ( ) ) ,
414+ error_message : Some ( format_timestamps ( & e. to_string ( ) ) ) ,
366415 } ;
367416 send_rows. push ( row) ;
368417 }
@@ -388,7 +437,7 @@ impl AppTrait for App {
388437 role : Role :: Receiver ,
389438 status : ReceiveSession :: Closed ( ReceiverSessionOutcome :: Failure ) ,
390439 completed_at : None ,
391- error_message : Some ( e. to_string ( ) ) ,
440+ error_message : Some ( format_timestamps ( & e. to_string ( ) ) ) ,
392441 } ;
393442 recv_rows. push ( row) ;
394443 }
@@ -415,7 +464,7 @@ impl AppTrait for App {
415464 role : Role :: Sender ,
416465 status : SendSession :: Closed ( SenderSessionOutcome :: Failure ) ,
417466 completed_at : Some ( completed_at) ,
418- error_message : Some ( e. to_string ( ) ) ,
467+ error_message : Some ( format_timestamps ( & e. to_string ( ) ) ) ,
419468 } ;
420469 send_rows. push ( row) ;
421470 }
@@ -443,7 +492,7 @@ impl AppTrait for App {
443492 role : Role :: Receiver ,
444493 status : ReceiveSession :: Closed ( ReceiverSessionOutcome :: Failure ) ,
445494 completed_at : Some ( completed_at) ,
446- error_message : Some ( e. to_string ( ) ) ,
495+ error_message : Some ( format_timestamps ( & e. to_string ( ) ) ) ,
447496 } ;
448497 recv_rows. push ( row) ;
449498 }
0 commit comments