From dff1ae0fb4db1a1e228d9fdb1928a73b119e7ff7 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Sun, 26 Apr 2020 11:16:59 +0200 Subject: [PATCH 1/3] move duration-formatting to a separate function and add tests for that --- src/context.rs | 8 +++----- src/dc_tools.rs | 43 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/context.rs b/src/context.rs index 0be974ae2..4bc2c3996 100644 --- a/src/context.rs +++ b/src/context.rs @@ -9,6 +9,7 @@ use crate::chat::*; use crate::config::Config; use crate::constants::*; use crate::contact::*; +use crate::dc_tools::duration_to_str; use crate::error::*; use crate::events::Event; use crate::imap::*; @@ -314,11 +315,8 @@ impl Context { ); res.insert("fingerprint", fingerprint_str); - let elapsed = self.creation_time.elapsed().as_secs(); - let hours = elapsed / 3600; - let minutes = elapsed % 3600 / 60; - let seconds = elapsed % 3600 % 60; - res.insert("uptime", format!("{}h {}m {}s", hours, minutes, seconds)); + let elapsed = self.creation_time.elapsed(); + res.insert("uptime", duration_to_str(elapsed.unwrap_or_default())); res } diff --git a/src/dc_tools.rs b/src/dc_tools.rs index b43696b03..0745dca9b 100644 --- a/src/dc_tools.rs +++ b/src/dc_tools.rs @@ -5,7 +5,7 @@ use core::cmp::{max, min}; use std::borrow::Cow; use std::path::{Path, PathBuf}; use std::str::FromStr; -use std::time::SystemTime; +use std::time::{Duration, SystemTime}; use std::{fmt, fs}; 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() } +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 { /* returns the offset that must be _added_ to an UTC/GMT-time to create the localtime. the function may return negative values. */ @@ -854,4 +862,37 @@ mod tests { let next = dc_smeared_time(&t.ctx); 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" + ); + } } From 39cb9c425cb0a2147f68f5b147bee717c57dacee Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Sun, 26 Apr 2020 11:44:12 +0200 Subject: [PATCH 2/3] fixup --- src/dc_tools.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dc_tools.rs b/src/dc_tools.rs index 0745dca9b..1adaa5ed8 100644 --- a/src/dc_tools.rs +++ b/src/dc_tools.rs @@ -78,8 +78,8 @@ pub fn dc_timestamp_to_str(wanted: i64) -> 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; + let m = (secs % 3600) / 60; + let s = (secs % 3600) % 60; format!("{}h {}m {}s", h, m, s) } From 432e4b7f0a961d7e6601bbefba2917246b117f98 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Sun, 26 Apr 2020 11:19:37 +0200 Subject: [PATCH 3/3] use std::time::SystemTime for uptime calculation std::time::Instant does not count eg. doze-time on android --- src/context.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/context.rs b/src/context.rs index 4bc2c3996..19e47f41c 100644 --- a/src/context.rs +++ b/src/context.rs @@ -22,7 +22,7 @@ use crate::message::{self, Message, MessengerMessage, MsgId}; use crate::param::Params; use crate::smtp::Smtp; use crate::sql::Sql; -use std::time::Instant; +use std::time::SystemTime; /// Callback function type for [Context] /// @@ -59,7 +59,7 @@ pub struct Context { /// Mutex to avoid generating the key for the user more than once. pub generating_key_mutex: Mutex<()>, pub translated_stockstrings: RwLock>, - creation_time: Instant, + creation_time: SystemTime, } #[derive(Debug, PartialEq, Eq)] @@ -141,7 +141,7 @@ impl Context { perform_inbox_jobs_needed: Arc::new(RwLock::new(false)), generating_key_mutex: Mutex::new(()), translated_stockstrings: RwLock::new(HashMap::new()), - creation_time: std::time::Instant::now(), + creation_time: std::time::SystemTime::now(), }; ensure!(