thanos1983 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

This is probably something really simple but I have spend a big amount of time and still can not figure it where I am going wrong.

I am using Net::NTP module to get the time of the reference server. Next I am trying to subtract the time of mu local OS with the reference time to get the offset. But along the way I am probably doing something extremely wrong on my calculation and the number is extremely big.

I would be expecting to see a number close to -0.00841593742370605 sec but instead I get 1687.41735005379. Can someone guide to understand where I am going so wrong.

Sample of code with the process:

#!/usr/bin/perl use strict; use warnings; use Net::NTP; use Time::HiRes qw( gettimeofday ); my ($seconds, $microseconds) = gettimeofday; my %response = get_ntp_response("0.se.pool.ntp.org",123); my $localHostTime = $seconds . "." . $microseconds; my $offSet = $localHostTime - $response{'Reference Timestamp'}; print "This is what I want: ".$response{'Offset'}."\n"; print "This is what I get: ".$offSet."\n"; __END__ This is what I want: -0.00841593742370605 This is what I get: 1687.41735005379

Thank you all for your time and effort reading and replying to my question.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re: How to calculate the offset of your local time against an NTP reference server
by martin (Friar) on Feb 09, 2015 at 00:24 UTC

    The Reference Timestamp is the time at which the server clock was last set or corrected. That does not tell anything about the local clock offset.

    In order to guess the local clock offset an SNTP client has to evaluate four timestamps:

    t1 = local clock, time request sent by client;
    t2 = server clock, time request received by server;
    t3 = server clock, time reply sent by server;
    t4 = local clock, time reply received by client

    The offset o is then calculated as:

    o = ((t2 - t1) + (t3 - t4)) / 2

    Net::NTP will save you the trouble of doing this explicitly and give you the offset right away. It will also not suffer additional inaccuracy from the delay between your gettimeofday() and get_ntp_response() calls.

      Hello Martin,

      Thank you for your time and effort reading my question and replying. I was afraid that I was so wrong when I also read the RFC4330 where it explains the way it should it should be calculated.

      As you mentioned I do not really need to calculate the offset since I can use the Net::NTP module. The reason that I need to use it is I am planning to apply this process on several remote OS through SSH, where I will not be able to have the NET::NTP module available.

      Again thank you for your feedback it is really valuable to me to contineou with my experimentation.

      Seeking for Perl wisdom...on the process of learning...not there...yet!

        If you can run your own script, then you can use Net::NTP

        It is pure perl, with only core dependencies.