summaryrefslogtreecommitdiffstats
path: root/src/timeman.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/timeman.rs')
-rw-r--r--src/timeman.rs26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/timeman.rs b/src/timeman.rs
index 32a6f45..64250ee 100644
--- a/src/timeman.rs
+++ b/src/timeman.rs
@@ -12,9 +12,12 @@ pub const HOUR: Second = MINUTE * 60;
pub const DAY: Second = HOUR * 24;
pub const YEAR: Second = DAY * 365;
+pub const SYSTEM_TICK_INTERVAL: Second = 1 * DAY;
+
pub struct TimeMan
{
time: Second,
+ last_time: Second,
pub auto_tick: Option<Second>
}
@@ -24,6 +27,7 @@ impl TimeMan
-> Self {
Self {
time,
+ last_time: time,
auto_tick: None
}
}
@@ -42,11 +46,33 @@ impl TimeMan
pub fn update(
&mut self)
+ -> Vec<Second>
{
match self.auto_tick {
Some(advance) => { self.time += advance; },
None => {}
}
+
+ let time_diff = self.time - self.last_time;
+
+ let tick_diff = self.time / SYSTEM_TICK_INTERVAL -
+ self.last_time / SYSTEM_TICK_INTERVAL;
+
+ if time_diff == 0 || tick_diff == 0 {
+ self.last_time = self.time;
+ return vec![];
+ }
+
+ let time_start = self.last_time - (self.last_time % SYSTEM_TICK_INTERVAL);
+ let time_end = self.time - (self.time % SYSTEM_TICK_INTERVAL);
+
+ let tick_start = time_start / SYSTEM_TICK_INTERVAL;
+ let tick_end = time_end / SYSTEM_TICK_INTERVAL;
+
+ self.last_time = self.time;
+ (tick_start..tick_end).map(|v| {
+ v * SYSTEM_TICK_INTERVAL
+ }).collect::<Vec<_>>()
}
pub fn format_duration(time: Second) -> String