Skip to content

Commit

Permalink
Accept any timezone when setting current time
Browse files Browse the repository at this point in the history
  • Loading branch information
zargony committed Dec 4, 2024
1 parent bf6e7b2 commit 2abe2a8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
7 changes: 3 additions & 4 deletions firmware/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::json::{self, FromJson, ToJson};
use crate::time;
use crate::wifi::{DnsSocket, TcpClient, TcpConnection, Wifi};
use alloc::vec::Vec;
use chrono::{DateTime, Utc};
use chrono::DateTime;
use core::convert::Infallible;
use core::{fmt, str};
use embedded_io_async::{BufRead, Read};
Expand Down Expand Up @@ -219,10 +219,9 @@ impl Connection<'_> {
.headers()
.find_map(|(k, v)| (k == "Date").then_some(v))
.and_then(|v| str::from_utf8(v).ok())
.and_then(|s| DateTime::parse_from_rfc2822(s).ok())
.map(|d| d.with_timezone(&Utc));
.and_then(|s| DateTime::parse_from_rfc2822(s).ok());
if let Some(time) = time {
time::set(time);
time::set(&time);
}

// Check HTTP response status
Expand Down
7 changes: 4 additions & 3 deletions firmware/src/time.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use chrono::{DateTime, TimeDelta, Utc};
use chrono::{DateTime, TimeDelta, TimeZone, Utc};
use core::cell::RefCell;
use embassy_sync::blocking_mutex::CriticalSectionMutex;
use embassy_time::Instant;
Expand Down Expand Up @@ -49,13 +49,14 @@ pub fn uptime() -> Option<TimeDelta> {
Instant::now().to_duration()
}

/// Current time
/// Current time. Always given in UTC since the local timezone is unknown.
pub fn now() -> Option<DateTime<Utc>> {
Instant::now().to_datetime()
}

/// Set current time by using the given current time to calculate the time of system start
pub fn set(now: DateTime<Utc>) {
pub fn set<TZ: TimeZone>(now: &DateTime<TZ>) {
let now = now.with_timezone(&Utc);
if let Some(uptime) = uptime() {
set_start_time(now - uptime);
debug!("Time: Current time set to {}", now);
Expand Down

0 comments on commit 2abe2a8

Please sign in to comment.