# Path: ogn/parser/utils.py
# def createTimestamp(time_string, reference_timestamp):
#     if time_string[-1] == "z":
#         dd = int(time_string[0:2])
#         hh = int(time_string[2:4])
#         mm = int(time_string[4:6])
# 
#         result = datetime(reference_timestamp.year,
#                           reference_timestamp.month,
#                           dd,
#                           hh, mm, 0,
#                           tzinfo=timezone.utc if reference_timestamp.tzinfo is not None else None)
# 
#         # correct wrong month
#         if result > reference_timestamp + timedelta(days=14):
#             result = (result.replace(day=1) - timedelta(days=14)).replace(day=result.day)
#         elif result < reference_timestamp - timedelta(days=14):
#             result = (result.replace(day=28) + timedelta(days=14)).replace(day=result.day)
#     else:
#         hh = int(time_string[0:2])
#         mm = int(time_string[2:4])
#         ss = int(time_string[4:6])
# 
#         result = datetime(reference_timestamp.year,
#                           reference_timestamp.month,
#                           reference_timestamp.day,
#                           hh, mm, ss,
#                           tzinfo=timezone.utc if reference_timestamp.tzinfo is not None else None)
# 
#         if result > reference_timestamp + timedelta(hours=12):
#             # shift timestamp to previous day
#             result -= timedelta(days=1)
#         elif result < reference_timestamp - timedelta(hours=12):
#             # shift timestamp to next day
#             result += timedelta(days=1)
# 
#     return result
# 
# Path: ogn/parser/pattern.py
# PATTERN_TELNET_50001 = re.compile(r"""
#     (?P<pps_offset>\d\.\d+)sec:(?P<frequency>\d+\.\d+)MHz:\s+
#     (?P<aircraft_type>\d):(?P<address_type>\d):(?P<address>[A-F0-9]{6})\s
#     (?P<timestamp>\d{6}):\s
#     \[\s*(?P<latitude>[+-]\d+\.\d+),\s*(?P<longitude>[+-]\d+\.\d+)\]deg\s*
#     (?P<altitude>\d+)m\s*
#     (?P<climb_rate>[+-]\d+\.\d+)m/s\s*
#     (?P<ground_speed>\d+\.\d+)m/s\s*
#     (?P<track>\d+\.\d+)deg\s*
#     (?P<turn_rate>[+-]\d+\.\d+)deg/sec\s*
#     (?P<magic_number>\d+)\s*
#     (?P<gps_status>[0-9x]+)m\s*
#     (?P<channel>\d+)(?P<flarm_timeslot>[f_])(?P<ogn_timeslot>[o_])\s*
#     (?P<frequency_offset>[+-]\d+\.\d+)kHz\s*
#     (?P<decode_quality>\d+\.\d+)/(?P<signal_quality>\d+\.\d+)dB/(?P<demodulator_type>\d+)\s+
#     (?P<error_count>\d+)e\s*
#     (?P<distance>\d+\.\d+)km\s*
#     (?P<bearing>\d+\.\d+)deg\s*
#     (?P<phi>[+-]\d+\.\d+)deg\s*
#     (?P<multichannel>\+)?\s*
#     \?\s*
#     R?\s*
#     (B(?P<baro_altitude>\d+))?
# """, re.VERBOSE | re.MULTILINE)


from datetime import datetime
from ogn.parser.utils import createTimestamp
from ogn.parser.pattern import PATTERN_TELNET_50001


telnet_50001_pattern = PATTERN_TELNET_50001


def parse(telnet_data):
    reference_timestamp = datetime.utcnow()

    match = telnet_50001_pattern.match(telnet_data)
    if match:

# Path: ogn/parser/utils.py
# def createTimestamp(time_string, reference_timestamp):
#     if time_string[-1] == "z":
#         dd = int(time_string[0:2])
#         hh = int(time_string[2:4])
#         mm = int(time_string[4:6])
# 
#         result = datetime(reference_timestamp.year,
#                           reference_timestamp.month,
#                           dd,
#                           hh, mm, 0,
#                           tzinfo=timezone.utc if reference_timestamp.tzinfo is not None else None)
# 
#         # correct wrong month
#         if result > reference_timestamp + timedelta(days=14):
#             result = (result.replace(day=1) - timedelta(days=14)).replace(day=result.day)
#         elif result < reference_timestamp - timedelta(days=14):
#             result = (result.replace(day=28) + timedelta(days=14)).replace(day=result.day)
#     else:
#         hh = int(time_string[0:2])
#         mm = int(time_string[2:4])
#         ss = int(time_string[4:6])
# 
#         result = datetime(reference_timestamp.year,
#                           reference_timestamp.month,
#                           reference_timestamp.day,
#                           hh, mm, ss,
#                           tzinfo=timezone.utc if reference_timestamp.tzinfo is not None else None)
# 
#         if result > reference_timestamp + timedelta(hours=12):
#             # shift timestamp to previous day
#             result -= timedelta(days=1)
#         elif result < reference_timestamp - timedelta(hours=12):
#             # shift timestamp to next day
#             result += timedelta(days=1)
# 
#     return result
# 
# Path: ogn/parser/pattern.py
# PATTERN_TELNET_50001 = re.compile(r"""
#     (?P<pps_offset>\d\.\d+)sec:(?P<frequency>\d+\.\d+)MHz:\s+
#     (?P<aircraft_type>\d):(?P<address_type>\d):(?P<address>[A-F0-9]{6})\s
#     (?P<timestamp>\d{6}):\s
#     \[\s*(?P<latitude>[+-]\d+\.\d+),\s*(?P<longitude>[+-]\d+\.\d+)\]deg\s*
#     (?P<altitude>\d+)m\s*
#     (?P<climb_rate>[+-]\d+\.\d+)m/s\s*
#     (?P<ground_speed>\d+\.\d+)m/s\s*
#     (?P<track>\d+\.\d+)deg\s*
#     (?P<turn_rate>[+-]\d+\.\d+)deg/sec\s*
#     (?P<magic_number>\d+)\s*
#     (?P<gps_status>[0-9x]+)m\s*
#     (?P<channel>\d+)(?P<flarm_timeslot>[f_])(?P<ogn_timeslot>[o_])\s*
#     (?P<frequency_offset>[+-]\d+\.\d+)kHz\s*
#     (?P<decode_quality>\d+\.\d+)/(?P<signal_quality>\d+\.\d+)dB/(?P<demodulator_type>\d+)\s+
#     (?P<error_count>\d+)e\s*
#     (?P<distance>\d+\.\d+)km\s*
#     (?P<bearing>\d+\.\d+)deg\s*
#     (?P<phi>[+-]\d+\.\d+)deg\s*
#     (?P<multichannel>\+)?\s*
#     \?\s*
#     R?\s*
#     (B(?P<baro_altitude>\d+))?
# """, re.VERBOSE | re.MULTILINE)


# Path: ogn/parser/telnet_parser.py
from datetime import datetime
from ogn.parser.utils import createTimestamp
from ogn.parser.pattern import PATTERN_TELNET_50001



telnet_50001_pattern = PATTERN_TELNET_50001


def parse(telnet_data):
    reference_timestamp = datetime.utcnow()

    match = telnet_50001_pattern.match(telnet_data)
    if match:

        return {'pps_offset': float(match.group('pps_offset')),