-
Notifications
You must be signed in to change notification settings - Fork 3
/
systemTime.go
47 lines (40 loc) · 1.13 KB
/
systemTime.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package timeKit
import (
"fmt"
"github.com/richelieu-yang/chimera/v2/src/cmdKit"
"github.com/richelieu-yang/chimera/v2/src/core/strKit"
"time"
)
// GetSystemTime 获取系统时间(机器时间;本地时间;time.Local).
func GetSystemTime() time.Time {
return time.Now()
}
// SetSystemTime 设置系统时间(机器时间)
/*
PS:
(1) 通过date命令设置root权限,需要root权限.
(2) 只能精确到"秒".
@param password root用户的密码
*/
func SetSystemTime(t time.Time, rootPassword string) error {
// 将时间转换为(date命令认可的)字符串
format := "010215042006.05"
timeStr := Format(t, TimeFormat(format))
var script string
if strKit.IsEmpty(rootPassword) {
script = fmt.Sprintf("date %s", timeStr)
} else {
script = fmt.Sprintf(`echo "%s" | sudo -S date %s`, rootPassword, timeStr)
}
_, err := cmdKit.ExecuteToString("sh", "-c", script)
return err
}
// CorrectSystemTime (根据网络时间)纠正系统时间
func CorrectSystemTime(rootPassword string) (t time.Time, err error) {
t, _, err = GetNetworkTime()
if err != nil {
return
}
err = SetSystemTime(t, rootPassword)
return
}