From 31b697819cdee2c28fb1839589a389e39156e414 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Thu, 31 Oct 2024 09:41:40 +1300 Subject: Fix issue with clock device returning UTC time instead of local time The project previously used the `time` crate for getting the current time in the local timezone. It was discovered that the time crate would fail to return the local time when the program used multiple threads, because of concerns that the other threads could modify timezone-related environment variables while the timezone was being read from the system and create a soundness issue. A second program thread was introduced recently by commit 1a830a3 to provide non-blocking reads of standard input for the local stream device, and so the time returned by the clock device was falling back to UTC. To fix this, the `time` crate has been replaced by the `chrono` crate which does not suffer from this restriction, and in any case seems to be the more popular of the two anyway. --- src/devices/clock_device.rs | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) (limited to 'src/devices/clock_device.rs') diff --git a/src/devices/clock_device.rs b/src/devices/clock_device.rs index 494e0c7..7cc877e 100644 --- a/src/devices/clock_device.rs +++ b/src/devices/clock_device.rs @@ -1,20 +1,9 @@ use bedrock_core::*; +use chrono::prelude::*; use std::time::{Duration, Instant}; -macro_rules! now { () => { - time::OffsetDateTime::now_local() - .unwrap_or_else(|_| time::OffsetDateTime::now_utc()) -}; } - -fn current_year() -> u8 { now!().year().saturating_sub(2000) as u8 } -fn current_month() -> u8 { now!().month() as u8 - 1 } -fn current_day() -> u8 { now!().day() as u8 - 1 } -fn current_hour() -> u8 { now!().hour() } -fn current_minute() -> u8 { now!().minute() } -fn current_second() -> u8 { now!().second() } - macro_rules! fn_read_timer { ($fn_name:ident($read:ident, $end:ident)) => { pub fn $fn_name(&mut self) { @@ -123,12 +112,12 @@ impl ClockDevice { impl Device for ClockDevice { fn read(&mut self, port: u8) -> u8 { match port { - 0x0 => current_year(), - 0x1 => current_month(), - 0x2 => current_day(), - 0x3 => current_hour(), - 0x4 => current_minute(), - 0x5 => current_second(), + 0x0 => Local::now().year().saturating_sub(2000) as u8, + 0x1 => Local::now().month().saturating_sub(1) as u8, + 0x2 => Local::now().day().saturating_sub(1) as u8, + 0x3 => Local::now().hour() as u8, + 0x4 => Local::now().minute() as u8, + 0x5 => Local::now().second() as u8, 0x6 => { self.read_uptime(); read_h!(self.uptime_read) }, 0x7 => read_l!(self.uptime_read), 0x8 => { self.read_t1(); read_h!(self.t1_read) }, -- cgit v1.2.3-70-g09d2