fix: 系统时间同步样式修改 (#1123)

This commit is contained in:
ssongliu
2023-05-24 15:43:21 +08:00
committed by GitHub
parent b979c2574c
commit be6b7157f4
14 changed files with 268 additions and 143 deletions

View File

@@ -31,7 +31,7 @@ type packet struct {
}
func GetRemoteTime(site string) (time.Time, error) {
conn, err := net.Dial("udp", site)
conn, err := net.Dial("udp", site+":123")
if err != nil {
return time.Time{}, fmt.Errorf("failed to connect: %v", err)
}
@@ -59,15 +59,10 @@ func GetRemoteTime(site string) (time.Time, error) {
return showtime, nil
}
func UpdateSystemTime(dateTime, timezone string) error {
func UpdateSystemTime(dateTime string) error {
system := runtime.GOOS
if system == "linux" {
stdout, err := cmd.Execf(`%s timedatectl set-timezone "%s"`, cmd.SudoHandleCmd(), timezone)
if err != nil {
return fmt.Errorf("update system time zone failed, stdout: %s, err: %v", stdout, err)
}
stdout2, err := cmd.Execf(`%s timedatectl set-time "%s"`, cmd.SudoHandleCmd(), dateTime)
stdout2, err := cmd.Execf(`%s date -s "%s"`, cmd.SudoHandleCmd(), dateTime)
if err != nil {
return fmt.Errorf("update system time failed,stdout: %s, err: %v", stdout2, err)
}
@@ -75,3 +70,15 @@ func UpdateSystemTime(dateTime, timezone string) error {
}
return fmt.Errorf("the current system architecture %v does not support synchronization", system)
}
func UpdateSystemTimeZone(timezone string) error {
system := runtime.GOOS
if system == "linux" {
stdout, err := cmd.Execf(`%s timedatectl set-timezone "%s"`, cmd.SudoHandleCmd(), timezone)
if err != nil {
return fmt.Errorf("update system time zone failed, stdout: %s, err: %v", stdout, err)
}
return nil
}
return fmt.Errorf("the current system architecture %v does not support synchronization", system)
}