From bb8e5db3fb3a5e3a352105985a4121ee860cf763 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Sun, 24 Jul 2022 22:25:40 +0200 Subject: make upload function even more robust This now works if there is really no date given in the GPX file, as done for example by the files produced by BRouter. --- fietsboek/util.py | 41 +++++++++++++++++++++-------------------- fietsboek/views/upload.py | 2 +- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/fietsboek/util.py b/fietsboek/util.py index aff41fe..ddc4832 100644 --- a/fietsboek/util.py +++ b/fietsboek/util.py @@ -127,26 +127,27 @@ def guess_gpx_timezone(gpx): # Next, we look if there's a "localTime" extension on the track, so we can # compare the local time to the time. - for track in gpx.tracks: - time = times[0] - local_time = None - for extension in track.extensions: - if extension.tag.lower() == 'localtime': - local_time = datetime.datetime.fromisoformat( - fix_iso_timestamp(extension.text)).replace(tzinfo=None) - elif extension.tag.lower() == 'time': - time = datetime.datetime.fromisoformat( - fix_iso_timestamp(extension.text)).replace(tzinfo=None) - if time is not None and local_time is not None: - # We found a pair that we can use! - offset = local_time - time - # With all the time madness, luckily most time zones seem to stick - # to an offset that is a multiple of 15 minutes (see - # https://en.wikipedia.org/wiki/List_of_UTC_offsets). We try to - # round the value to the nearest of 15 minutes, to prevent any - # funky offsets from happening due to slight clock desyncs. - offset = round_timedelta_to_multiple(offset, datetime.timedelta(minutes=15)) - return datetime.timezone(offset) + if times: + for track in gpx.tracks: + time = times[0] + local_time = None + for extension in track.extensions: + if extension.tag.lower() == 'localtime': + local_time = datetime.datetime.fromisoformat( + fix_iso_timestamp(extension.text)).replace(tzinfo=None) + elif extension.tag.lower() == 'time': + time = datetime.datetime.fromisoformat( + fix_iso_timestamp(extension.text)).replace(tzinfo=None) + if local_time is not None: + # We found a pair that we can use! + offset = local_time - time + # With all the time madness, luckily most time zones seem to stick + # to an offset that is a multiple of 15 minutes (see + # https://en.wikipedia.org/wiki/List_of_UTC_offsets). We try to + # round the value to the nearest of 15 minutes, to prevent any + # funky offsets from happening due to slight clock desyncs. + offset = round_timedelta_to_multiple(offset, datetime.timedelta(minutes=15)) + return datetime.timezone(offset) # If all else fails, we assume that we are UTC+00:00 return datetime.timezone.utc diff --git a/fietsboek/views/upload.py b/fietsboek/views/upload.py index 4f42b80..bbd8f68 100644 --- a/fietsboek/views/upload.py +++ b/fietsboek/views/upload.py @@ -104,7 +104,7 @@ def finish_upload(request): badges = [(False, badge) for badge in badges] gpx = gpxpy.parse(upload.gpx_data) timezone = util.guess_gpx_timezone(gpx) - date = gpx.time or gpx.get_time_bounds().start_time + date = gpx.time or gpx.get_time_bounds().start_time or datetime.datetime.now() date = date.astimezone(timezone) tz_offset = timezone.utcoffset(date) track_name = "" -- cgit v1.2.3