mirror of
https://github.com/chatmail/core.git
synced 2026-05-08 17:36:29 +03:00
move duration-formatting to a separate function and add tests for that
This commit is contained in:
@@ -9,6 +9,7 @@ use crate::chat::*;
|
|||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
use crate::constants::*;
|
use crate::constants::*;
|
||||||
use crate::contact::*;
|
use crate::contact::*;
|
||||||
|
use crate::dc_tools::duration_to_str;
|
||||||
use crate::error::*;
|
use crate::error::*;
|
||||||
use crate::events::Event;
|
use crate::events::Event;
|
||||||
use crate::imap::*;
|
use crate::imap::*;
|
||||||
@@ -314,11 +315,8 @@ impl Context {
|
|||||||
);
|
);
|
||||||
res.insert("fingerprint", fingerprint_str);
|
res.insert("fingerprint", fingerprint_str);
|
||||||
|
|
||||||
let elapsed = self.creation_time.elapsed().as_secs();
|
let elapsed = self.creation_time.elapsed();
|
||||||
let hours = elapsed / 3600;
|
res.insert("uptime", duration_to_str(elapsed.unwrap_or_default()));
|
||||||
let minutes = elapsed % 3600 / 60;
|
|
||||||
let seconds = elapsed % 3600 % 60;
|
|
||||||
res.insert("uptime", format!("{}h {}m {}s", hours, minutes, seconds));
|
|
||||||
|
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ use core::cmp::{max, min};
|
|||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::time::SystemTime;
|
use std::time::{Duration, SystemTime};
|
||||||
use std::{fmt, fs};
|
use std::{fmt, fs};
|
||||||
|
|
||||||
use chrono::{Local, TimeZone};
|
use chrono::{Local, TimeZone};
|
||||||
@@ -75,6 +75,14 @@ pub fn dc_timestamp_to_str(wanted: i64) -> String {
|
|||||||
ts.format("%Y.%m.%d %H:%M:%S").to_string()
|
ts.format("%Y.%m.%d %H:%M:%S").to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn duration_to_str(duration: Duration) -> String {
|
||||||
|
let secs = duration.as_secs();
|
||||||
|
let h = secs / 3600;
|
||||||
|
let m = secs % 3600 / 60;
|
||||||
|
let s = secs % 3600 % 60;
|
||||||
|
format!("{}h {}m {}s", h, m, s)
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn dc_gm2local_offset() -> i64 {
|
pub(crate) fn dc_gm2local_offset() -> i64 {
|
||||||
/* returns the offset that must be _added_ to an UTC/GMT-time to create the localtime.
|
/* returns the offset that must be _added_ to an UTC/GMT-time to create the localtime.
|
||||||
the function may return negative values. */
|
the function may return negative values. */
|
||||||
@@ -854,4 +862,37 @@ mod tests {
|
|||||||
let next = dc_smeared_time(&t.ctx);
|
let next = dc_smeared_time(&t.ctx);
|
||||||
assert!((start + count - 1) < next);
|
assert!((start + count - 1) < next);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_duration_to_str() {
|
||||||
|
assert_eq!(duration_to_str(Duration::from_secs(0)), "0h 0m 0s");
|
||||||
|
assert_eq!(duration_to_str(Duration::from_secs(59)), "0h 0m 59s");
|
||||||
|
assert_eq!(duration_to_str(Duration::from_secs(60)), "0h 1m 0s");
|
||||||
|
assert_eq!(duration_to_str(Duration::from_secs(61)), "0h 1m 1s");
|
||||||
|
assert_eq!(duration_to_str(Duration::from_secs(59 * 60)), "0h 59m 0s");
|
||||||
|
assert_eq!(
|
||||||
|
duration_to_str(Duration::from_secs(59 * 60 + 59)),
|
||||||
|
"0h 59m 59s"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
duration_to_str(Duration::from_secs(59 * 60 + 60)),
|
||||||
|
"1h 0m 0s"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
duration_to_str(Duration::from_secs(2 * 60 * 60 + 59 * 60 + 59)),
|
||||||
|
"2h 59m 59s"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
duration_to_str(Duration::from_secs(2 * 60 * 60 + 59 * 60 + 60)),
|
||||||
|
"3h 0m 0s"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
duration_to_str(Duration::from_secs(3 * 60 * 60 + 59)),
|
||||||
|
"3h 0m 59s"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
duration_to_str(Duration::from_secs(3 * 60 * 60 + 60)),
|
||||||
|
"3h 1m 0s"
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user