...
1
2
3
4
5
6
7 package unix
8
9 import "time"
10
11
12 func TimespecToNsec(ts Timespec) int64 { return ts.Nano() }
13
14
15 func NsecToTimespec(nsec int64) Timespec {
16 sec := nsec / 1e9
17 nsec = nsec % 1e9
18 if nsec < 0 {
19 nsec += 1e9
20 sec--
21 }
22 return setTimespec(sec, nsec)
23 }
24
25
26
27
28
29 func TimeToTimespec(t time.Time) (Timespec, error) {
30 sec := t.Unix()
31 nsec := int64(t.Nanosecond())
32 ts := setTimespec(sec, nsec)
33
34
35
36
37 if int64(ts.Sec) != sec {
38 return Timespec{}, ERANGE
39 }
40 return ts, nil
41 }
42
43
44 func TimevalToNsec(tv Timeval) int64 { return tv.Nano() }
45
46
47 func NsecToTimeval(nsec int64) Timeval {
48 nsec += 999
49 usec := nsec % 1e9 / 1e3
50 sec := nsec / 1e9
51 if usec < 0 {
52 usec += 1e6
53 sec--
54 }
55 return setTimeval(sec, usec)
56 }
57
58
59 func (ts *Timespec) Unix() (sec int64, nsec int64) {
60 return int64(ts.Sec), int64(ts.Nsec)
61 }
62
63
64 func (tv *Timeval) Unix() (sec int64, nsec int64) {
65 return int64(tv.Sec), int64(tv.Usec) * 1000
66 }
67
68
69 func (ts *Timespec) Nano() int64 {
70 return int64(ts.Sec)*1e9 + int64(ts.Nsec)
71 }
72
73
74 func (tv *Timeval) Nano() int64 {
75 return int64(tv.Sec)*1e9 + int64(tv.Usec)*1000
76 }
77
View as plain text