rn42v
rn42v

Reputation: 97

Convert NTP Short Format to Seconds

I'm trying to convert the RFC 5905 NTP Short Format to seconds. The NTP Short Format is a uint32_t with the first 16 bits being the seconds (i.e seconds = format >> 16) and the other 16 bits being the fraction of a second (i.e fraction = format & 0xFFFF). After converting I'd like the output as a double.

Upvotes: 0

Views: 872

Answers (2)

rn42v
rn42v

Reputation: 97

With TS_short being the uint32_t RFC 5905 NTP Short Format the following will convert it to seconds as a double:

double secs = (double)(TS_short >> 16) + (double)(TS_short & 0xFFFF) / (double)(1LL << 16);

Upvotes: 0

Ugnes
Ugnes

Reputation: 779

This answer is taken from a Google Group chat. I hope it helps:
Treat it as a 32-bit fraction with the binary point at the left, multiply by a million, keep the integer part.

microsecs = ((unsigned long long) frac * 1000000) >> 32;

To round (can round up to 1000000),

microsecs = ((unsigned long long) frac * 1000000 + (1LL<<31)) >> 32;

Upvotes: 1

Related Questions