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
5// +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris
6
7package syscall
8
9// TimespecToNsec converts a Timespec value into a number of
10// nanoseconds since the Unix epoch.
11func TimespecToNsec(ts Timespec) int64 { return ts.Nano() }
12
13// NsecToTimespec takes a number of nanoseconds since the Unix epoch
14// and returns the corresponding Timespec value.
15func NsecToTimespec(nsec int64) Timespec {
16	sec := nsec / 1e9
17	nsec = nsec % 1e9
18	if nsec < 0 {
19		nsec += 1e9
20		sec--
21	}
22	return setTimespec(sec, nsec)
23}
24
25// TimevalToNsec converts a Timeval value into a number of nanoseconds
26// since the Unix epoch.
27func TimevalToNsec(tv Timeval) int64 { return tv.Nano() }
28
29// NsecToTimeval takes a number of nanoseconds since the Unix epoch
30// and returns the corresponding Timeval value.
31func NsecToTimeval(nsec int64) Timeval {
32	nsec += 999 // round up to microsecond
33	usec := nsec % 1e9 / 1e3
34	sec := nsec / 1e9
35	if usec < 0 {
36		usec += 1e6
37		sec--
38	}
39	return setTimeval(sec, usec)
40}
41