mirror of
https://github.com/golang/sys.git
synced 2026-01-29 07:02:06 +03:00
Done with: go get go@1.18 go mod tidy go fix ./... Using go1.21.3. Also update code generators to use only the new go:build lines, not the old +build ones. For golang/go#60268. Change-Id: I6aabc42efb6ab3329981100e1db2263aac5e92a6 Reviewed-on: https://go-review.googlesource.com/c/sys/+/534222 Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
55 lines
1.7 KiB
Go
55 lines
1.7 KiB
Go
// Copyright 2017 The Go Authors. All right 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
|
|
|
|
package unix_test
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
"unsafe"
|
|
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
func TestTimeToTimespec(t *testing.T) {
|
|
timeTests := []struct {
|
|
time time.Time
|
|
valid bool
|
|
}{
|
|
{time.Unix(0, 0), true},
|
|
{time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), true},
|
|
{time.Date(2262, time.December, 31, 23, 0, 0, 0, time.UTC), false},
|
|
{time.Unix(0x7FFFFFFF, 0), true},
|
|
{time.Unix(0x80000000, 0), false},
|
|
{time.Unix(0x7FFFFFFF, 1000000000), false},
|
|
{time.Unix(0x7FFFFFFF, 999999999), true},
|
|
{time.Unix(-0x80000000, 0), true},
|
|
{time.Unix(-0x80000001, 0), false},
|
|
{time.Date(2038, time.January, 19, 3, 14, 7, 0, time.UTC), true},
|
|
{time.Date(2038, time.January, 19, 3, 14, 8, 0, time.UTC), false},
|
|
{time.Date(1901, time.December, 13, 20, 45, 52, 0, time.UTC), true},
|
|
{time.Date(1901, time.December, 13, 20, 45, 51, 0, time.UTC), false},
|
|
}
|
|
|
|
// Currently all targets have either int32 or int64 for Timespec.Sec.
|
|
// If there were a new target with unsigned or floating point type for
|
|
// it, this test must be adjusted.
|
|
have64BitTime := (unsafe.Sizeof(unix.Timespec{}.Sec) == 8)
|
|
for _, tt := range timeTests {
|
|
ts, err := unix.TimeToTimespec(tt.time)
|
|
tt.valid = tt.valid || have64BitTime
|
|
if tt.valid && err != nil {
|
|
t.Errorf("TimeToTimespec(%v): %v", tt.time, err)
|
|
}
|
|
if err == nil {
|
|
tstime := time.Unix(int64(ts.Sec), int64(ts.Nsec))
|
|
if !tstime.Equal(tt.time) {
|
|
t.Errorf("TimeToTimespec(%v) is the time %v", tt.time, tstime)
|
|
}
|
|
}
|
|
}
|
|
}
|