From f56b9c1264f8bbc9f29cc0839a45a5de3fe0a84d Mon Sep 17 00:00:00 2001 From: nik Date: Wed, 9 Mar 2016 01:01:18 +0100 Subject: [PATCH] changed implementation of ntp timestamp conversion Ported from the OSC (scosc?) implementation ... --- pythonosc/parsing/ntp.py | 22 +++++++++++----------- pythonosc/parsing/osc_types.py | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/pythonosc/parsing/ntp.py b/pythonosc/parsing/ntp.py index e7846c4..737f272 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 9279adc..c385075 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: -- GitLab