-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract current time from HTTP responses and keep track of it
- Loading branch information
Showing
5 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,7 @@ mod keypad; | |
mod nfc; | ||
mod pn532; | ||
mod screen; | ||
mod time; | ||
mod ui; | ||
mod user; | ||
mod vereinsflieger; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use chrono::{DateTime, TimeDelta, Utc}; | ||
use core::cell::RefCell; | ||
use embassy_sync::blocking_mutex::CriticalSectionMutex; | ||
use log::debug; | ||
|
||
/// Calculated time of system start | ||
static SYSTEM_START_TIME: CriticalSectionMutex<RefCell<Option<DateTime<Utc>>>> = | ||
CriticalSectionMutex::new(RefCell::new(None)); | ||
|
||
/// Time of system start | ||
fn start_time() -> Option<DateTime<Utc>> { | ||
SYSTEM_START_TIME.lock(|sst| *sst.borrow()) | ||
} | ||
|
||
/// Set time of system start | ||
fn set_start_time(time: DateTime<Utc>) { | ||
SYSTEM_START_TIME.lock(|sst| { | ||
*sst.borrow_mut() = Some(time); | ||
}); | ||
} | ||
|
||
/// Duration of system run time | ||
pub fn uptime() -> Option<TimeDelta> { | ||
let millis = esp_hal::time::now().duration_since_epoch().to_millis(); | ||
TimeDelta::try_milliseconds(i64::try_from(millis).ok()?) | ||
} | ||
|
||
/// Current time | ||
#[allow(dead_code)] | ||
pub fn now() -> Option<DateTime<Utc>> { | ||
if let (Some(start_time), Some(uptime)) = (start_time(), uptime()) { | ||
Some(start_time + uptime) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
/// Set current time by using the given current time to calculate the time of system start | ||
pub fn set(now: DateTime<Utc>) { | ||
if let Some(uptime) = uptime() { | ||
set_start_time(now - uptime); | ||
debug!("Time: Current time set to {}", now); | ||
} | ||
} |