github.com/fletavendor/sys@v0.0.0-20181107165924-66b7b1311ac8/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  // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
     6  
     7  package unix_test
     8  
     9  import (
    10  	"testing"
    11  	"time"
    12  	"unsafe"
    13  
    14  	"golang.org/x/sys/unix"
    15  )
    16  
    17  func TestTimeToTimespec(t *testing.T) {
    18  	timeTests := []struct {
    19  		time  time.Time
    20  		valid bool
    21  	}{
    22  		{time.Unix(0, 0), true},
    23  		{time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), true},
    24  		{time.Date(2262, time.December, 31, 23, 0, 0, 0, time.UTC), false},
    25  		{time.Unix(0x7FFFFFFF, 0), true},
    26  		{time.Unix(0x80000000, 0), false},
    27  		{time.Unix(0x7FFFFFFF, 1000000000), false},
    28  		{time.Unix(0x7FFFFFFF, 999999999), true},
    29  		{time.Unix(-0x80000000, 0), true},
    30  		{time.Unix(-0x80000001, 0), false},
    31  		{time.Date(2038, time.January, 19, 3, 14, 7, 0, time.UTC), true},
    32  		{time.Date(2038, time.January, 19, 3, 14, 8, 0, time.UTC), false},
    33  		{time.Date(1901, time.December, 13, 20, 45, 52, 0, time.UTC), true},
    34  		{time.Date(1901, time.December, 13, 20, 45, 51, 0, time.UTC), false},
    35  	}
    36  
    37  	// Currently all targets have either int32 or int64 for Timespec.Sec.
    38  	// If there were a new target with unsigned or floating point type for
    39  	// it, this test must be adjusted.
    40  	have64BitTime := (unsafe.Sizeof(unix.Timespec{}.Sec) == 8)
    41  	for _, tt := range timeTests {
    42  		ts, err := unix.TimeToTimespec(tt.time)
    43  		tt.valid = tt.valid || have64BitTime
    44  		if tt.valid && err != nil {
    45  			t.Errorf("TimeToTimespec(%v): %v", tt.time, err)
    46  		}
    47  		if err == nil {
    48  			tstime := time.Unix(int64(ts.Sec), int64(ts.Nsec))
    49  			if !tstime.Equal(tt.time) {
    50  				t.Errorf("TimeToTimespec(%v) is the time %v", tt.time, tstime)
    51  			}
    52  		}
    53  	}
    54  }