1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- package unix
- import "time"
- func TimespecToNsec(ts Timespec) int64 { return ts.Nano() }
- func NsecToTimespec(nsec int64) Timespec {
- sec := nsec / 1e9
- nsec = nsec % 1e9
- if nsec < 0 {
- nsec += 1e9
- sec--
- }
- return setTimespec(sec, nsec)
- }
- func TimeToTimespec(t time.Time) (Timespec, error) {
- sec := t.Unix()
- nsec := int64(t.Nanosecond())
- ts := setTimespec(sec, nsec)
-
-
-
- if int64(ts.Sec) != sec {
- return Timespec{}, ERANGE
- }
- return ts, nil
- }
- func TimevalToNsec(tv Timeval) int64 { return tv.Nano() }
- func NsecToTimeval(nsec int64) Timeval {
- nsec += 999
- usec := nsec % 1e9 / 1e3
- sec := nsec / 1e9
- if usec < 0 {
- usec += 1e6
- sec--
- }
- return setTimeval(sec, usec)
- }
- func (ts *Timespec) Unix() (sec int64, nsec int64) {
- return int64(ts.Sec), int64(ts.Nsec)
- }
- func (tv *Timeval) Unix() (sec int64, nsec int64) {
- return int64(tv.Sec), int64(tv.Usec) * 1000
- }
- func (ts *Timespec) Nano() int64 {
- return int64(ts.Sec)*1e9 + int64(ts.Nsec)
- }
- func (tv *Timeval) Nano() int64 {
- return int64(tv.Sec)*1e9 + int64(tv.Usec)*1000
- }
|