def get_date(dgram, start_index): """Get a 64-bit big-endian fixed-point time tag as a date from the datagram. According to the specifications, a date is represented as is: "the first 32 bits specify the number of seconds since midnight on January 1, 1900, and the last 32 bits specify fractional parts of a second to a precision of about 200 picoseconds". Args: dgram: A datagram packet. start_index: An index where the date starts in the datagram. Returns: A tuple containing the system date and the new end index. returns osc_immediately (0) if the corresponding OSC sequence was found. Raises: ParseError if the datagram could not be parsed. """ # Check for the special case first. if dgram[start_index:start_index + _DATE_DGRAM_LEN] == ntp.IMMEDIATELY: return IMMEDIATELY, start_index + _DATE_DGRAM_LEN if len(dgram[start_index:]) < _DATE_DGRAM_LEN: raise ParseError('Datagram is too short') num_secs, start_index = get_int(dgram, start_index) fraction, start_index = get_int(dgram, start_index) # Sum seconds and fraction of second: system_time = num_secs + (fraction / ntp.FRACTIONAL_CONVERSION) print(ntp.ntp_to_system_time(system_time)) return ntp.ntp_to_system_time(system_time), start_index
def get_date(dgram, start_index): """Get a 64-bit big-endian fixed-point time tag as a date from the datagram. According to the specifications, a date is represented as is: "the first 32 bits specify the number of seconds since midnight on January 1, 1900, and the last 32 bits specify fractional parts of a second to a precision of about 200 picoseconds". Args: dgram: A datagram packet. start_index: An index where the date starts in the datagram. Returns: A tuple containing the system date and the new end index. returns osc_immediately (0) if the corresponding OSC sequence was found. Raises: ParseError if the datagram could not be parsed. """ # Check for the special case first. if dgram[start_index:start_index + _DATE_DGRAM_LEN] == ntp.IMMEDIATELY: return IMMEDIATELY, start_index + _DATE_DGRAM_LEN if len(dgram[start_index:]) < _DATE_DGRAM_LEN: raise ParseError('Datagram is too short') num_secs, start_index = get_int(dgram, start_index) fraction, start_index = get_int(dgram, start_index) # Get a decimal representation from those two values. dec = decimal.Decimal(str(num_secs) + '.' + str(fraction)) # And convert it to float simply. system_time = float(dec) return ntp.ntp_to_system_time(system_time), start_index
def get_date(dgram: bytes, start_index: int) -> Tuple[Union[int, float], int]: """Get a 64-bit big-endian fixed-point time tag as a date from the datagram. According to the specifications, a date is represented as is: "the first 32 bits specify the number of seconds since midnight on January 1, 1900, and the last 32 bits specify fractional parts of a second to a precision of about 200 picoseconds". Args: dgram: A datagram packet. start_index: An index where the date starts in the datagram. Returns: A tuple containing the system date and the new end index. returns osc_immediately (0) if the corresponding OSC sequence was found. Raises: ParseError if the datagram could not be parsed. """ # Check for the special case first. if dgram[start_index:start_index + _DATE_DGRAM_LEN] == ntp.IMMEDIATELY: return IMMEDIATELY, start_index + _DATE_DGRAM_LEN if len(dgram[start_index:]) < _DATE_DGRAM_LEN: raise ParseError('Datagram is too short') num_secs, start_index = get_int(dgram, start_index) fraction, start_index = get_int(dgram, start_index) # Sum seconds and fraction of second: system_time = num_secs + (fraction / ntp.FRACTIONAL_CONVERSION) return ntp.ntp_to_system_time(system_time), start_index
def test_nto_to_system_time(self): unix_time = time.time() timestamp = ntp.system_time_to_ntp(unix_time) unix_time2 = ntp.ntp_to_system_time(timestamp) self.assertTrue(type(unix_time) is float) self.assertTrue(type(timestamp) is bytes) self.assertTrue(type(unix_time2) is float) self.assertEqual(round(unix_time, 6), round(unix_time2, 6))
def test_nto_to_system_time(self): self.assertGreater(0, ntp.ntp_to_system_time(0))