-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
53 lines (46 loc) · 1.03 KB
/
utils.go
File metadata and controls
53 lines (46 loc) · 1.03 KB
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
package ohrad
import (
"os"
"os/signal"
"syscall"
"time"
)
func unix2Ntp(tsp int64) uint32 {
return uint32(Jan1970 + tsp)
}
func unix2NtpNanos(tsp int64) uint32 {
return uint32((Jan1970 * 1000 * 1000 * 1000) + tsp)
}
func ntp2Unix(tsp int64) uint32 {
return uint32(tsp - Jan1970)
}
func HandleSignals(cleanup func() error) {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM, syscall.SIGCHLD, syscall.SIGHUP)
go func() {
for s := range c {
switch s {
case os.Interrupt, syscall.SIGTERM:
Log.Info("Gracefully shutting down the daemon")
if err := cleanup(); err != nil {
Log.Err(err.Error())
os.Exit(-1)
} else {
Log.Info("Shut down finished")
os.Exit(0)
}
}
}
}()
}
func getTimeNow() NtpLong {
t := time.Now()
secs := t.Unix()
nanos := t.UnixNano()
left := nanos - (secs * NanosPerSecond)
fraction := float32(left) / float32(NanosPerSecond)
return NtpLong{
IntParl: unix2Ntp(secs),
Fractionl: uint32(float32(fraction) * float32(NanosPerSecond)),
}
}