-
Notifications
You must be signed in to change notification settings - Fork 580
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unix: add unix.TimeToPtpClockTime on Linux
Add time conversion helpers for PtpClockTime similar to those existing for Timespec and Timeval. Allows to use the maths of the standard time package while avoiding the boilerplate: write "ptpt.Time()" instead of "time.Unix(ptpt.Sec, int64(ptpt.Nsec))", and "unix.TimeToPtpClockTime(t)" instead of "unix.PtpClockTime{Sec: t.Unix(), Nsec: uint32(t.Nanosecond())}".
- Loading branch information
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2024 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
//go:build linux | ||
|
||
package unix | ||
|
||
import "time" | ||
|
||
// TimeToPtpClockTime returns t as PtpClockTime | ||
func TimeToPtpClockTime(t time.Time) PtpClockTime { | ||
sec := t.Unix() | ||
nsec := uint32(t.Nanosecond()) | ||
return PtpClockTime{Sec: sec, Nsec: nsec} | ||
} | ||
|
||
// Time returns PTPClockTime as time.Time | ||
func (t *PtpClockTime) Time() time.Time { | ||
return time.Unix(t.Sec, int64(t.Nsec)) | ||
} | ||
|
||
// Unix returns the time stored in t as seconds plus nanoseconds. | ||
func (t *PtpClockTime) Unix() (sec int64, nsec int64) { | ||
return t.Sec, int64(t.Nsec) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2024 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 linux | ||
|
||
package unix_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func TestTimeToPtpClockTime(t *testing.T) { | ||
testcases := []struct { | ||
time time.Time | ||
}{ | ||
{time.Unix(0, 0)}, | ||
{time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)}, | ||
{time.Date(2262, time.December, 31, 23, 0, 0, 0, time.UTC)}, | ||
{time.Unix(0x7FFFFFFF, 0)}, | ||
{time.Unix(0x80000000, 0)}, | ||
{time.Unix(0x7FFFFFFF, 1000000000)}, | ||
{time.Unix(0x7FFFFFFF, 999999999)}, | ||
{time.Unix(-0x80000000, 0)}, | ||
{time.Unix(-0x80000001, 0)}, | ||
{time.Date(2038, time.January, 19, 3, 14, 7, 0, time.UTC)}, | ||
{time.Date(2038, time.January, 19, 3, 14, 8, 0, time.UTC)}, | ||
{time.Date(1901, time.December, 13, 20, 45, 52, 0, time.UTC)}, | ||
{time.Date(1901, time.December, 13, 20, 45, 51, 0, time.UTC)}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
ts := unix.TimeToPtpClockTime(tc.time) | ||
tstime := time.Unix(int64(ts.Sec), int64(ts.Nsec)) | ||
if !tstime.Equal(tc.time) { | ||
t.Errorf("TimeToPtpClockTime(%v) is the time %v", tc.time, tstime) | ||
} | ||
} | ||
} |