diff --git a/pythonosc/parsing/ntp.py b/pythonosc/parsing/ntp.py index e7846c4b21e765e7a2bfb575ca3d22fb9d9ff424..737f2728a3006c672747b0ca18244a65f3d254be 100644 --- a/pythonosc/parsing/ntp.py +++ b/pythonosc/parsing/ntp.py @@ -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. - - 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)) + """ since 1970 => since 1900 64b OSC """ + sec_1970 = int(date) + sec_1900 = sec_1970 + JAN_1970 + + sec_frac = float(date - sec_1970) + picos = int(sec_frac * SECS_TO_PICOS) + + return struct.pack('>I', int(sec_1900)) + struct.pack('>I', picos) diff --git a/pythonosc/parsing/osc_types.py b/pythonosc/parsing/osc_types.py index 9279adc177ae7bcbcd4255b91d0e8e5aac12799a..c385075c0bd1c3122edb2357989d3d0b36416e81 100644 --- a/pythonosc/parsing/osc_types.py +++ b/pythonosc/parsing/osc_types.py @@ -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: