-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.go
96 lines (81 loc) · 1.81 KB
/
clock.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package connman
import (
"fmt"
"os/exec"
"time"
)
type Clock struct {
Method string `json:"method"`
Hours int `json:"hours"`
Minutes int `json:"mins"`
Year int `json:"year"`
Month int `json:"month"`
Day int `json:"day"`
}
func (c *Clock) FromTime(t time.Time) {
var m time.Month
c.Hours, c.Minutes, _ = t.Clock()
c.Year, m, c.Day = t.Date()
c.Month = int(m)
}
func (c *Clock) ToTime() (time.Time, error) {
str := fmt.Sprintf("%v/%v/%v %v:%v", c.Day, c.Month, c.Year, c.Hours, c.Minutes)
tm, err := time.Parse("2/1/2006 15:4", str)
return tm, err
}
func (c *Clock) Parse(hours, mins, year, month, day string) error {
str := fmt.Sprintf("%v/%v/%v %v:%v", day, month, year, hours, mins)
tm, err := time.Parse("2/1/2006 15:4", str)
if err != nil {
return err
}
c.FromTime(tm)
return nil
}
func GetTime() *Clock {
c := &Clock{}
c.FromTime(time.Now())
c.Method, _ = GetTimeMethod()
return c
}
func SetManualTime(c Clock) error {
time, err := c.ToTime()
if err != nil {
return err
}
ck, err := DBusClock()
if err != nil {
return err
}
if err := ck.Set("TimeUpdates", "manual"); err != nil {
return err
}
if err := ck.Set("Time", uint64(time.Unix())); err != nil {
return err
}
// Save the time to HW clock
return exec.Command("hwclock", "-w").Run()
}
func SetAutoTime() error {
ck, err := DBusClock()
if err != nil {
return err
}
err = ck.Set("TimeUpdates", "auto")
if err != nil {
return err
}
// Force time sync
exec.Command("systemctl", "stop", "ntpd").Run()
exec.Command("ntpdate", "-s", "0.develer.pool.ntp.org").Run()
exec.Command("systemctl", "start", "ntpd").Run()
return nil
}
func GetTimeMethod() (string, error) {
ck, err := DBusClock()
if err != nil {
return "", nil
}
method, err := ck.Get("TimeUpdates")
return method.(string), err
}