diff options
| author | Ian Lance Taylor <iant@golang.org> | 2016-10-11 21:04:16 -0700 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2016-10-12 13:10:54 +0000 |
| commit | 6c517df4daa0acaa25a16baeef5ea037c9a0194c (patch) | |
| tree | ebd06f90eae1ac20c76d4a6e29b543d89a0d279c /src/syscall/timestruct.go | |
| parent | 6ca48f710f6ae163aa87e883a0a4cf8a91dad0a4 (diff) | |
| download | go-6c517df4daa0acaa25a16baeef5ea037c9a0194c.tar.xz | |
syscall: unify NsecToTime{spec,val}, fix for times < 1970
All the implementations of NsecToTimespec and NsecToTimeval were the
same other than types. Write a single version that uses
GOARCH/GOOS-specific setTimespec and setTimeval functions to handle the
types.
The logic in NsecToTimespec and NsecToTimeval caused times before 1970
to have a negative usec/nsec. The Linux kernel requires that usec
contain a positive number; for consistency, we do this for both
NsecToTimespec and NsecToTimeval.
Change-Id: I525eaba2e7cdb00cb57fa00182dabf19fec298ae
Reviewed-on: https://go-review.googlesource.com/30826
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
Diffstat (limited to 'src/syscall/timestruct.go')
| -rw-r--r-- | src/syscall/timestruct.go | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/syscall/timestruct.go b/src/syscall/timestruct.go index 4c4e204916..49c3383b4f 100644 --- a/src/syscall/timestruct.go +++ b/src/syscall/timestruct.go @@ -6,6 +6,35 @@ package syscall +// TimespecToNsec converts a Timespec value into a number of +// nanoseconds since the Unix epoch. func TimespecToNsec(ts Timespec) int64 { return int64(ts.Sec)*1e9 + int64(ts.Nsec) } +// NsecToTimespec takes a number of nanoseconds since the Unix epoch +// and returns the corresponding Timespec value. +func NsecToTimespec(nsec int64) Timespec { + sec := nsec / 1e9 + nsec = nsec % 1e9 + if nsec < 0 { + nsec += 1e9 + sec-- + } + return setTimespec(sec, nsec) +} + +// TimevalToNsec converts a Timeval value into a number of nanoseconds +// since the Unix epoch. func TimevalToNsec(tv Timeval) int64 { return int64(tv.Sec)*1e9 + int64(tv.Usec)*1e3 } + +// NsecToTimeval takes a number of nanoseconds since the Unix epoch +// and returns the corresponding Timeval value. +func NsecToTimeval(nsec int64) Timeval { + nsec += 999 // round up to microsecond + usec := nsec % 1e9 / 1e3 + sec := nsec / 1e9 + if usec < 0 { + usec += 1e6 + sec-- + } + return setTimeval(sec, usec) +} |
