|
| 1 | +// Copyright 2024 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 linux |
| 6 | + |
| 7 | +package unix_test |
| 8 | + |
| 9 | +import ( |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "golang.org/x/sys/unix" |
| 14 | +) |
| 15 | + |
| 16 | +func TestTimeToPtpClockTime(t *testing.T) { |
| 17 | + testcases := []struct { |
| 18 | + time time.Time |
| 19 | + }{ |
| 20 | + {time.Unix(0, 0)}, |
| 21 | + {time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)}, |
| 22 | + {time.Date(2262, time.December, 31, 23, 0, 0, 0, time.UTC)}, |
| 23 | + {time.Unix(0x7FFFFFFF, 0)}, |
| 24 | + {time.Unix(0x80000000, 0)}, |
| 25 | + {time.Unix(0x7FFFFFFF, 1000000000)}, |
| 26 | + {time.Unix(0x7FFFFFFF, 999999999)}, |
| 27 | + {time.Unix(-0x80000000, 0)}, |
| 28 | + {time.Unix(-0x80000001, 0)}, |
| 29 | + {time.Date(2038, time.January, 19, 3, 14, 7, 0, time.UTC)}, |
| 30 | + {time.Date(2038, time.January, 19, 3, 14, 8, 0, time.UTC)}, |
| 31 | + {time.Date(1901, time.December, 13, 20, 45, 52, 0, time.UTC)}, |
| 32 | + {time.Date(1901, time.December, 13, 20, 45, 51, 0, time.UTC)}, |
| 33 | + } |
| 34 | + |
| 35 | + for _, tc := range testcases { |
| 36 | + ts := unix.TimeToPtpClockTime(tc.time) |
| 37 | + tstime := time.Unix(int64(ts.Sec), int64(ts.Nsec)) |
| 38 | + if !tstime.Equal(tc.time) { |
| 39 | + t.Errorf("TimeToPtpClockTime(%v) is the time %v", tc.time, tstime) |
| 40 | + } |
| 41 | + } |
| 42 | +} |
0 commit comments