Skip to content

Commit b79c202

Browse files
committed
Properly format history timestmap
This Pr addresses #1472. When the session history is displayed the timestmap isn't in readable form.
1 parent 45d286f commit b79c202

File tree

4 files changed

+66
-10
lines changed

4 files changed

+66
-10
lines changed

Cargo-minimal.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,8 +875,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
875875
checksum = "c673075a2e0e5f4a1dde27ce9dee1ea4558c7ffe648f576438a20ca1d2acc4b0"
876876
dependencies = [
877877
"iana-time-zone",
878+
"js-sys",
878879
"num-traits",
879880
"serde",
881+
"wasm-bindgen",
880882
"windows-link",
881883
]
882884

@@ -2786,6 +2788,7 @@ dependencies = [
27862788
"anyhow",
27872789
"async-trait",
27882790
"bitcoind-async-client",
2791+
"chrono",
27892792
"clap 4.6.0",
27902793
"config",
27912794
"dirs",

Cargo-recent.lock

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -874,13 +874,15 @@ dependencies = [
874874

875875
[[package]]
876876
name = "chrono"
877-
version = "0.4.42"
877+
version = "0.4.44"
878878
source = "registry+https://github.com/rust-lang/crates.io-index"
879-
checksum = "145052bdd345b87320e369255277e3fb5152762ad123a901ef5c262dd38fe8d2"
879+
checksum = "c673075a2e0e5f4a1dde27ce9dee1ea4558c7ffe648f576438a20ca1d2acc4b0"
880880
dependencies = [
881881
"iana-time-zone",
882+
"js-sys",
882883
"num-traits",
883884
"serde",
885+
"wasm-bindgen",
884886
"windows-link 0.2.1",
885887
]
886888

@@ -2754,6 +2756,7 @@ dependencies = [
27542756
"anyhow",
27552757
"async-trait",
27562758
"bitcoind-async-client",
2759+
"chrono",
27572760
"clap 4.5.46",
27582761
"config",
27592762
"dirs",

payjoin-cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ v2 = ["payjoin/v2", "payjoin/io"]
2929
anyhow = "1.0.99"
3030
async-trait = "0.1.89"
3131
bitcoind-async-client = "0.10.2"
32+
chrono = "0.4.44"
3233
clap = { version = "4.5.45", features = ["derive"] }
3334
config = "0.15.14"
3435
dirs = "6.0.0"

payjoin-cli/src/app/v2/mod.rs

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::fmt;
22
use std::sync::{Arc, Mutex};
33

44
use anyhow::{anyhow, Context, Result};
5+
use chrono::{DateTime, Local};
56
use payjoin::bitcoin::consensus::encode::serialize_hex;
67
use payjoin::bitcoin::{Amount, FeeRate};
78
use 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+
120172
impl<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

Comments
 (0)