Skip to content

Commit 241a5df

Browse files
authored
Merge pull request #1248 from tnull/2026-11-drop-chrono-dependency
Drop direct `chrono` dependency
2 parents 0824b3f + a74c5c9 commit 241a5df

5 files changed

Lines changed: 117 additions & 37 deletions

File tree

‎Cargo.toml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ bip21 = { version = "0.5", features = ["std"], default-features = false, optiona
114114

115115
base64 = { version = "0.22.1", default-features = false, features = ["std"] }
116116
getrandom = { version = "0.3", default-features = false }
117-
chrono = { version = "0.4", default-features = false, features = ["clock"] }
118117
tokio = { version = "1.39", default-features = false, features = [ "rt-multi-thread", "time", "sync", "macros", "net" ] }
119118
tokio-util = { version = "0.7.10", default-features = false, features = ["rt"] }
120119
esplora-client = { version = "0.12", default-features = false, features = ["tokio", "async-https-rustls"], optional = true }

‎src/liquidity/client/lsps2/mod.rs‎

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,11 +1340,19 @@ mod tests {
13401340
use super::*;
13411341
use std::future::poll_fn;
13421342
use std::task::Poll;
1343+
use std::time::{SystemTime, UNIX_EPOCH};
1344+
1345+
use lightning_liquidity::lsps0::ser::LSPSDateTime;
13431346

13441347
use crate::builder::NodeBuilder;
13451348
use crate::entropy::NodeEntropy;
13461349
use crate::io::test_utils::InMemoryStore;
13471350

1351+
fn datetime_from_now(offset: Duration) -> LSPSDateTime {
1352+
let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
1353+
LSPSDateTime::new_from_duration_since_epoch(now + offset)
1354+
}
1355+
13481356
fn rejection_test_node() -> (crate::Node, PublicKey) {
13491357
test_node_with_fee_limit(None)
13501358
}
@@ -1381,10 +1389,7 @@ mod tests {
13811389
let params = LSPS2OpeningFeeParams {
13821390
min_fee_msat: 1,
13831391
proportional: 1,
1384-
valid_until: (chrono::Utc::now() + chrono::Duration::days(2))
1385-
.to_rfc3339()
1386-
.parse()
1387-
.unwrap(),
1392+
valid_until: datetime_from_now(Duration::from_secs(2 * 24 * 60 * 60)),
13881393
min_lifetime: 1,
13891394
max_client_to_self_delay: 1,
13901395
min_payment_size_msat: 1,
@@ -1450,11 +1455,9 @@ mod tests {
14501455
let params = |valid_until: u64| LSPS2OpeningFeeParams {
14511456
min_fee_msat: 1,
14521457
proportional: 1,
1453-
valid_until: chrono::DateTime::from_timestamp(valid_until as i64, 0)
1454-
.unwrap()
1455-
.to_rfc3339()
1456-
.parse()
1457-
.unwrap(),
1458+
valid_until: LSPSDateTime::new_from_duration_since_epoch(Duration::from_secs(
1459+
valid_until,
1460+
)),
14581461
min_lifetime: 1,
14591462
max_client_to_self_delay: 1,
14601463
min_payment_size_msat: 1,
@@ -1588,11 +1591,9 @@ mod tests {
15881591
params: LSPS2OpeningFeeParams {
15891592
min_fee_msat: 1,
15901593
proportional: 1,
1591-
valid_until: chrono::DateTime::from_timestamp(valid_until as i64, 0)
1592-
.unwrap()
1593-
.to_rfc3339()
1594-
.parse()
1595-
.unwrap(),
1594+
valid_until: LSPSDateTime::new_from_duration_since_epoch(Duration::from_secs(
1595+
valid_until,
1596+
)),
15961597
min_lifetime: 1,
15971598
max_client_to_self_delay: 1,
15981599
min_payment_size_msat: 1,
@@ -1768,11 +1769,9 @@ mod tests {
17681769
params: LSPS2OpeningFeeParams {
17691770
min_fee_msat: 1,
17701771
proportional: 1,
1771-
valid_until: chrono::DateTime::from_timestamp(valid_until as i64, 0)
1772-
.unwrap()
1773-
.to_rfc3339()
1774-
.parse()
1775-
.unwrap(),
1772+
valid_until: LSPSDateTime::new_from_duration_since_epoch(Duration::from_secs(
1773+
valid_until,
1774+
)),
17761775
min_lifetime: 1,
17771776
max_client_to_self_delay: 1,
17781777
min_payment_size_msat: 1,
@@ -1898,11 +1897,10 @@ mod tests {
18981897

18991898
#[test]
19001899
fn opening_params_must_cover_invoice_lifetime() {
1901-
let valid_until = chrono::Utc::now() + chrono::Duration::hours(3);
19021900
let mut params = LSPS2OpeningFeeParams {
19031901
min_fee_msat: 1,
19041902
proportional: 1,
1905-
valid_until: valid_until.to_rfc3339().parse().unwrap(),
1903+
valid_until: datetime_from_now(Duration::from_secs(3 * 60 * 60)),
19061904
min_lifetime: 1,
19071905
max_client_to_self_delay: 1,
19081906
min_payment_size_msat: 1,
@@ -1915,9 +1913,7 @@ mod tests {
19151913
assert!(!opening_fee_params_valid_for(&params, u64::MAX));
19161914

19171915
let invoice_lifetime_secs = 2 * 60 * 60;
1918-
let valid_until =
1919-
chrono::Utc::now() + chrono::Duration::seconds(invoice_lifetime_secs as i64 + 1);
1920-
params.valid_until = valid_until.to_rfc3339().parse().unwrap();
1916+
params.valid_until = datetime_from_now(Duration::from_secs(invoice_lifetime_secs + 1));
19211917
assert!(
19221918
!opening_fee_params_valid_for(&params, invoice_lifetime_secs),
19231919
"opening parameters leave no time for the buy round trip"
@@ -1953,10 +1949,7 @@ mod tests {
19531949
LSPS2OpeningFeeParams {
19541950
min_fee_msat: 1_500,
19551951
proportional: 0,
1956-
valid_until: (chrono::Utc::now() + chrono::Duration::days(2))
1957-
.to_rfc3339()
1958-
.parse()
1959-
.unwrap(),
1952+
valid_until: datetime_from_now(Duration::from_secs(2 * 24 * 60 * 60)),
19601953
min_lifetime: 1,
19611954
max_client_to_self_delay: 1,
19621955
min_payment_size_msat: 1,

‎src/liquidity/service/lsps2.rs‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77

88
use std::ops::Deref;
99
use std::sync::{Arc, RwLock, Weak};
10-
use std::time::Duration;
10+
use std::time::{Duration, SystemTime, UNIX_EPOCH};
1111

1212
use bitcoin::secp256k1::PublicKey;
1313
use bitcoin::Transaction;
14-
use chrono::Utc;
1514
use lightning::events::HTLCHandlingFailureType;
1615
use lightning::ln::channelmanager::InterceptId;
1716
use lightning::ln::types::ChannelId;
@@ -343,7 +342,12 @@ where
343342
}
344343
}
345344

346-
let valid_until = LSPSDateTime(Utc::now() + LSPS2_GETINFO_REQUEST_EXPIRY);
345+
let now = SystemTime::now()
346+
.duration_since(UNIX_EPOCH)
347+
.expect("system clock to be ahead of the unix epoch");
348+
let valid_until = LSPSDateTime::new_from_duration_since_epoch(
349+
now + LSPS2_GETINFO_REQUEST_EXPIRY,
350+
);
347351
let opening_fee_params = LSPS2RawOpeningFeeParams {
348352
min_fee_msat: service_config.min_channel_opening_fee_msat,
349353
proportional: service_config.channel_opening_fee_ppm,

‎src/logger.rs‎

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ use std::io::Write;
1414
use std::os::unix::fs::OpenOptionsExt;
1515
use std::path::Path;
1616
use std::sync::Arc;
17+
use std::time::{Duration, SystemTime, UNIX_EPOCH};
1718

1819
use bitcoin::secp256k1::PublicKey;
19-
use chrono::Utc;
2020
use lightning::ln::types::ChannelId;
2121
use lightning::types::payment::PaymentHash;
2222
pub use lightning::util::logger::Level as LogLevel;
@@ -26,6 +26,41 @@ use log::{Level as LogFacadeLevel, Record as LogFacadeRecord};
2626

2727
use crate::io::utils::create_dir_all_private;
2828

29+
/// Displays a duration since the Unix epoch as a `YYYY-MM-DD hh:mm:ss.mmm` UTC timestamp.
30+
struct UtcTimestamp(Duration);
31+
32+
impl fmt::Display for UtcTimestamp {
33+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
34+
let secs = self.0.as_secs();
35+
let secs_of_day = secs % 86_400;
36+
37+
// Converts the day count to a proleptic Gregorian date, counting years from March 1st so
38+
// that the leap day falls at the end of the year.
39+
let days = secs / 86_400 + 719_468;
40+
let era = days / 146_097;
41+
let day_of_era = days % 146_097;
42+
let year_of_era =
43+
(day_of_era - day_of_era / 1_460 + day_of_era / 36_524 - day_of_era / 146_096) / 365;
44+
let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
45+
let shifted_month = (5 * day_of_year + 2) / 153;
46+
let day = day_of_year - (153 * shifted_month + 2) / 5 + 1;
47+
let month = if shifted_month < 10 { shifted_month + 3 } else { shifted_month - 9 };
48+
let year = year_of_era + era * 400 + u64::from(month <= 2);
49+
50+
write!(
51+
f,
52+
"{:04}-{:02}-{:02} {:02}:{:02}:{:02}.{:03}",
53+
year,
54+
month,
55+
day,
56+
secs_of_day / 3_600,
57+
secs_of_day % 3_600 / 60,
58+
secs_of_day % 60,
59+
self.0.subsec_millis(),
60+
)
61+
}
62+
}
63+
2964
fn open_log_file(file_path: &str) -> std::io::Result<fs::File> {
3065
let mut options = fs::OpenOptions::new();
3166
options.create(true).append(true);
@@ -210,9 +245,11 @@ impl LogWriter for Writer {
210245
return;
211246
}
212247

248+
let since_epoch =
249+
SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or(Duration::ZERO);
213250
let log = format!(
214251
"{} {:<5} [{}:{}] {}{}\n",
215-
Utc::now().format("%Y-%m-%d %H:%M:%S%.3f"),
252+
UtcTimestamp(since_epoch),
216253
record.level.to_string(),
217254
record.module_path,
218255
record.line,
@@ -324,6 +361,22 @@ mod tests {
324361
#[cfg(unix)]
325362
use crate::io::test_utils::random_storage_path;
326363

364+
#[test]
365+
fn formats_utc_timestamps() {
366+
for (secs, millis, expected) in [
367+
(0, 0, "1970-01-01 00:00:00.000"),
368+
(951782399, 999, "2000-02-28 23:59:59.999"),
369+
(951782400, 0, "2000-02-29 00:00:00.000"),
370+
(1709251199, 7, "2024-02-29 23:59:59.007"),
371+
(1709251200, 0, "2024-03-01 00:00:00.000"),
372+
(1794960000, 123, "2026-11-18 00:00:00.123"),
373+
(4102444799, 50, "2099-12-31 23:59:59.050"),
374+
] {
375+
let since_epoch = Duration::from_secs(secs) + Duration::from_millis(millis);
376+
assert_eq!(UtcTimestamp(since_epoch).to_string(), expected);
377+
}
378+
}
379+
327380
#[cfg(unix)]
328381
#[test]
329382
fn creates_private_log_file() {

‎tests/common/logging.rs‎

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::sync::{Arc, Mutex};
2+
use std::time::{SystemTime, UNIX_EPOCH};
23

3-
use chrono::Utc;
44
use ldk_node::logger::{LogContext, LogLevel, LogRecord, LogWriter};
55
#[cfg(not(feature = "uniffi"))]
66
use log::Record as LogFacadeRecord;
@@ -19,6 +19,37 @@ impl Default for TestLogWriter {
1919
}
2020
}
2121

22+
/// Returns the current UTC time formatted as `YYYY-MM-DD hh:mm:ss.mmm`.
23+
fn utc_timestamp_now() -> String {
24+
let since_epoch = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
25+
let secs = since_epoch.as_secs();
26+
let secs_of_day = secs % 86_400;
27+
28+
// Converts the day count to a proleptic Gregorian date, counting years from March 1st so
29+
// that the leap day falls at the end of the year.
30+
let days = secs / 86_400 + 719_468;
31+
let era = days / 146_097;
32+
let day_of_era = days % 146_097;
33+
let year_of_era =
34+
(day_of_era - day_of_era / 1_460 + day_of_era / 36_524 - day_of_era / 146_096) / 365;
35+
let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
36+
let shifted_month = (5 * day_of_year + 2) / 153;
37+
let day = day_of_year - (153 * shifted_month + 2) / 5 + 1;
38+
let month = if shifted_month < 10 { shifted_month + 3 } else { shifted_month - 9 };
39+
let year = year_of_era + era * 400 + u64::from(month <= 2);
40+
41+
format!(
42+
"{:04}-{:02}-{:02} {:02}:{:02}:{:02}.{:03}",
43+
year,
44+
month,
45+
day,
46+
secs_of_day / 3_600,
47+
secs_of_day % 3_600 / 60,
48+
secs_of_day % 60,
49+
since_epoch.subsec_millis(),
50+
)
51+
}
52+
2253
pub(crate) struct MockLogFacadeLogger {
2354
logs: Arc<Mutex<Vec<String>>>,
2455
}
@@ -41,7 +72,7 @@ impl LogFacadeLog for MockLogFacadeLogger {
4172
fn log(&self, record: &log::Record) {
4273
let message = format!(
4374
"{} {:<5} [{}:{}] {}",
44-
Utc::now().format("%Y-%m-%d %H:%M:%S"),
75+
&utc_timestamp_now()[.."YYYY-MM-DD hh:mm:ss".len()],
4576
record.level().to_string(),
4677
record.module_path().unwrap(),
4778
record.line().unwrap(),
@@ -158,7 +189,7 @@ impl LogWriter for MultiNodeLogger {
158189
let log = format!(
159190
"[{}] {} {:<5} [{}:{}] {}{}\n",
160191
self.node_id,
161-
Utc::now().format("%Y-%m-%d %H:%M:%S%.3f"),
192+
utc_timestamp_now(),
162193
record.level.to_string(),
163194
record.module_path,
164195
record.line,

0 commit comments

Comments
 (0)