timestruct.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  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. //go:build aix || darwin || dragonfly || freebsd || hurd || (js && wasm) || linux || netbsd || openbsd || solaris
  5. package syscall
  6. // TimespecToNSec returns the time stored in ts as nanoseconds.
  7. func TimespecToNsec(ts Timespec) int64 { return ts.Nano() }
  8. // NsecToTimespec converts a number of nanoseconds into a Timespec.
  9. func NsecToTimespec(nsec int64) Timespec {
  10. sec := nsec / 1e9
  11. nsec = nsec % 1e9
  12. if nsec < 0 {
  13. nsec += 1e9
  14. sec--
  15. }
  16. return setTimespec(sec, nsec)
  17. }
  18. // TimevalToNsec returns the time stored in tv as nanoseconds.
  19. func TimevalToNsec(tv Timeval) int64 { return tv.Nano() }
  20. // NsecToTimeval converts a number of nanoseconds into a Timeval.
  21. func NsecToTimeval(nsec int64) Timeval {
  22. nsec += 999 // round up to microsecond
  23. usec := nsec % 1e9 / 1e3
  24. sec := nsec / 1e9
  25. if usec < 0 {
  26. usec += 1e6
  27. sec--
  28. }
  29. return setTimeval(sec, usec)
  30. }