aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorJoris2022-02-13 12:17:00 +0100
committerJoris2022-02-13 12:17:00 +0100
commit8a29f30fb2a949c03b318c4f7699136a8001be37 (patch)
tree51decc33aa776201bc800dc2196bc4f8b72337d7 /src/util
parent8170fb5e432cc81986479a6a3a400e009426d76a (diff)
Synchronize deck only if necessary
Look at the modification time of the deck, and synchronize if it has been modified after the last deck read.
Diffstat (limited to 'src/util')
-rw-r--r--src/util/time.rs28
1 files changed, 25 insertions, 3 deletions
diff --git a/src/util/time.rs b/src/util/time.rs
index f88955d..d9a9f72 100644
--- a/src/util/time.rs
+++ b/src/util/time.rs
@@ -1,9 +1,14 @@
use anyhow::Result;
+use std::thread;
use std::time::SystemTime;
-pub fn now() -> Result<u64> {
- Ok(SystemTime::now()
- .duration_since(SystemTime::UNIX_EPOCH)?
+pub fn seconds_since_unix_epoch() -> Result<u64> {
+ Ok(seconds_since_unix_epoch_of(SystemTime::now())?)
+}
+
+pub fn seconds_since_unix_epoch_of(time: SystemTime) -> Result<u64> {
+ Ok(time
+ .duration_since(std::time::SystemTime::UNIX_EPOCH)?
.as_secs())
}
@@ -31,3 +36,20 @@ fn plural(n: u64, str: &str) -> String {
format!("{} {}s", n, str)
}
}
+
+/// Call the function, then sleep if necessary.
+///
+/// Calling this will at least take the duration asked for in parameters.
+pub fn wait_at_least<F>(f: F, d: std::time::Duration) -> Result<()>
+where
+ F: Fn() -> Result<()>,
+{
+ let t1 = SystemTime::now();
+ f()?;
+ let t2 = SystemTime::now();
+ let elapsed = t2.duration_since(t1)?;
+ if elapsed < d {
+ thread::sleep(d - elapsed);
+ }
+ Ok(())
+}