// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || zos
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris zos

package unix

import 

// TimespecToNsec returns the time stored in ts as nanoseconds.
func ( Timespec) int64 { return .Nano() }

// NsecToTimespec converts a number of nanoseconds into a Timespec.
func ( int64) Timespec {
	 :=  / 1e9
	 =  % 1e9
	if  < 0 {
		 += 1e9
		--
	}
	return setTimespec(, )
}

// TimeToTimespec converts t into a Timespec.
// On some 32-bit systems the range of valid Timespec values are smaller
// than that of time.Time values.  So if t is out of the valid range of
// Timespec, it returns a zero Timespec and ERANGE.
func ( time.Time) (Timespec, error) {
	 := .Unix()
	 := int64(.Nanosecond())
	 := setTimespec(, )

	// Currently all targets have either int32 or int64 for Timespec.Sec.
	// If there were a new target with floating point type for it, we have
	// to consider the rounding error.
	if int64(.Sec) !=  {
		return Timespec{}, ERANGE
	}
	return , nil
}

// TimevalToNsec returns the time stored in tv as nanoseconds.
func ( Timeval) int64 { return .Nano() }

// NsecToTimeval converts a number of nanoseconds into a Timeval.
func ( int64) Timeval {
	 += 999 // round up to microsecond
	 :=  % 1e9 / 1e3
	 :=  / 1e9
	if  < 0 {
		 += 1e6
		--
	}
	return setTimeval(, )
}

// Unix returns the time stored in ts as seconds plus nanoseconds.
func ( *Timespec) () ( int64,  int64) {
	return int64(.Sec), int64(.Nsec)
}

// Unix returns the time stored in tv as seconds plus nanoseconds.
func ( *Timeval) () ( int64,  int64) {
	return int64(.Sec), int64(.Usec) * 1000
}

// Nano returns the time stored in ts as nanoseconds.
func ( *Timespec) () int64 {
	return int64(.Sec)*1e9 + int64(.Nsec)
}

// Nano returns the time stored in tv as nanoseconds.
func ( *Timeval) () int64 {
	return int64(.Sec)*1e9 + int64(.Usec)*1000
}