Skip to content
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

[zend-timesync] fix ntp time sync #153

Merged
merged 2 commits into from
Dec 5, 2022
Merged

[zend-timesync] fix ntp time sync #153

merged 2 commits into from
Dec 5, 2022

Conversation

falkenhawk
Copy link
Member

@falkenhawk falkenhawk commented Dec 2, 2022

NTP timestamps are represented as a 64-bit fixed-point number, in seconds relative to 0000 UT on 1 January 1900. The integer part is in the first 32 bits and the fraction part in the last 32 bits.

https://www.eecis.udel.edu/~mills/time.html
https://tickelton.gitlab.io/articles/ntp-timestamps/

But calculation of the fractional part was broken since forever, always sending 00000000 to a ntp server - fractional seconds were not being multiplied by max value of 32-bit+1 to represent the fraction in 0-[(2^32)-1] range, as expected by ntp protocol. In result, Zent_TimeSync_Ntp has always been reporting time in full-second resolution to ntp servers when syncing time.

Moreover, it triggered Implicit conversion from float-string "0.xyz" to int loses precision in Zend/TimeSync/Ntp.php:70 on PHP 8.1 and that's how I started my investigation.

@falkenhawk falkenhawk self-assigned this Dec 2, 2022
@falkenhawk
Copy link
Member Author

for the record, I compared the logic with java implementation TimeStamp::toNtpTime()

NTP timestamps are represented as a 64-bit fixed-point number, in
seconds relative to 0000 UT on 1 January 1900.  The integer part is
in the first 32 bits and the fraction part in the last 32 bits.

But calculation of the fractional part was broken since forever, always sending 00000000 to a ntp server - fractional seconds were not being multiplied by max value of 32-bit+1 to represent the fraction in 0-[(2^32)-1] range, as expected by ntp protocol.
In result, Zent_TimeSync_Ntp has always been reporting time in full-second resolution to ntp servers when syncing time.
@falkenhawk falkenhawk force-pushed the fix-zend-timesync-ntp branch from dca5a99 to 0346982 Compare December 4, 2022 09:29
Copy link
Contributor

@partikus partikus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for a silence. Looks good. Thank you for your effort 👏

@partikus partikus merged commit 897db56 into master Dec 5, 2022
@partikus partikus deleted the fix-zend-timesync-ntp branch December 5, 2022 21:38
@falkenhawk falkenhawk mentioned this pull request Dec 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants