github.com/giovannyortegon/go@v0.0.0-20220115155912-8890063f5bdd/src/pkg/mod/golang.org/x/sys@v0.0.0-20210927094055-39ccf1dd6fa6/unix/timestruct_test.go (about)

     1  // Copyright 2017 The Go Authors. All right reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
     6  // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
     7  
     8  package unix_test
     9  
    10  import (
    11  	"testing"
    12  	"time"
    13  	"unsafe"
    14  
    15  	"golang.org/x/sys/unix"
    16  )
    17  
    18  func TestTimeToTimespec(t *testing.T) {
    19  	timeTests := []struct {
    20  		time  time.Time
    21  		valid bool
    22  	}{
    23  		{time.Unix(0, 0), true},
    24  		{time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), true},
    25  		{time.Date(2262, time.December, 31, 23, 0, 0, 0, time.UTC), false},
    26  		{time.Unix(0x7FFFFFFF, 0), true},
    27  		{time.Unix(0x80000000, 0), false},
    28  		{time.Unix(0x7FFFFFFF, 1000000000), false},
    29  		{time.Unix(0x7FFFFFFF, 999999999), true},
    30  		{time.Unix(-0x80000000, 0), true},
    31  		{time.Unix(-0x80000001, 0), false},
    32  		{time.Date(2038, time.January, 19, 3, 14, 7, 0, time.UTC), true},
    33  		{time.Date(2038, time.January, 19, 3, 14, 8, 0, time.UTC), false},
    34  		{time.Date(1901, time.December, 13, 20, 45, 52, 0, time.UTC), true},
    35  		{time.Date(1901, time.December, 13, 20, 45, 51, 0, time.UTC), false},
    36  	}
    37  
    38  	// Currently all targets have either int32 or int64 for Timespec.Sec.
    39  	// If there were a new target with unsigned or floating point type for
    40  	// it, this test must be adjusted.
    41  	have64BitTime := (unsafe.Sizeof(unix.Timespec{}.Sec) == 8)
    42  	for _, tt := range timeTests {
    43  		ts, err := unix.TimeToTimespec(tt.time)
    44  		tt.valid = tt.valid || have64BitTime
    45  		if tt.valid && err != nil {
    46  			t.Errorf("TimeToTimespec(%v): %v", tt.time, err)
    47  		}
    48  		if err == nil {
    49  			tstime := time.Unix(int64(ts.Sec), int64(ts.Nsec))
    50  			if !tstime.Equal(tt.time) {
    51  				t.Errorf("TimeToTimespec(%v) is the time %v", tt.time, tstime)
    52  			}
    53  		}
    54  	}
    55  }