-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: x/sys/unix: add methods to convert between time.Time and unix.PtpClockTime on Linux #70032
Comments
Change https://go.dev/cl/621498 mentions this issue: |
Summary: Pull Request resolved: #418 Avoid the non-upstreamed unix.PtpClockTime.Time(). The related [golang/go#70032](golang/go#70032) is still pending decision. In case of positive outcome we can unland this. Reviewed By: leoleovich Differential Revision: D64901381 fbshipit-source-id: b06c389dff7f0720391bc8dac81e6a346a111a58
This proposal has been added to the active column of the proposals project |
I'm probably missing some background on this. If you convert to a time.Time to do time math and then back to a unix.PtpClockTime, what exactly is the point of unix.PtpClockTime? It seems like any additional information it could carry would be lost. |
I'd still like to understand if there's any danger of lossiness in the conversion to/from |
Bump. It seems like there's no lossiness today, but I don't really understand what the plan is with the "reserved" bits. |
The reserved bits MUST be set to zero in all PTP-related IOCTLs. In fact, the v2 of each of those calls were created to reject non-zero uses of the reserved bytes. In other words, nothing today should be setting those bytes to anything but zero. I hope this clears it up. |
Thanks. I think we should generally clean up these APIs. Right now we have: // type Timespec
func NsecToTimespec(nsec int64) Timespec
func TimeToTimespec(t time.Time) (Timespec, error)
func (ts *Timespec) Nano() int64
func (ts *Timespec) Unix() (sec int64, nsec int64)
// type Timeval
func NsecToTimeval(nsec int64) Timeval
func (tv *Timeval) Nano() int64
func (tv *Timeval) Unix() (sec int64, nsec int64)
// type PtpClockTime
// ... nothing ... It seems clear that we should at least complete the product of these types and operations: // type Timeval
func TimeToTimeval(t time.Time) (Timeval, error)
// type PtpClockTime
func NsecToPtpClockTime(nsec int64) PtpClockTime
func TimeToPtpClockTime(t time.Time) (PtpClockTime, error)
func (t *PtpClockTime) Nano() int64
func (t *PtpClockTime) Unix() (sec int64, nsec int64) We could also add a |
@aclements That works, too – if you are happy with that, I can update the proposal. |
We also want to be very clear that it's only a storage format conversion – there's nothing done to the underlying values here, e.g. no implicit adjustments for the UTC-TAI offset or any other leap second manipulations. Indeed, it would be convenient to track possible offsets (e.g. PHC can be in TAI, per local policy) as the underlying time's Location, but time.Unix() always assumes Location as time.Local (which is itself may be a bug – shouldn't it be time.UTC?), and there's no way to override that. But that's a problem outside of the scope of this proposal. |
Your mention of TAI worries me. Do PTP clock times follow TAI or UTC? Do PTP clock times imply a location (e.g., UTC)? If so, maybe it should have a method that goes straight to time.Time with the appropriate Location, rather than going through |
PTP clock can be either, TAI or UTC, it is deployment-specific. There is no location information currently tracked in PtpClockTime; the convertion to time.Time would happen just like it is for time.Unix, assuming the default location. It's on the user to track and apply any coversion logic between TAI and UTC, whatever local considerations are, e.g. apply leap second smearing. The proposal deliberately leaves that out of scope. |
Thanks for clarifying that. Given that PTP can be TAI, I actually think we should not have a Could we use take a PtpClockTime in conjunction with a PtpSysOffsetPrecise or PtpSysOffsetExtended (or PtpSysOffset? I don't really understand the difference between all these) to create a time.Time? If I understand this correctly, that would seem to be an API that encourages doing the right thing in that conversion. This is the best reference I've found on these APIs: https://github.com/torvalds/linux/blob/master/include/uapi/linux/ptp_clock.h |
Proposal Details
Recently we have exposed the syscalls and types related to PTP (IEEE 1588) on Linux in the
unix
package.Amongs those is a struct representing time in PTP calls,
unix.PtpClockTime
. Semantically it is very similar to Unix time, differing in underlying type for the nanoseconds field and having a placeholder for future extentions.The proposal is to add the convenience methods to carry conversion between unix.PtpClockTime and time.Time:
This shall allow to use the maths of the standard time package while avoiding
the boilerplate: write
ptpt.Time()
instead oftime.Unix(ptpt.Sec, int64(ptpt.Nsec))
,and
unix.TimeToPtpClockTime(t)
instead ofunix.PtpClockTime{Sec: t.Unix(), Nsec: uint32(t.Nanosecond())}
.As an example of what the boilerplate looks like in real-world code, please see facebook/time#418.
golang/sys#230 implements the proposal.
Thank you for consideration.
The text was updated successfully, but these errors were encountered: