blob: 3d893040553be2a12ee54aecd174429493cebdc2 [file] [log] [blame]
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -07001// 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.
4
David K. Bainbridgec415efe2021-08-19 13:05:21 +00005//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || zos
6// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris zos
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -07007
8package unix
9
10import "time"
11
David K. Bainbridgec415efe2021-08-19 13:05:21 +000012// TimespecToNSec returns the time stored in ts as nanoseconds.
13func TimespecToNsec(ts Timespec) int64 { return ts.Nano() }
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070014
David K. Bainbridgec415efe2021-08-19 13:05:21 +000015// NsecToTimespec converts a number of nanoseconds into a Timespec.
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070016func NsecToTimespec(nsec int64) Timespec {
17 sec := nsec / 1e9
18 nsec = nsec % 1e9
19 if nsec < 0 {
20 nsec += 1e9
21 sec--
22 }
23 return setTimespec(sec, nsec)
24}
25
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.
30func TimeToTimespec(t time.Time) (Timespec, error) {
31 sec := t.Unix()
32 nsec := int64(t.Nanosecond())
33 ts := setTimespec(sec, nsec)
34
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
40 }
41 return ts, nil
42}
43
David K. Bainbridgec415efe2021-08-19 13:05:21 +000044// TimevalToNsec returns the time stored in tv as nanoseconds.
45func TimevalToNsec(tv Timeval) int64 { return tv.Nano() }
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070046
David K. Bainbridgec415efe2021-08-19 13:05:21 +000047// NsecToTimeval converts a number of nanoseconds into a Timeval.
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070048func NsecToTimeval(nsec int64) Timeval {
49 nsec += 999 // round up to microsecond
50 usec := nsec % 1e9 / 1e3
51 sec := nsec / 1e9
52 if usec < 0 {
53 usec += 1e6
54 sec--
55 }
56 return setTimeval(sec, usec)
57}
58
David K. Bainbridgec415efe2021-08-19 13:05:21 +000059// Unix returns the time stored in ts as seconds plus nanoseconds.
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070060func (ts *Timespec) Unix() (sec int64, nsec int64) {
61 return int64(ts.Sec), int64(ts.Nsec)
62}
63
David K. Bainbridgec415efe2021-08-19 13:05:21 +000064// Unix returns the time stored in tv as seconds plus nanoseconds.
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070065func (tv *Timeval) Unix() (sec int64, nsec int64) {
66 return int64(tv.Sec), int64(tv.Usec) * 1000
67}
68
David K. Bainbridgec415efe2021-08-19 13:05:21 +000069// Nano returns the time stored in ts as nanoseconds.
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070070func (ts *Timespec) Nano() int64 {
71 return int64(ts.Sec)*1e9 + int64(ts.Nsec)
72}
73
David K. Bainbridgec415efe2021-08-19 13:05:21 +000074// Nano returns the time stored in tv as nanoseconds.
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070075func (tv *Timeval) Nano() int64 {
76 return int64(tv.Sec)*1e9 + int64(tv.Usec)*1000
77}