Commit e8259736 authored by Tobias Klauser's avatar Tobias Klauser Committed by Tobias Klauser

unix: 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.

Follow CL 30826 which did the same for syscall.

Change-Id: Id6c6f4fef8450251447d1a5b01f35c2a36b5aeb1
Reviewed-on: https://go-review.googlesource.com/73170
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
parent bb1b7fe5
...@@ -11,17 +11,12 @@ import ( ...@@ -11,17 +11,12 @@ import (
"unsafe" "unsafe"
) )
func NsecToTimespec(nsec int64) (ts Timespec) { func setTimespec(sec, nsec int64) Timespec {
ts.Sec = int32(nsec / 1e9) return Timespec{Sec: int32(sec), Nsec: int32(nsec)}
ts.Nsec = int32(nsec % 1e9)
return
} }
func NsecToTimeval(nsec int64) (tv Timeval) { func setTimeval(sec, usec int64) Timeval {
nsec += 999 // round up to microsecond return Timeval{Sec: int32(sec), Usec: int32(usec)}
tv.Usec = int32(nsec % 1e9 / 1e3)
tv.Sec = int32(nsec / 1e9)
return
} }
//sysnb gettimeofday(tp *Timeval) (sec int32, usec int32, err error) //sysnb gettimeofday(tp *Timeval) (sec int32, usec int32, err error)
......
...@@ -11,17 +11,12 @@ import ( ...@@ -11,17 +11,12 @@ import (
"unsafe" "unsafe"
) )
func NsecToTimespec(nsec int64) (ts Timespec) { func setTimespec(sec, nsec int64) Timespec {
ts.Sec = nsec / 1e9 return Timespec{Sec: sec, Nsec: nsec}
ts.Nsec = nsec % 1e9
return
} }
func NsecToTimeval(nsec int64) (tv Timeval) { func setTimeval(sec, usec int64) Timeval {
nsec += 999 // round up to microsecond return Timeval{Sec: sec, Usec: int32(usec)}
tv.Usec = int32(nsec % 1e9 / 1e3)
tv.Sec = int64(nsec / 1e9)
return
} }
//sysnb gettimeofday(tp *Timeval) (sec int64, usec int32, err error) //sysnb gettimeofday(tp *Timeval) (sec int64, usec int32, err error)
......
...@@ -9,17 +9,12 @@ import ( ...@@ -9,17 +9,12 @@ import (
"unsafe" "unsafe"
) )
func NsecToTimespec(nsec int64) (ts Timespec) { func setTimespec(sec, nsec int64) Timespec {
ts.Sec = int32(nsec / 1e9) return Timespec{Sec: int32(sec), Nsec: int32(nsec)}
ts.Nsec = int32(nsec % 1e9)
return
} }
func NsecToTimeval(nsec int64) (tv Timeval) { func setTimeval(sec, usec int64) Timeval {
nsec += 999 // round up to microsecond return Timeval{Sec: int32(sec), Usec: int32(usec)}
tv.Usec = int32(nsec % 1e9 / 1e3)
tv.Sec = int32(nsec / 1e9)
return
} }
//sysnb gettimeofday(tp *Timeval) (sec int32, usec int32, err error) //sysnb gettimeofday(tp *Timeval) (sec int32, usec int32, err error)
......
...@@ -11,17 +11,12 @@ import ( ...@@ -11,17 +11,12 @@ import (
"unsafe" "unsafe"
) )
func NsecToTimespec(nsec int64) (ts Timespec) { func setTimespec(sec, nsec int64) Timespec {
ts.Sec = nsec / 1e9 return Timespec{Sec: sec, Nsec: nsec}
ts.Nsec = nsec % 1e9
return
} }
func NsecToTimeval(nsec int64) (tv Timeval) { func setTimeval(sec, usec int64) Timeval {
nsec += 999 // round up to microsecond return Timeval{Sec: sec, Usec: int32(usec)}
tv.Usec = int32(nsec % 1e9 / 1e3)
tv.Sec = int64(nsec / 1e9)
return
} }
//sysnb gettimeofday(tp *Timeval) (sec int64, usec int32, err error) //sysnb gettimeofday(tp *Timeval) (sec int64, usec int32, err error)
......
...@@ -11,17 +11,12 @@ import ( ...@@ -11,17 +11,12 @@ import (
"unsafe" "unsafe"
) )
func NsecToTimespec(nsec int64) (ts Timespec) { func setTimespec(sec, nsec int64) Timespec {
ts.Sec = nsec / 1e9 return Timespec{Sec: sec, Nsec: nsec}
ts.Nsec = nsec % 1e9
return
} }
func NsecToTimeval(nsec int64) (tv Timeval) { func setTimeval(sec, usec int64) Timeval {
nsec += 999 // round up to microsecond return Timeval{Sec: sec, Usec: usec}
tv.Usec = nsec % 1e9 / 1e3
tv.Sec = int64(nsec / 1e9)
return
} }
func SetKevent(k *Kevent_t, fd, mode, flags int) { func SetKevent(k *Kevent_t, fd, mode, flags int) {
......
...@@ -11,17 +11,12 @@ import ( ...@@ -11,17 +11,12 @@ import (
"unsafe" "unsafe"
) )
func NsecToTimespec(nsec int64) (ts Timespec) { func setTimespec(sec, nsec int64) Timespec {
ts.Sec = int32(nsec / 1e9) return Timespec{Sec: int32(sec), Nsec: int32(nsec)}
ts.Nsec = int32(nsec % 1e9)
return
} }
func NsecToTimeval(nsec int64) (tv Timeval) { func setTimeval(sec, usec int64) Timeval {
nsec += 999 // round up to microsecond return Timeval{Sec: int32(sec), Usec: int32(usec)}
tv.Usec = int32(nsec % 1e9 / 1e3)
tv.Sec = int32(nsec / 1e9)
return
} }
func SetKevent(k *Kevent_t, fd, mode, flags int) { func SetKevent(k *Kevent_t, fd, mode, flags int) {
......
...@@ -11,17 +11,12 @@ import ( ...@@ -11,17 +11,12 @@ import (
"unsafe" "unsafe"
) )
func NsecToTimespec(nsec int64) (ts Timespec) { func setTimespec(sec, nsec int64) Timespec {
ts.Sec = nsec / 1e9 return Timespec{Sec: sec, Nsec: nsec}
ts.Nsec = nsec % 1e9
return
} }
func NsecToTimeval(nsec int64) (tv Timeval) { func setTimeval(sec, usec int64) Timeval {
nsec += 999 // round up to microsecond return Timeval{Sec: sec, Usec: usec}
tv.Usec = nsec % 1e9 / 1e3
tv.Sec = int64(nsec / 1e9)
return
} }
func SetKevent(k *Kevent_t, fd, mode, flags int) { func SetKevent(k *Kevent_t, fd, mode, flags int) {
......
...@@ -11,17 +11,12 @@ import ( ...@@ -11,17 +11,12 @@ import (
"unsafe" "unsafe"
) )
func NsecToTimespec(nsec int64) (ts Timespec) { func setTimespec(sec, nsec int64) Timespec {
ts.Sec = nsec / 1e9 return Timespec{Sec: sec, Nsec: int32(nsec)}
ts.Nsec = int32(nsec % 1e9)
return
} }
func NsecToTimeval(nsec int64) (tv Timeval) { func setTimeval(sec, usec int64) Timeval {
nsec += 999 // round up to microsecond return Timeval{Sec: sec, Usec: int32(usec)}
tv.Usec = int32(nsec % 1e9 / 1e3)
tv.Sec = nsec / 1e9
return
} }
func SetKevent(k *Kevent_t, fd, mode, flags int) { func SetKevent(k *Kevent_t, fd, mode, flags int) {
......
...@@ -14,17 +14,12 @@ import ( ...@@ -14,17 +14,12 @@ import (
"unsafe" "unsafe"
) )
func NsecToTimespec(nsec int64) (ts Timespec) { func setTimespec(sec, nsec int64) Timespec {
ts.Sec = int32(nsec / 1e9) return Timespec{Sec: int32(sec), Nsec: int32(nsec)}
ts.Nsec = int32(nsec % 1e9)
return
} }
func NsecToTimeval(nsec int64) (tv Timeval) { func setTimeval(sec, usec int64) Timeval {
nsec += 999 // round up to microsecond return Timeval{Sec: int32(sec), Usec: int32(usec)}
tv.Sec = int32(nsec / 1e9)
tv.Usec = int32(nsec % 1e9 / 1e3)
return
} }
//sysnb pipe(p *[2]_C_int) (err error) //sysnb pipe(p *[2]_C_int) (err error)
......
...@@ -83,17 +83,12 @@ func Time(t *Time_t) (tt Time_t, err error) { ...@@ -83,17 +83,12 @@ func Time(t *Time_t) (tt Time_t, err error) {
//sys Utime(path string, buf *Utimbuf) (err error) //sys Utime(path string, buf *Utimbuf) (err error)
func NsecToTimespec(nsec int64) (ts Timespec) { func setTimespec(sec, nsec int64) Timespec {
ts.Sec = nsec / 1e9 return Timespec{Sec: sec, Nsec: nsec}
ts.Nsec = nsec % 1e9
return
} }
func NsecToTimeval(nsec int64) (tv Timeval) { func setTimeval(sec, usec int64) Timeval {
nsec += 999 // round up to microsecond return Timeval{Sec: sec, Usec: usec}
tv.Sec = nsec / 1e9
tv.Usec = nsec % 1e9 / 1e3
return
} }
//sysnb pipe(p *[2]_C_int) (err error) //sysnb pipe(p *[2]_C_int) (err error)
......
...@@ -11,17 +11,12 @@ import ( ...@@ -11,17 +11,12 @@ import (
"unsafe" "unsafe"
) )
func NsecToTimespec(nsec int64) (ts Timespec) { func setTimespec(sec, nsec int64) Timespec {
ts.Sec = int32(nsec / 1e9) return Timespec{Sec: int32(sec), Nsec: int32(nsec)}
ts.Nsec = int32(nsec % 1e9)
return
} }
func NsecToTimeval(nsec int64) (tv Timeval) { func setTimeval(sec, usec int64) Timeval {
nsec += 999 // round up to microsecond return Timeval{Sec: int32(sec), Usec: int32(usec)}
tv.Sec = int32(nsec / 1e9)
tv.Usec = int32(nsec % 1e9 / 1e3)
return
} }
func Pipe(p []int) (err error) { func Pipe(p []int) (err error) {
......
...@@ -73,17 +73,12 @@ func Lstat(path string, stat *Stat_t) (err error) { ...@@ -73,17 +73,12 @@ func Lstat(path string, stat *Stat_t) (err error) {
//sysnb Gettimeofday(tv *Timeval) (err error) //sysnb Gettimeofday(tv *Timeval) (err error)
func NsecToTimespec(nsec int64) (ts Timespec) { func setTimespec(sec, nsec int64) Timespec {
ts.Sec = nsec / 1e9 return Timespec{Sec: sec, Nsec: nsec}
ts.Nsec = nsec % 1e9
return
} }
func NsecToTimeval(nsec int64) (tv Timeval) { func setTimeval(sec, usec int64) Timeval {
nsec += 999 // round up to microsecond return Timeval{Sec: sec, Usec: usec}
tv.Sec = nsec / 1e9
tv.Usec = nsec % 1e9 / 1e3
return
} }
func Time(t *Time_t) (Time_t, error) { func Time(t *Time_t) (Time_t, error) {
......
...@@ -76,17 +76,12 @@ func Time(t *Time_t) (tt Time_t, err error) { ...@@ -76,17 +76,12 @@ func Time(t *Time_t) (tt Time_t, err error) {
//sys Utime(path string, buf *Utimbuf) (err error) //sys Utime(path string, buf *Utimbuf) (err error)
func NsecToTimespec(nsec int64) (ts Timespec) { func setTimespec(sec, nsec int64) Timespec {
ts.Sec = nsec / 1e9 return Timespec{Sec: sec, Nsec: nsec}
ts.Nsec = nsec % 1e9
return
} }
func NsecToTimeval(nsec int64) (tv Timeval) { func setTimeval(sec, usec int64) Timeval {
nsec += 999 // round up to microsecond return Timeval{Sec: sec, Usec: usec}
tv.Sec = nsec / 1e9
tv.Usec = nsec % 1e9 / 1e3
return
} }
func Pipe(p []int) (err error) { func Pipe(p []int) (err error) {
......
...@@ -99,17 +99,12 @@ func Seek(fd int, offset int64, whence int) (off int64, err error) { ...@@ -99,17 +99,12 @@ func Seek(fd int, offset int64, whence int) (off int64, err error) {
return return
} }
func NsecToTimespec(nsec int64) (ts Timespec) { func setTimespec(sec, nsec int64) Timespec {
ts.Sec = int32(nsec / 1e9) return Timespec{Sec: int32(sec), Nsec: int32(nsec)}
ts.Nsec = int32(nsec % 1e9)
return
} }
func NsecToTimeval(nsec int64) (tv Timeval) { func setTimeval(sec, usec int64) Timeval {
nsec += 999 // round up to microsecond return Timeval{Sec: int32(sec), Usec: int32(usec)}
tv.Sec = int32(nsec / 1e9)
tv.Usec = int32(nsec % 1e9 / 1e3)
return
} }
//sysnb pipe2(p *[2]_C_int, flags int) (err error) //sysnb pipe2(p *[2]_C_int, flags int) (err error)
......
...@@ -66,17 +66,12 @@ package unix ...@@ -66,17 +66,12 @@ package unix
//sys Utime(path string, buf *Utimbuf) (err error) //sys Utime(path string, buf *Utimbuf) (err error)
func NsecToTimespec(nsec int64) (ts Timespec) { func setTimespec(sec, nsec int64) Timespec {
ts.Sec = nsec / 1e9 return Timespec{Sec: sec, Nsec: nsec}
ts.Nsec = nsec % 1e9
return
} }
func NsecToTimeval(nsec int64) (tv Timeval) { func setTimeval(sec, usec int64) Timeval {
nsec += 999 // round up to microsecond return Timeval{Sec: sec, Usec: usec}
tv.Sec = nsec / 1e9
tv.Usec = nsec % 1e9 / 1e3
return
} }
func (r *PtraceRegs) PC() uint64 { return r.Nip } func (r *PtraceRegs) PC() uint64 { return r.Nip }
......
...@@ -62,17 +62,12 @@ func Time(t *Time_t) (tt Time_t, err error) { ...@@ -62,17 +62,12 @@ func Time(t *Time_t) (tt Time_t, err error) {
//sys Utime(path string, buf *Utimbuf) (err error) //sys Utime(path string, buf *Utimbuf) (err error)
func NsecToTimespec(nsec int64) (ts Timespec) { func setTimespec(sec, nsec int64) Timespec {
ts.Sec = nsec / 1e9 return Timespec{Sec: sec, Nsec: nsec}
ts.Nsec = nsec % 1e9
return
} }
func NsecToTimeval(nsec int64) (tv Timeval) { func setTimeval(sec, usec int64) Timeval {
nsec += 999 // round up to microsecond return Timeval{Sec: sec, Usec: usec}
tv.Sec = nsec / 1e9
tv.Usec = nsec % 1e9 / 1e3
return
} }
//sysnb pipe2(p *[2]_C_int, flags int) (err error) //sysnb pipe2(p *[2]_C_int, flags int) (err error)
......
...@@ -82,17 +82,12 @@ func Time(t *Time_t) (tt Time_t, err error) { ...@@ -82,17 +82,12 @@ func Time(t *Time_t) (tt Time_t, err error) {
//sys Utime(path string, buf *Utimbuf) (err error) //sys Utime(path string, buf *Utimbuf) (err error)
func NsecToTimespec(nsec int64) (ts Timespec) { func setTimespec(sec, nsec int64) Timespec {
ts.Sec = nsec / 1e9 return Timespec{Sec: sec, Nsec: nsec}
ts.Nsec = nsec % 1e9
return
} }
func NsecToTimeval(nsec int64) (tv Timeval) { func setTimeval(sec, usec int64) Timeval {
nsec += 999 // round up to microsecond return Timeval{Sec: sec, Usec: int32(usec)}
tv.Sec = nsec / 1e9
tv.Usec = int32(nsec % 1e9 / 1e3)
return
} }
func (r *PtraceRegs) PC() uint64 { return r.Tpc } func (r *PtraceRegs) PC() uint64 { return r.Tpc }
......
...@@ -6,17 +6,12 @@ ...@@ -6,17 +6,12 @@
package unix package unix
func NsecToTimespec(nsec int64) (ts Timespec) { func setTimespec(sec, nsec int64) Timespec {
ts.Sec = int64(nsec / 1e9) return Timespec{Sec: sec, Nsec: int32(nsec)}
ts.Nsec = int32(nsec % 1e9)
return
} }
func NsecToTimeval(nsec int64) (tv Timeval) { func setTimeval(sec, usec int64) Timeval {
nsec += 999 // round up to microsecond return Timeval{Sec: sec, Usec: int32(usec)}
tv.Usec = int32(nsec % 1e9 / 1e3)
tv.Sec = int64(nsec / 1e9)
return
} }
func SetKevent(k *Kevent_t, fd, mode, flags int) { func SetKevent(k *Kevent_t, fd, mode, flags int) {
......
...@@ -6,17 +6,12 @@ ...@@ -6,17 +6,12 @@
package unix package unix
func NsecToTimespec(nsec int64) (ts Timespec) { func setTimespec(sec, nsec int64) Timespec {
ts.Sec = int64(nsec / 1e9) return Timespec{Sec: sec, Nsec: nsec}
ts.Nsec = int64(nsec % 1e9)
return
} }
func NsecToTimeval(nsec int64) (tv Timeval) { func setTimeval(sec, usec int64) Timeval {
nsec += 999 // round up to microsecond return Timeval{Sec: sec, Usec: int32(usec)}
tv.Usec = int32(nsec % 1e9 / 1e3)
tv.Sec = int64(nsec / 1e9)
return
} }
func SetKevent(k *Kevent_t, fd, mode, flags int) { func SetKevent(k *Kevent_t, fd, mode, flags int) {
......
...@@ -6,17 +6,12 @@ ...@@ -6,17 +6,12 @@
package unix package unix
func NsecToTimespec(nsec int64) (ts Timespec) { func setTimespec(sec, nsec int64) Timespec {
ts.Sec = int64(nsec / 1e9) return Timespec{Sec: sec, Nsec: int32(nsec)}
ts.Nsec = int32(nsec % 1e9)
return
} }
func NsecToTimeval(nsec int64) (tv Timeval) { func setTimeval(sec, usec int64) Timeval {
nsec += 999 // round up to microsecond return Timeval{Sec: sec, Usec: int32(usec)}
tv.Usec = int32(nsec % 1e9 / 1e3)
tv.Sec = int64(nsec / 1e9)
return
} }
func SetKevent(k *Kevent_t, fd, mode, flags int) { func SetKevent(k *Kevent_t, fd, mode, flags int) {
......
...@@ -6,17 +6,12 @@ ...@@ -6,17 +6,12 @@
package unix package unix
func NsecToTimespec(nsec int64) (ts Timespec) { func setTimespec(sec, nsec int64) Timespec {
ts.Sec = int64(nsec / 1e9) return Timespec{Sec: sec, Nsec: int32(nsec)}
ts.Nsec = int32(nsec % 1e9)
return
} }
func NsecToTimeval(nsec int64) (tv Timeval) { func setTimeval(sec, usec int64) Timeval {
nsec += 999 // round up to microsecond return Timeval{Sec: sec, Usec: int32(usec)}
tv.Usec = int32(nsec % 1e9 / 1e3)
tv.Sec = int64(nsec / 1e9)
return
} }
func SetKevent(k *Kevent_t, fd, mode, flags int) { func SetKevent(k *Kevent_t, fd, mode, flags int) {
......
...@@ -6,17 +6,12 @@ ...@@ -6,17 +6,12 @@
package unix package unix
func NsecToTimespec(nsec int64) (ts Timespec) { func setTimespec(sec, nsec int64) Timespec {
ts.Sec = nsec / 1e9 return Timespec{Sec: sec, Nsec: nsec}
ts.Nsec = nsec % 1e9
return
} }
func NsecToTimeval(nsec int64) (tv Timeval) { func setTimeval(sec, usec int64) Timeval {
nsec += 999 // round up to microsecond return Timeval{Sec: sec, Usec: usec}
tv.Usec = nsec % 1e9 / 1e3
tv.Sec = nsec / 1e9
return
} }
func SetKevent(k *Kevent_t, fd, mode, flags int) { func SetKevent(k *Kevent_t, fd, mode, flags int) {
......
...@@ -6,17 +6,12 @@ ...@@ -6,17 +6,12 @@
package unix package unix
func NsecToTimespec(nsec int64) (ts Timespec) { func setTimespec(sec, nsec int64) Timespec {
ts.Sec = int64(nsec / 1e9) return Timespec{Sec: sec, Nsec: int32(nsec)}
ts.Nsec = int32(nsec % 1e9)
return
} }
func NsecToTimeval(nsec int64) (tv Timeval) { func setTimeval(sec, usec int64) Timeval {
nsec += 999 // round up to microsecond return Timeval{Sec: sec, Usec: int32(usec)}
tv.Usec = int32(nsec % 1e9 / 1e3)
tv.Sec = int64(nsec / 1e9)
return
} }
func SetKevent(k *Kevent_t, fd, mode, flags int) { func SetKevent(k *Kevent_t, fd, mode, flags int) {
......
...@@ -6,17 +6,12 @@ ...@@ -6,17 +6,12 @@
package unix package unix
func NsecToTimespec(nsec int64) (ts Timespec) { func setTimespec(sec, nsec int64) Timespec {
ts.Sec = nsec / 1e9 return Timespec{Sec: sec, Nsec: nsec}
ts.Nsec = nsec % 1e9
return
} }
func NsecToTimeval(nsec int64) (tv Timeval) { func setTimeval(sec, usec int64) Timeval {
nsec += 999 // round up to microsecond return Timeval{Sec: sec, Usec: usec}
tv.Usec = nsec % 1e9 / 1e3
tv.Sec = int64(nsec / 1e9)
return
} }
func (iov *Iovec) SetLen(length int) { func (iov *Iovec) SetLen(length int) {
......
...@@ -10,6 +10,31 @@ package unix ...@@ -10,6 +10,31 @@ package unix
// nanoseconds since the Unix epoch. // nanoseconds since the Unix epoch.
func TimespecToNsec(ts Timespec) int64 { return int64(ts.Sec)*1e9 + int64(ts.Nsec) } 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 // TimevalToNsec converts a Timeval value into a number of nanoseconds
// since the Unix epoch. // since the Unix epoch.
func TimevalToNsec(tv Timeval) int64 { return int64(tv.Sec)*1e9 + int64(tv.Usec)*1e3 } 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)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment