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
//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || zos
6
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris zos
12
// TimespecToNsec returns the time stored in ts as nanoseconds.
13
func TimespecToNsec(ts Timespec) int64 { return ts.Nano() }
15
// NsecToTimespec converts a number of nanoseconds into a Timespec.
16
func NsecToTimespec(nsec int64) Timespec {
23
return setTimespec(sec, nsec)
26
// TimeToTimespec converts t into a Timespec.
27
// On some 32-bit systems the range of valid Timespec values are smaller
28
// than that of time.Time values. So if t is out of the valid range of
29
// Timespec, it returns a zero Timespec and ERANGE.
30
func TimeToTimespec(t time.Time) (Timespec, error) {
32
nsec := int64(t.Nanosecond())
33
ts := setTimespec(sec, nsec)
35
// Currently all targets have either int32 or int64 for Timespec.Sec.
36
// If there were a new target with floating point type for it, we have
37
// to consider the rounding error.
38
if int64(ts.Sec) != sec {
39
return Timespec{}, ERANGE
44
// TimevalToNsec returns the time stored in tv as nanoseconds.
45
func TimevalToNsec(tv Timeval) int64 { return tv.Nano() }
47
// NsecToTimeval converts a number of nanoseconds into a Timeval.
48
func NsecToTimeval(nsec int64) Timeval {
49
nsec += 999 // round up to microsecond
50
usec := nsec % 1e9 / 1e3
56
return setTimeval(sec, usec)
59
// Unix returns the time stored in ts as seconds plus nanoseconds.
60
func (ts *Timespec) Unix() (sec int64, nsec int64) {
61
return int64(ts.Sec), int64(ts.Nsec)
64
// Unix returns the time stored in tv as seconds plus nanoseconds.
65
func (tv *Timeval) Unix() (sec int64, nsec int64) {
66
return int64(tv.Sec), int64(tv.Usec) * 1000
69
// Nano returns the time stored in ts as nanoseconds.
70
func (ts *Timespec) Nano() int64 {
71
return int64(ts.Sec)*1e9 + int64(ts.Nsec)
74
// Nano returns the time stored in tv as nanoseconds.
75
func (tv *Timeval) Nano() int64 {
76
return int64(tv.Sec)*1e9 + int64(tv.Usec)*1000