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 returns the time stored in ts as nanoseconds.
12 func TimespecToNsec(ts Timespec) int64 { return ts.Nano() }
14 // NsecToTimespec converts a number of nanoseconds into a Timespec.
15 func NsecToTimespec(nsec int64) Timespec {
22 return setTimespec(sec, nsec)
25 // TimeToTimespec converts t into a Timespec.
26 // On some 32-bit systems the range of valid Timespec values are smaller
27 // than that of time.Time values. So if t is out of the valid range of
28 // Timespec, it returns a zero Timespec and ERANGE.
29 func TimeToTimespec(t time.Time) (Timespec, error) {
31 nsec := int64(t.Nanosecond())
32 ts := setTimespec(sec, nsec)
34 // Currently all targets have either int32 or int64 for Timespec.Sec.
35 // If there were a new target with floating point type for it, we have
36 // to consider the rounding error.
37 if int64(ts.Sec) != sec {
38 return Timespec{}, ERANGE
43 // TimevalToNsec returns the time stored in tv as nanoseconds.
44 func TimevalToNsec(tv Timeval) int64 { return tv.Nano() }
46 // NsecToTimeval converts a number of nanoseconds into a Timeval.
47 func NsecToTimeval(nsec int64) Timeval {
48 nsec += 999 // round up to microsecond
49 usec := nsec % 1e9 / 1e3
55 return setTimeval(sec, usec)
58 // Unix returns the time stored in ts as seconds plus nanoseconds.
59 func (ts *Timespec) Unix() (sec int64, nsec int64) {
60 return int64(ts.Sec), int64(ts.Nsec)
63 // Unix returns the time stored in tv as seconds plus nanoseconds.
64 func (tv *Timeval) Unix() (sec int64, nsec int64) {
65 return int64(tv.Sec), int64(tv.Usec) * 1000
68 // Nano returns the time stored in ts as nanoseconds.
69 func (ts *Timespec) Nano() int64 {
70 return int64(ts.Sec)*1e9 + int64(ts.Nsec)
73 // Nano returns the time stored in tv as nanoseconds.
74 func (tv *Timeval) Nano() int64 {
75 return int64(tv.Sec)*1e9 + int64(tv.Usec)*1000