Skip to content
Snippets Groups Projects
Commit f56b9c12 authored by nik's avatar nik
Browse files

changed implementation of ntp timestamp conversion

Ported from the OSC (scosc?) implementation ...
parent b735de81
Branches
No related tags found
No related merge requests found
......@@ -5,6 +5,9 @@ import struct
import time
JAN_1970 = 2208988800
SECS_TO_PICOS = 4294967296
# 63 zero bits followed by a one in the least signifigant bit is a special
# case meaning "immediately."
IMMEDIATELY = struct.pack('>q', 1)
......@@ -26,15 +29,12 @@ def ntp_to_system_time(date):
"""
return date - _NTP_DELTA
def system_time_to_ntp(date):
"""Convert a system time to a NTP time datagram.
""" since 1970 => since 1900 64b OSC """
sec_1970 = int(date)
sec_1900 = sec_1970 + JAN_1970
System time is reprensented by seconds since the epoch in UTC.
"""
try:
ntp = date + _NTP_DELTA
except TypeError as ve:
raise NtpError('Invalud date: {}'.format(ve))
num_secs, fraction = str(ntp).split('.')
return struct.pack('>I', int(num_secs)) + struct.pack('>I', int(fraction))
sec_frac = float(date - sec_1970)
picos = int(sec_frac * SECS_TO_PICOS)
return struct.pack('>I', int(sec_1900)) + struct.pack('>I', picos)
......@@ -112,7 +112,7 @@ def get_int(dgram, start_index):
if len(dgram[start_index:]) < _INT_DGRAM_LEN:
raise ParseError('Datagram is too short')
return (
struct.unpack('>i',
struct.unpack('>I',
dgram[start_index:start_index + _INT_DGRAM_LEN])[0],
start_index + _INT_DGRAM_LEN)
except (struct.error, TypeError) as e:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment