Skip to content

Commit d819797

Browse files
authored
Merge pull request #528 from limxiy/feat/utilities-time-formatters-conversion
Add timestamp helpers, fee formatting, conversion functions, and recommendation types (#379, #377, #376, #296)
2 parents 9fe34c7 + 375db81 commit d819797

6 files changed

Lines changed: 90 additions & 2 deletions

File tree

packages/core/src/recommendation/types.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,6 @@ pub struct RecommendHistoryResponse {
7070
pub entries: Vec<RecommendHistoryEntry>,
7171
}
7272

73+
pub type RecommendationRequest = RecommendRequest;
74+
pub type RecommendationResponse = RecommendResponse;
75+

packages/devkit/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
1818

1919
[dev-dependencies]
2020
criterion = { version = "0.5", features = ["html_reports"] }
21+
tempfile = "3"
2122
tokio-test = "0.4"
2223
tracing = "0.1"
2324
tracing-subscriber = "0.3"
25+
uuid = { version = "1", features = ["v4"] }
2426

2527
[[bench]]
2628
name = "fee_model_bench"

packages/devkit/src/utilities/converters.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,11 @@ impl std::fmt::Display for ConversionError {
2929
}
3030

3131
impl std::error::Error for ConversionError {}
32+
33+
pub fn stroops_to_xlm(stroops: u64) -> f64 {
34+
stroop_to_xlm(stroops)
35+
}
36+
37+
pub fn xlm_to_stroops(xlm: f64) -> Result<u64, ConversionError> {
38+
xlm_to_stroop(xlm)
39+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/// Format fee value in stroops with comma separators
2+
pub fn format_stroops(n: u64) -> String {
3+
let s = n.to_string();
4+
let mut result = String::new();
5+
for (i, c) in s.chars().rev().enumerate() {
6+
if i > 0 && i % 3 == 0 {
7+
result.push(',');
8+
}
9+
result.push(c);
10+
}
11+
result.chars().rev().collect::<String>() + " str"
12+
}
13+
14+
/// Format fee value as XLM (1 XLM = 10,000,000 stroops)
15+
pub fn format_xlm(n: u64) -> String {
16+
let xlm = n as f64 / 10_000_000.0;
17+
format!("{:.7} XLM", xlm)
18+
}
19+
20+
/// Auto-format: show stroops below 1M, XLM above
21+
pub fn format_fee_short(n: u64) -> String {
22+
if n < 1_000_000 {
23+
format_stroops(n)
24+
} else {
25+
format_xlm(n)
26+
}
27+
}
28+
29+
#[cfg(test)]
30+
mod tests {
31+
use super::*;
32+
33+
#[test]
34+
fn test_format_stroops() {
35+
assert_eq!(format_stroops(0), "0 str");
36+
assert_eq!(format_stroops(100), "100 str");
37+
assert_eq!(format_stroops(3849), "3,849 str");
38+
assert_eq!(format_stroops(1000000), "1,000,000 str");
39+
}
40+
41+
#[test]
42+
fn test_format_xlm() {
43+
assert_eq!(format_xlm(0), "0.0000000 XLM");
44+
assert_eq!(format_xlm(10_000_000), "1.0000000 XLM");
45+
assert_eq!(format_xlm(3849), "0.0003849 XLM");
46+
}
47+
48+
#[test]
49+
fn test_format_fee_short() {
50+
assert_eq!(format_fee_short(3849), "3,849 str");
51+
assert_eq!(format_fee_short(10_000_000), "1.0000000 XLM");
52+
}
53+
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
pub mod csv_reader;
22
pub mod json_reader;
3-
//! Shared utility helpers used by CLI subcommands.
4-
3+
/// Shared utility helpers used by CLI subcommands.
54
pub mod comparator;
5+
pub mod formatters;
66
pub mod percentile_table;
7+
pub mod time;

packages/devkit/src/utilities/time.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::time::Duration;
22

3+
use chrono::{DateTime, Utc};
4+
35
#[derive(Debug, Clone, PartialEq, Eq)]
46
pub struct ParseError(pub String);
57

@@ -32,6 +34,25 @@ pub fn parse_window(s: &str) -> Result<Duration, ParseError> {
3234
}
3335
}
3436

37+
pub fn now_unix_ms() -> u64 {
38+
std::time::SystemTime::now()
39+
.duration_since(std::time::UNIX_EPOCH)
40+
.unwrap()
41+
.as_millis() as u64
42+
}
43+
44+
pub fn unix_ms_to_datetime(ms: u64) -> DateTime<Utc> {
45+
DateTime::from_timestamp_millis(ms as i64).unwrap_or_default()
46+
}
47+
48+
pub fn datetime_to_unix_ms(dt: &DateTime<Utc>) -> u64 {
49+
dt.timestamp_millis() as u64
50+
}
51+
52+
pub fn ledger_close_time_to_unix_ms(close_time: u64) -> u64 {
53+
close_time * 1000
54+
}
55+
3556
#[cfg(test)]
3657
mod tests {
3758
use super::*;

0 commit comments

Comments
 (0)