cleanup datetime API deprecation warnings

This commit is contained in:
bunnie 2024-02-08 13:46:24 +08:00
parent df7c31a7df
commit fb68eb9dd2
6 changed files with 27 additions and 16 deletions

View file

@ -211,7 +211,7 @@ pub(crate) fn ntp_updater(time_conn: xous::CID) {
match result {
Ok(time) => {
log::debug!("Got NTP time: {}.{}", time.sec(), time.sec_fraction());
let current_time = Utc.ymd(1970, 1, 1).and_hms(0, 0, 0)
let current_time = Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap()
+ chrono::Duration::seconds(time.sec() as i64);
log::debug!("Setting UTC time: {:?}", current_time.to_string());
xous::send_message(

View file

@ -766,8 +766,8 @@ impl From<PasswordRecord> for Vec<u8> {
/// target
fn utc_now() -> DateTime<Utc> {
let now = SystemTime::now().duration_since(UNIX_EPOCH).expect("system time before Unix epoch");
let naive = NaiveDateTime::from_timestamp(now.as_secs() as i64, now.subsec_nanos() as u32);
DateTime::from_utc(naive, Utc)
let naive = NaiveDateTime::from_timestamp_opt(now.as_secs() as i64, now.subsec_nanos() as u32).unwrap();
DateTime::from_naive_utc_and_offset(naive, Utc)
}
pub fn hex(data: Vec<u8>) -> String {

View file

@ -63,7 +63,10 @@ pub fn atime_to_str(req_atime: u64) -> String {
request_str.push_str(t!("vault.u2f.appinfo.never", locales::LANG));
} else {
let now = utc_now();
let atime = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(req_atime as i64, 0), Utc);
let atime = DateTime::<Utc>::from_naive_utc_and_offset(
NaiveDateTime::from_timestamp_opt(req_atime as i64, 0).unwrap(),
Utc,
);
// avoid format! macro, it is too slow.
if now.signed_duration_since(atime).num_days() > 1 {
request_str.push_str(t!("vault.u2f.appinfo.last_authtime", locales::LANG));
@ -90,8 +93,8 @@ pub fn atime_to_str(req_atime: u64) -> String {
/// target
pub fn utc_now() -> DateTime<Utc> {
let now = SystemTime::now().duration_since(UNIX_EPOCH).expect("system time before Unix epoch");
let naive = NaiveDateTime::from_timestamp(now.as_secs() as i64, now.subsec_nanos() as u32);
DateTime::from_utc(naive, Utc)
let naive = NaiveDateTime::from_timestamp_opt(now.as_secs() as i64, now.subsec_nanos() as u32).unwrap();
DateTime::from_naive_utc_and_offset(naive, Utc)
}
/// app info format:

View file

@ -666,7 +666,7 @@ pub(crate) fn start_time_ux() {
match result {
Ok(time) => {
log::info!("Got NTP time: {}.{}", time.sec(), time.sec_fraction());
let current_time = Utc.ymd(1970, 1, 1).and_hms(0, 0, 0)
let current_time = Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap()
+ chrono::Duration::seconds(time.sec() as i64);
log::info!("Setting UTC time: {:?}", current_time.to_string());
xous::send_message(
@ -744,9 +744,17 @@ pub(crate) fn start_time_ux() {
}
log::info!("Setting time: {}/{}/{} {}:{}:{}", months, days, years, hours, mins, secs);
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);
let new_dt = chrono::FixedOffset::east_opt((tz_offset_ms / 1000) as i32)
.unwrap()
.with_ymd_and_hms(
years as i32 + 2000,
months as u32,
days as u32,
hours as u32,
mins as u32,
secs as u32,
)
.unwrap();
xous::send_message(
timeserver_cid,
Message::new_scalar(

View file

@ -36,8 +36,8 @@ impl<'a> ShellCmdApi<'a> for RtcCmd {
let mut localtime = llio::LocalTime::new();
if let Some(timestamp) = localtime.get_local_time_ms() {
// we "say" UTC but actually local time is in whatever the local time is
let dt = chrono::DateTime::<Utc>::from_utc(
NaiveDateTime::from_timestamp(timestamp as i64 / 1000, 0),
let dt = chrono::DateTime::<Utc>::from_naive_utc_and_offset(
NaiveDateTime::from_timestamp_opt(timestamp as i64 / 1000, 0).unwrap(),
chrono::offset::Utc,
);
let timestr = dt.format("%m/%d/%Y %T").to_string();

View file

@ -530,8 +530,8 @@ fn wrapped_main() -> ! {
// once we have unlocked the PDDB and know our timezone, we'll compare the embedded
// timestamp to our current time, and delete the backup if it's too
// old.
backup_time = Some(chrono::DateTime::<Utc>::from_utc(
NaiveDateTime::from_timestamp(header.timestamp as i64, 0),
backup_time = Some(chrono::DateTime::<Utc>::from_naive_utc_and_offset(
NaiveDateTime::from_timestamp_opt(header.timestamp as i64, 0).unwrap(),
chrono::offset::Utc,
));
}
@ -1234,8 +1234,8 @@ fn wrapped_main() -> ! {
uptime_tv.clear_str();
if let Some(timestamp) = localtime.get_local_time_ms() {
// we "say" UTC but actually local time is in whatever the local time is
let dt = chrono::DateTime::<Utc>::from_utc(
NaiveDateTime::from_timestamp(timestamp as i64 / 1000, 0),
let dt = chrono::DateTime::<Utc>::from_naive_utc_and_offset(
NaiveDateTime::from_timestamp_opt(timestamp as i64 / 1000, 0).unwrap(),
chrono::offset::Utc,
);
let timestr = dt.format("%H:%M %m/%d").to_string();