github.com/dolotech/hongbao@v0.0.0-20191130105438-fd59d7a5dda5/src/golang.org/x/sys/unix/timestruct.go (about) 1 // Copyright 2017 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // +build darwin dragonfly freebsd linux netbsd openbsd solaris 6 7 package unix 8 9 // TimespecToNsec converts a Timespec value into a number of 10 // nanoseconds since the Unix epoch. 11 func TimespecToNsec(ts Timespec) int64 { return int64(ts.Sec)*1e9 + int64(ts.Nsec) } 12 13 // NsecToTimespec takes a number of nanoseconds since the Unix epoch 14 // and returns the corresponding Timespec value. 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 // TimevalToNsec converts a Timeval value into a number of nanoseconds 26 // since the Unix epoch. 27 func TimevalToNsec(tv Timeval) int64 { return int64(tv.Sec)*1e9 + int64(tv.Usec)*1e3 } 28 29 // NsecToTimeval takes a number of nanoseconds since the Unix epoch 30 // and returns the corresponding Timeval value. 31 func NsecToTimeval(nsec int64) Timeval { 32 nsec += 999 // round up to microsecond 33 usec := nsec % 1e9 / 1e3 34 sec := nsec / 1e9 35 if usec < 0 { 36 usec += 1e6 37 sec-- 38 } 39 return setTimeval(sec, usec) 40 } 41 42 // Unix returns ts as the number of seconds and nanoseconds elapsed since the 43 // Unix epoch. 44 func (ts *Timespec) Unix() (sec int64, nsec int64) { 45 return int64(ts.Sec), int64(ts.Nsec) 46 } 47 48 // Unix returns tv as the number of seconds and nanoseconds elapsed since the 49 // Unix epoch. 50 func (tv *Timeval) Unix() (sec int64, nsec int64) { 51 return int64(tv.Sec), int64(tv.Usec) * 1000 52 } 53 54 // Nano returns ts as the number of nanoseconds elapsed since the Unix epoch. 55 func (ts *Timespec) Nano() int64 { 56 return int64(ts.Sec)*1e9 + int64(ts.Nsec) 57 } 58 59 // Nano returns tv as the number of nanoseconds elapsed since the Unix epoch. 60 func (tv *Timeval) Nano() int64 { 61 return int64(tv.Sec)*1e9 + int64(tv.Usec)*1000 62 }