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.
5 // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
11 // TimespecToNsec converts a Timespec value into a number of
12 // nanoseconds since the Unix epoch.
13 func TimespecToNsec(ts Timespec) int64 { return int64(ts.Sec)*1e9 + int64(ts.Nsec) }
15 // NsecToTimespec takes a number of nanoseconds since the Unix epoch
16 // and returns the corresponding Timespec value.
17 func NsecToTimespec(nsec int64) Timespec {
24 return setTimespec(sec, nsec)
27 // TimeToTimespec converts t into a Timespec.
28 // On some 32-bit systems the range of valid Timespec values are smaller
29 // than that of time.Time values. So if t is out of the valid range of
30 // Timespec, it returns a zero Timespec and ERANGE.
31 func TimeToTimespec(t time.Time) (Timespec, error) {
33 nsec := int64(t.Nanosecond())
34 ts := setTimespec(sec, nsec)
36 // Currently all targets have either int32 or int64 for Timespec.Sec.
37 // If there were a new target with floating point type for it, we have
38 // to consider the rounding error.
39 if int64(ts.Sec) != sec {
40 return Timespec{}, ERANGE
45 // TimevalToNsec converts a Timeval value into a number of nanoseconds
46 // since the Unix epoch.
47 func TimevalToNsec(tv Timeval) int64 { return int64(tv.Sec)*1e9 + int64(tv.Usec)*1e3 }
49 // NsecToTimeval takes a number of nanoseconds since the Unix epoch
50 // and returns the corresponding Timeval value.
51 func NsecToTimeval(nsec int64) Timeval {
52 nsec += 999 // round up to microsecond
53 usec := nsec % 1e9 / 1e3
59 return setTimeval(sec, usec)
62 // Unix returns ts as the number of seconds and nanoseconds elapsed since the
64 func (ts *Timespec) Unix() (sec int64, nsec int64) {
65 return int64(ts.Sec), int64(ts.Nsec)
68 // Unix returns tv as the number of seconds and nanoseconds elapsed since the
70 func (tv *Timeval) Unix() (sec int64, nsec int64) {
71 return int64(tv.Sec), int64(tv.Usec) * 1000
74 // Nano returns ts as the number of nanoseconds elapsed since the Unix epoch.
75 func (ts *Timespec) Nano() int64 {
76 return int64(ts.Sec)*1e9 + int64(ts.Nsec)
79 // Nano returns tv as the number of nanoseconds elapsed since the Unix epoch.
80 func (tv *Timeval) Nano() int64 {
81 return int64(tv.Sec)*1e9 + int64(tv.Usec)*1000