[ntp:questions] implementation question (Java): Timestamp

Morpheus spam at world.com
Sat Aug 2 20:08:52 UTC 2003


Hi,
i'm here because i'm implementing the NTP protocol in Java for an university
project. I've found some codes around the net, but they didn't clarify me
about timestamp (i've even giva a look at the source code, but it is very
difficult to me because it's complex and it's in C...). Ok, i got my
numbers of milliseconds (which i'll call ms) from 1-1-1970, and i can
divide it in the seconds part and fractional part:

    secs = ms / 1000;
    mlls = ms % 1000;

To obtain seconds in NTP format, i got to add to 'secs' the number of
seconds between 1-1-1900 and 1-1-1970 (which is known or calculable) and i
obtain the number of seconds from 1-1-1900 to now; 'till now it's all
clear. Now it starts the hard part (for me...): i have to 'shift' the
milliseconds till the 'decimal point' between the 32th and 33th bit of the
timestamp. i found someone doing

    fract = (mlls * 0x100000000L ) / 1000;

multiply for 2^32 and divide for 1000 but i found it incorrect... wouldn't
it be better just doing

    fract = mlls * 0x10000000L;

i.e. multiply the milliseconds for 2^28, shifting thus to the last byte?

thanks for any help!



More information about the questions mailing list