dbus-settings-bindings/timedate/examples/timedatectl.rs
Vukašin Vojinović cc80763ffc refactor: replace time with jiff
Also updates the `timedatectl` example from `chrono` to `jiff`.
2026-03-13 15:20:44 +01:00

59 lines
1.7 KiB
Rust

// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
use jiff::{Timestamp, tz::TimeZone};
const TZ_FORMAT: &str = "%a %Y-%m-%d %H:%M:%S %Z";
const RTC_FORMAT: &str = "%a %Y-%m-%d %H:%M:%S";
const CHOICES: &[&str] = &["no", "yes"];
#[tokio::main]
pub async fn main() -> zbus::Result<()> {
let connection = zbus::Connection::system().await?;
let proxy = timedate_zbus::TimeDateProxy::new(&connection).await?;
let ntp_service = if proxy.ntp().await? {
"active"
} else {
"inactive"
};
let rtc_in_local = proxy.local_rtc().await?;
let rtc_time_usecs = proxy.rtctime_usec().await?;
let time_usecs = proxy.time_usec().await?;
let timezone = proxy.timezone().await?;
let tz = TimeZone::get(&timezone).unwrap();
let utc = TimeZone::UTC;
let datetime = Timestamp::from_microsecond(time_usecs as i64)
.unwrap()
.to_zoned(tz.clone());
let rtc_ts = Timestamp::from_microsecond(rtc_time_usecs as i64).unwrap();
let rtc_time = (if rtc_in_local {
rtc_ts.to_zoned(tz)
} else {
rtc_ts.to_zoned(utc.clone())
})
.strftime(RTC_FORMAT);
let local = datetime.strftime(TZ_FORMAT);
let universal = datetime.with_time_zone(utc).strftime(TZ_FORMAT);
let tz_string = datetime.strftime("%Z, %z");
let rtc_in_local = CHOICES[usize::from(rtc_in_local)];
let synchronized = CHOICES[usize::from(proxy.ntp_synchronized().await.unwrap_or_default())];
println!(
" Local time: {local}
Universal time: {universal}
RTC time: {rtc_time}
Time zone: {timezone} ({tz_string})
System clock synchronized: {synchronized}
NTP Service: {ntp_service}
RTC in local TZ: {rtc_in_local}"
);
Ok(())
}