use e_utils::{chrono::FixedOffset, cmd::Cmd};
use std::time::Duration;
#[cfg(windows)]
pub async fn ensure_windows_time_service() -> e_utils::AnyResult<()> {
let status = Cmd::new("w32tm").args(["/query", "/status"]).a_output().await?;
crate::p(&status.stdout);
if !status.stdout.contains("Leap") {
crate::p("正在重新配置 Windows 时间服务...");
let _ = Cmd::new("w32tm")
.args(["/unregister"])
.a_output()
.await
.inspect(|v| crate::p(format!("取消注册服务: {}", v.stdout)));
let _ = Cmd::new("w32tm")
.args(["/register"])
.a_output()
.await
.inspect(|v| crate::p(format!("注册服务: {}", v.stdout)));
let start_result = Cmd::new("net").args(["start", "w32time"]).a_output().await?;
crate::p(format!("启动服务: {}", start_result.stdout));
if !start_result.status.success() {
return Err("无法启动 Windows 时间服务".into());
}
tokio::time::sleep(Duration::from_secs(2)).await;
}
Ok(())
}
pub async fn sync_datetime(server: &str, is_register: bool) -> e_utils::AnyResult<String> {
#[cfg(not(windows))]
return Err("不支持的系统".into());
#[cfg(windows)]
{
if is_register {
ensure_windows_time_service().await?;
}
let config_result = Cmd::new("w32tm")
.args([
"/config",
&format!("/manualpeerlist:{}", server),
"/syncfromflags:manual",
"/reliable:yes",
"/update",
])
.a_output()
.await?;
if !config_result.status.success() {
return Err("配置时间服务器失败".into());
}
for i in 1..=3 {
crate::wp(format!("正在进行第 {i} 次时间同步尝试连接 {server}..."));
let sync_result = Cmd::new("w32tm").args(["/resync", "/force"]).a_output().await?;
if sync_result.status.success()
&& (sync_result.stdout.contains("成功")
|| sync_result.stdout.contains("success")
|| sync_result.stdout.contains("已成功完成")
|| sync_result.stdout.is_empty())
{
return Ok(format!("时间同步{server}成功",));
}
if i < 3 {
crate::wp("同步失败,等待重试...");
tokio::time::sleep(Duration::from_secs(1)).await;
}
}
Err("时间同步失败,请检查网络连接".into())
}
}
pub fn get_current_timezone() -> FixedOffset {
let local_now = e_utils::chrono::Local::now();
local_now.offset().clone()
}