github.com/ltltlt/go-source-code@v0.0.0-20190830023027-95be009773aa/syscall/timestruct.go (about) 1 // Copyright 2016 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 nacl netbsd openbsd solaris 6 7 package syscall 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 }