dbus-settings-bindings/timedate/examples/timedatectl.rs

60 lines
1.7 KiB
Rust
Raw Normal View History

// 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<()> {
2024-02-19 11:28:20 -05:00
let connection = zbus::Connection::system().await?;
let proxy = timedate_zbus::TimeDateProxy::new(&connection).await?;
2024-02-19 11:28:20 -05:00
let ntp_service = if proxy.ntp().await? {
"active"
} else {
"inactive"
};
2024-02-19 11:28:20 -05:00
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();
2024-02-19 11:28:20 -05:00
let rtc_time = (if rtc_in_local {
rtc_ts.to_zoned(tz)
2024-02-19 11:28:20 -05:00
} else {
rtc_ts.to_zoned(utc.clone())
2024-02-19 11:28:20 -05:00
})
.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");
2024-02-19 11:28:20 -05:00
let rtc_in_local = CHOICES[usize::from(rtc_in_local)];
let synchronized = CHOICES[usize::from(proxy.ntp_synchronized().await.unwrap_or_default())];
2024-02-19 11:28:20 -05:00
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}"
2024-02-19 11:28:20 -05:00
);
2024-02-19 11:28:20 -05:00
Ok(())
}