From c78273f5408ebb7b444c3c3b8da460b6e75125e3 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 28 Mar 2022 03:37:27 +0800 Subject: [PATCH] refactor time setting to make a little more sense now you set time in local time. If time zone hasn't been set already, it'll prompt you to enter a time zone. The main oddity is now that you have to know your time zone as an offset from UTC. This can be fixed later with a proper symbolic TZ to numeric mapping, but, that's actually a surprisingly hard feature to implement. --- services/llio/locales/i18n.json | 110 ---------------------------- services/status/locales/i18n.json | 114 ++++++++++++++++++++++++++++-- services/status/src/main.rs | 45 ++++++++++-- services/status/src/time.rs | 4 +- 4 files changed, 151 insertions(+), 122 deletions(-) delete mode 100644 services/llio/locales/i18n.json diff --git a/services/llio/locales/i18n.json b/services/llio/locales/i18n.json deleted file mode 100644 index 0b3289f91..000000000 --- a/services/llio/locales/i18n.json +++ /dev/null @@ -1,110 +0,0 @@ -{ - "rtc.month": { - "en": "Enter month (1-12)", - "ja": "月(1-12)を入力してください。", - "zh": "输入月份 (1-12)", - "en-tts": "Enter month one through twelve" - }, - "rtc.day": { - "en": "Enter day (1-31)", - "ja": "日数 (1-31) を入力してください。", - "zh": "输入日期 (1-31)", - "en-tts": "Enter day one through 31" - }, - "rtc.year": { - "en": "Enter last two digits of year", - "ja": "西暦の下2桁を入力してください。", - "zh": "输入年份 (最后两位数)", - "en-tts": "Enter last two digits of year" - }, - "rtc.bodge": { - "en": "Enter the system time in ⚠UTC⚠.\nThis is likely not the same thing as your local time.", - "ja": "システム時刻を⚠UTC⚠に入力します。\nこれは現地時間と同じではない可能性があります。", - "zh": "以⚠UTC⚠输入系统时间。\n这可能与您的本地时间不同。", - "en-tts": "Enter the system time in ⚠UTC⚠.\nThis is likely not the same thing as your local time." - }, - "rtc.hour": { - "en": "Enter hours for UTC (0-23)", - "ja": "UTC時間(0-23)を入力してください。", - "zh": "输入UTC小时 (0-23)", - "en-tts": "Enter hours for UTC as 24 hour format" - }, - "rtc.minute": { - "en": "Enter minutes (0-59)", - "ja": "分(0-59)を入力してください。", - "zh": "输入分钟 (0-59)", - "en-tts": "Enter minutes" - }, - "rtc.seconds": { - "en": "Enter seconds (0-59)", - "ja": "秒 (0-59) を入力してください。", - "zh": "输入秒数 (0-59)", - "en-tts": "Enter seconds" - }, - "rtc.day_of_week": { - "en": "Select the day of week", - "ja": "曜日を選択してください。", - "zh": "[星期几]清单框", - "en-tts": "Select the day of week" - }, - "rtc.monday": { - "en": "Monday", - "ja": "月曜日", - "zh": "星期一", - "en-tts": "Monday" - }, - "rtc.tuesday": { - "en": "Tuesday", - "ja": "火曜日", - "zh": "星期二", - "en-tts": "Tuesday" - }, - "rtc.wednesday": { - "en": "Wednesday", - "ja": "水曜日", - "zh": "星期三", - "en-tts": "Wednesday" - }, - "rtc.thursday": { - "en": "Thursday", - "ja": "木曜日", - "zh": "星期四", - "en-tts": "Thursday" - }, - "rtc.friday": { - "en": "Friday", - "ja": "金曜日", - "zh": "星期五", - "en-tts": "Friday" - }, - "rtc.saturday": { - "en": "Saturday", - "ja": "土曜日", - "zh": "星期六", - "en-tts": "Saturday" - }, - "rtc.sunday": { - "en": "Sunday", - "ja": "日曜日", - "zh": "星期日", - "en-tts": "Sunday" - }, - "rtc.timezone": { - "en": "Please enter your local offset from UTC in hours (-12.0 to +14.0 hours).\nNote: this offset does not yet track daylight savings.", - "ja": "UTCからのローカルオフセットを時間単位で入力してください(-12.0〜 + 14.0時間):", - "zh": "请以小时为单位输入您与 UTC 的本地偏移量(-12.0 到 +14.0 小时):", - "en-tts": "Please enter your local offset from UTC in hours (-12.0 to +14.0 hours):" - }, - "rtc.integer_err": { - "en": "Error: entry was not numeric", - "ja": "エラー:エントリは数値ではありませんでした。", - "zh": "错误:输入不是数字", - "en-tts": "Error: entry was not numeric" - }, - "rtc.range_err": { - "en": "Error: input out of range", - "ja": "エラー:入力が範囲外です。", - "zh": "错误:输入超出范围", - "en-tts": "Error: input out of range" - } -} \ No newline at end of file diff --git a/services/status/locales/i18n.json b/services/status/locales/i18n.json index f74fbaaec..471cc5ac3 100644 --- a/services/status/locales/i18n.json +++ b/services/status/locales/i18n.json @@ -109,16 +109,16 @@ "en-tts": "Sign Xous update" }, "mainmenu.set_rtc": { - "en": "Set UTC time", - "ja": "UTC 時間設定", - "zh": "设置UTC时间", - "en-tts": "Set UTC time" + "en": "Set time", + "ja": "時間設定", + "zh": "设置时间", + "en-tts": "Set time" }, "mainmenu.set_tz": { - "en": "Set local timezone offset", + "en": "Set timezone", "ja": "タイムゾーンを設定", "zh": "设置本地时区", - "en-tts": "Set local timezone offset" + "en-tts": "Set timezone" }, "mainmenu.pddb": { "en": "PDDB Submenu", @@ -167,5 +167,107 @@ "ja": "Shellchat", "zh": "外壳聊天", "en-tts": "Shellchat" + }, + "rtc.month": { + "en": "Enter month (1-12)", + "ja": "月(1-12)を入力してください。", + "zh": "输入月份 (1-12)", + "en-tts": "Enter month one through twelve" + }, + "rtc.day": { + "en": "Enter day (1-31)", + "ja": "日数 (1-31) を入力してください。", + "zh": "输入日期 (1-31)", + "en-tts": "Enter day one through 31" + }, + "rtc.year": { + "en": "Enter last two digits of year", + "ja": "西暦の下2桁を入力してください。", + "zh": "输入年份 (最后两位数)", + "en-tts": "Enter last two digits of year" + }, + "rtc.hour": { + "en": "Enter hours in local timezone (0-23)", + "ja": "時間(0-23)を入力してください。", + "zh": "输入小时 (0-23)", + "en-tts": "Enter hours for local timezone as 24 hour format" + }, + "rtc.minute": { + "en": "Enter minutes (0-59)", + "ja": "分(0-59)を入力してください。", + "zh": "输入分钟 (0-59)", + "en-tts": "Enter minutes" + }, + "rtc.seconds": { + "en": "Enter seconds (0-59)", + "ja": "秒 (0-59) を入力してください。", + "zh": "输入秒数 (0-59)", + "en-tts": "Enter seconds" + }, + "rtc.day_of_week": { + "en": "Select the day of week", + "ja": "曜日を選択してください。", + "zh": "[星期几]清单框", + "en-tts": "Select the day of week" + }, + "rtc.monday": { + "en": "Monday", + "ja": "月曜日", + "zh": "星期一", + "en-tts": "Monday" + }, + "rtc.tuesday": { + "en": "Tuesday", + "ja": "火曜日", + "zh": "星期二", + "en-tts": "Tuesday" + }, + "rtc.wednesday": { + "en": "Wednesday", + "ja": "水曜日", + "zh": "星期三", + "en-tts": "Wednesday" + }, + "rtc.thursday": { + "en": "Thursday", + "ja": "木曜日", + "zh": "星期四", + "en-tts": "Thursday" + }, + "rtc.friday": { + "en": "Friday", + "ja": "金曜日", + "zh": "星期五", + "en-tts": "Friday" + }, + "rtc.saturday": { + "en": "Saturday", + "ja": "土曜日", + "zh": "星期六", + "en-tts": "Saturday" + }, + "rtc.sunday": { + "en": "Sunday", + "ja": "日曜日", + "zh": "星期日", + "en-tts": "Sunday" + }, + "rtc.timezone": { + "en": "Please enter your local offset from UTC in hours (-12.0 to +14.0 hours).\nNote: Precursor does not yet track daylight savings.", + "ja": "UTCからのローカルオフセットを時間単位で入力してください(-12.0〜 + 14.0時間):", + "zh": "请以小时为单位输入您与 UTC 的本地偏移量(-12.0 到 +14.0 小时):", + "en-tts": "Please enter your local offset from UTC in hours (-12.0 to +14.0 hours):" + }, + "rtc.integer_err": { + "en": "Error: entry was not numeric", + "ja": "エラー:エントリは数値ではありませんでした。", + "zh": "错误:输入不是数字", + "en-tts": "Error: entry was not numeric" + }, + "rtc.range_err": { + "en": "Error: input out of range", + "ja": "エラー:入力が範囲外です。", + "zh": "错误:输入超出范围", + "en-tts": "Error: input out of range" } } \ No newline at end of file diff --git a/services/status/src/main.rs b/services/status/src/main.rs index 12009f4e5..bdac0c756 100644 --- a/services/status/src/main.rs +++ b/services/status/src/main.rs @@ -24,6 +24,7 @@ use chrono::prelude::*; use std::collections::HashMap; use std::sync::{Arc, Mutex}; use std::sync::atomic::{AtomicBool, Ordering}; +use std::io::Read; #[cfg_attr( not(any(target_os = "none", target_os = "xous")), @@ -590,6 +591,44 @@ fn xmain() -> ! { continue; } pump_run.store(false, Ordering::Relaxed); // stop status updates while we do this + + let mut tz_set_handle = pddb::Pddb::new(); + let mut tz_set = false; + let mut tz_offset_ms = 0i64; + let maybe_tz_set_key = tz_set_handle.get( + time::TIME_SERVER_DICT, + time::TIME_SERVER_TZ_OFFSET, + None, false, false, + None, + None:: + ).ok(); + if let Some(mut tz_set_key) = maybe_tz_set_key { + let mut tz_buf = [0u8; 8]; + if tz_set_key.read(&mut tz_buf).unwrap_or(0) == 8 { + tz_offset_ms = i64::from_le_bytes(tz_buf); + tz_set = true; + } + } + // note that we don't do an "else" here because we also want to catch the case of + // a key exists, but nothing was written to it (length of key was 0 or inappropriate) + if !tz_set { + let tz = modals.get_text( + t!("rtc.timezone", xous::LANG), + Some(tz_ux_validator), None + ).expect("couldn't get timezone").as_str() + .parse::().expect("pre-validated input failed to re-parse!"); + log::info!("got tz offset {}", tz); + tz_offset_ms = (tz * 3600.0 * 1000.0) as i64; + xous::send_message(timeserver_cid, + Message::new_scalar( + crate::time::TimeOp::SetTzOffsetMs.to_usize().unwrap(), + (tz_offset_ms >> 32) as usize, + (tz_offset_ms & 0xFFFF_FFFF) as usize, + 0, 0, + ) + ).expect("couldn't set timezone"); + } + let secs: u8; let mins: u8; let hours: u8; @@ -618,8 +657,6 @@ fn xmain() -> ! { .parse::().expect("pre-validated input failed to re-parse!"); log::debug!("got years {}", years); - modals.show_notification(t!("rtc.bodge", xous::LANG)).expect("couldn't show bodge note"); - hours = modals.get_text( t!("rtc.hour", xous::LANG), Some(rtc_ux_validator), Some(ValidatorOp::UxHour.to_u32().unwrap()) @@ -642,8 +679,8 @@ fn xmain() -> ! { log::debug!("got seconds {}", secs); log::info!("Setting time: {}/{}/{} {}:{}:{}", months, days, years, hours, mins, secs); - let new_dt = chrono::Utc.ymd(years as i32 + 2000, months as u32, days as u32) - .and_hms(hours as u32, mins as u32, secs as u32); + let new_dt = chrono::FixedOffset::east((tz_offset_ms / 1000) as i32).ymd(years as i32 + 2000, months as u32, days as u32) + .and_hms(hours as u32, mins as u32, secs as u32); xous::send_message(timeserver_cid, Message::new_scalar( crate::time::TimeOp::SetUtcTimeMs.to_usize().unwrap(), diff --git a/services/status/src/time.rs b/services/status/src/time.rs index 7556a2508..d0a2381a7 100644 --- a/services/status/src/time.rs +++ b/services/status/src/time.rs @@ -35,11 +35,11 @@ use num_traits::*; pub const TIME_SERVER_PUBLIC: &'static [u8; 16] = b"timeserverpublic"; /// Dictionary for RTC settings. -const TIME_SERVER_DICT: &'static str = "sys.rtc"; +pub(crate) const TIME_SERVER_DICT: &'static str = "sys.rtc"; /// This is the UTC offset from the current hardware RTC reading. This should be fixed once time is set. const TIME_SERVER_UTC_OFFSET: &'static str = "utc_offset"; /// This is the offset from UTC to the display time zone. This can vary when the user changes time zones. -const TIME_SERVER_TZ_OFFSET: &'static str = "tz_offset"; +pub(crate) const TIME_SERVER_TZ_OFFSET: &'static str = "tz_offset"; const CTL3: usize = 0; const SECS: usize = 1;