timestruct.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
  5. package unix
  6. import "time"
  7. // TimespecToNsec converts a Timespec value into a number of
  8. // nanoseconds since the Unix epoch.
  9. func TimespecToNsec(ts Timespec) int64 { return int64(ts.Sec)*1e9 + int64(ts.Nsec) }
  10. // NsecToTimespec takes a number of nanoseconds since the Unix epoch
  11. // and returns the corresponding Timespec value.
  12. func NsecToTimespec(nsec int64) Timespec {
  13. sec := nsec / 1e9
  14. nsec = nsec % 1e9
  15. if nsec < 0 {
  16. nsec += 1e9
  17. sec--
  18. }
  19. return setTimespec(sec, nsec)
  20. }
  21. // TimeToTimespec converts t into a Timespec.
  22. // On some 32-bit systems the range of valid Timespec values are smaller
  23. // than that of time.Time values. So if t is out of the valid range of
  24. // Timespec, it returns a zero Timespec and ERANGE.
  25. func TimeToTimespec(t time.Time) (Timespec, error) {
  26. sec := t.Unix()
  27. nsec := int64(t.Nanosecond())
  28. ts := setTimespec(sec, nsec)
  29. // Currently all targets have either int32 or int64 for Timespec.Sec.
  30. // If there were a new target with floating point type for it, we have
  31. // to consider the rounding error.
  32. if int64(ts.Sec) != sec {
  33. return Timespec{}, ERANGE
  34. }
  35. return ts, nil
  36. }
  37. // TimevalToNsec converts a Timeval value into a number of nanoseconds
  38. // since the Unix epoch.
  39. func TimevalToNsec(tv Timeval) int64 { return int64(tv.Sec)*1e9 + int64(tv.Usec)*1e3 }
  40. // NsecToTimeval takes a number of nanoseconds since the Unix epoch
  41. // and returns the corresponding Timeval value.
  42. func NsecToTimeval(nsec int64) Timeval {
  43. nsec += 999 // round up to microsecond
  44. usec := nsec % 1e9 / 1e3
  45. sec := nsec / 1e9
  46. if usec < 0 {
  47. usec += 1e6
  48. sec--
  49. }
  50. return setTimeval(sec, usec)
  51. }
  52. // Unix returns ts as the number of seconds and nanoseconds elapsed since the
  53. // Unix epoch.
  54. func (ts *Timespec) Unix() (sec int64, nsec int64) {
  55. return int64(ts.Sec), int64(ts.Nsec)
  56. }
  57. // Unix returns tv as the number of seconds and nanoseconds elapsed since the
  58. // Unix epoch.
  59. func (tv *Timeval) Unix() (sec int64, nsec int64) {
  60. return int64(tv.Sec), int64(tv.Usec) * 1000
  61. }
  62. // Nano returns ts as the number of nanoseconds elapsed since the Unix epoch.
  63. func (ts *Timespec) Nano() int64 {
  64. return int64(ts.Sec)*1e9 + int64(ts.Nsec)
  65. }
  66. // Nano returns tv as the number of nanoseconds elapsed since the Unix epoch.
  67. func (tv *Timeval) Nano() int64 {
  68. return int64(tv.Sec)*1e9 + int64(tv.Usec)*1000
  69. }