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