From 2f1b35b3fafaba80f69abff7ef946fc04613da9f Mon Sep 17 00:00:00 2001 From: gerard780 Date: Mon, 30 Dec 2019 14:01:59 -0500 Subject: [PATCH 1/2] added support for timezones with - offset introduced new regexp to better handle timestamps with a negative timezone offset like '2019-21-30T14:01:46.79231-05:00' --- pwnagotchi/plugins/default/webgpsmap.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/pwnagotchi/plugins/default/webgpsmap.py b/pwnagotchi/plugins/default/webgpsmap.py index 3d7ae4d..f09f131 100644 --- a/pwnagotchi/plugins/default/webgpsmap.py +++ b/pwnagotchi/plugins/default/webgpsmap.py @@ -308,12 +308,17 @@ class PositionFile: if date_iso_formated.endswith("Z"): date_iso_formated = date_iso_formated[:-1] + "+00:00" # bad microseconds fix: fill/cut microseconds to 6 numbers - part1, part2, part3 = re.split('\.|\+', date_iso_formated) + parts = re.split('^(([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[Tt]([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60))(\.[0-9]+)?(([Zz])|([\+|\-]([01][0-9]|2[0-3]):?[0-5][0-9]))$', date_iso_formated) + + part1 = parts[1] + part2 = parts[8][1:] + part3 = parts[11] + part2 = part2.ljust(6, '0')[:6] # timezone fix: 0200 >>> 02:00 - if len(part3) == 4: - part3 = part3[1:2].rjust(2, '0') + ':' + part3[3:4].rjust(2, '0') - date_iso_formated = part1 + "." + part2 + "+" + part3 + if len(part3) == 5: + part3 = part3[0:1] + part3[1:2].rjust(2, '0') + ':' + part3[3:4].rjust(2, '0') + date_iso_formated = part1 + "." + part2 + part3 dateObj = datetime.datetime.fromisoformat(date_iso_formated) return_ts = int("%.0f" % dateObj.timestamp()) else: From 78fba1f74b556fe71e691dec95c91ca78c4202f7 Mon Sep 17 00:00:00 2001 From: dadav <33197631+dadav@users.noreply.github.com> Date: Mon, 13 Jan 2020 20:10:43 +0100 Subject: [PATCH 2/2] dateutil is easier than regex --- pwnagotchi/plugins/default/webgpsmap.py | 21 +++------------------ requirements.txt | 1 + 2 files changed, 4 insertions(+), 18 deletions(-) diff --git a/pwnagotchi/plugins/default/webgpsmap.py b/pwnagotchi/plugins/default/webgpsmap.py index f09f131..2aeceae 100644 --- a/pwnagotchi/plugins/default/webgpsmap.py +++ b/pwnagotchi/plugins/default/webgpsmap.py @@ -6,6 +6,7 @@ import re import datetime from flask import Response from functools import lru_cache +from dateutil.parser import parse ''' 2do: @@ -17,7 +18,7 @@ from functools import lru_cache https://blogs.kent.ac.uk/websolutions/2015/01/29/filtering-map-markers-with-leaflet-js-a-brief-technical-overview/ http://www.digital-geography.com/filter-leaflet-maps-slider/ http://bl.ocks.org/zross/47760925fcb1643b4225 - - + - ''' class Webgpsmap(plugins.Plugin): @@ -303,23 +304,7 @@ class PositionFile: return_ts = self._json['ts'] elif 'Updated' in self._json: # convert gps datetime to unix timestamp: "2019-10-05T23:12:40.422996+01:00" - date_iso_formated = self._json['Updated'] - #fix timezone "Z": "2019-11-28T04:44:46.79231Z" >> "2019-11-28T04:44:46.79231+00:00" - if date_iso_formated.endswith("Z"): - date_iso_formated = date_iso_formated[:-1] + "+00:00" - # bad microseconds fix: fill/cut microseconds to 6 numbers - parts = re.split('^(([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[Tt]([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60))(\.[0-9]+)?(([Zz])|([\+|\-]([01][0-9]|2[0-3]):?[0-5][0-9]))$', date_iso_formated) - - part1 = parts[1] - part2 = parts[8][1:] - part3 = parts[11] - - part2 = part2.ljust(6, '0')[:6] - # timezone fix: 0200 >>> 02:00 - if len(part3) == 5: - part3 = part3[0:1] + part3[1:2].rjust(2, '0') + ':' + part3[3:4].rjust(2, '0') - date_iso_formated = part1 + "." + part2 + part3 - dateObj = datetime.datetime.fromisoformat(date_iso_formated) + dateObj = parse(self._json['Updated']) return_ts = int("%.0f" % dateObj.timestamp()) else: # use file timestamp last modification of the json file diff --git a/requirements.txt b/requirements.txt index 2b8eecf..4981923 100644 --- a/requirements.txt +++ b/requirements.txt @@ -18,3 +18,4 @@ gast==0.2.2 flask==1.0.2 flask-cors==3.0.7 flask-wtf==0.14.2 +python-dateutil==2.8.1