Skip to content

Enhanced parsing of timestamp_utc #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions adafruit_gps.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@

_SENTENCE_PARAMS = (
# 0 - _GLL
"dcdcfcC",
"dcdcscC",
# 1 - _RMC
"fcdcdcffiDCC",
"scdcdcffsDCC",
# 2 - _GGA
"fdcdciiffsfsIS",
"sdcdciiffsfsIS",
# 3 - _GSA
"ciIIIIIIIIIIIIfff",
# 4 - _GSA_4_11
Expand All @@ -68,7 +68,7 @@
# 8 - _GSV19
"iiiiiiIiiiIiiiIiiiI",
# 9 - _RMC_4_1
"fcdcdcffiDCCC",
"scdcdcffsDCCC",
)


Expand Down Expand Up @@ -394,9 +394,9 @@ def _parse_sentence(self):
return (data_type, sentence[delimiter + 1 :])

def _update_timestamp_utc(self, time_utc, date=None):
hours = time_utc // 10000
mins = (time_utc // 100) % 100
secs = time_utc % 100
hours = int(time_utc[0:2])
mins = int(time_utc[2:4])
secs = int(time_utc[4:6])
if date is None:
if self.timestamp_utc is None:
day, month, year = 0, 0, 0
Expand All @@ -405,9 +405,9 @@ def _update_timestamp_utc(self, time_utc, date=None):
month = self.timestamp_utc.tm_mon
year = self.timestamp_utc.tm_year
else:
day = date // 10000
month = (date // 100) % 100
year = 2000 + date % 100
day = int(date[0:2])
month = int(date[2:4])
year = 2000 + int(date[4:6])

self.timestamp_utc = time.struct_time(
(year, month, day, hours, mins, secs, 0, 0, -1)
Expand All @@ -429,7 +429,7 @@ def _parse_gll(self, data):
self.longitude = _read_degrees(data, 2, "w")

# UTC time of position
self._update_timestamp_utc(int(data[4]))
self._update_timestamp_utc(data[4])

# Status Valid(A) or Invalid(V)
self.isactivedata = data[5]
Expand All @@ -450,7 +450,7 @@ def _parse_rmc(self, data):
return False # Params didn't parse

# UTC time of position and date
self._update_timestamp_utc(int(data[0]), data[8])
self._update_timestamp_utc(data[0], data[8])

# Status Valid(A) or Invalid(V)
self.isactivedata = data[1]
Expand Down Expand Up @@ -494,7 +494,7 @@ def _parse_gga(self, data):
return False # Params didn't parse

# UTC time of position
self._update_timestamp_utc(int(data[0]))
self._update_timestamp_utc(data[0])

# Latitude
self.latitude = _read_degrees(data, 1, "s")
Expand Down
6 changes: 3 additions & 3 deletions tests/adafruit_gps_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,14 @@ def test_GPS_update_timestamp_UTC_date_None():
assert gps.datetime is None
assert gps.timestamp_utc is None
exp_struct = time.struct_time((0, 0, 0, 22, 14, 11, 0, 0, -1))
gps._update_timestamp_utc(time_utc=221411)
gps._update_timestamp_utc(time_utc="221411")
assert gps.timestamp_utc == exp_struct


def test_GPS_update_timestamp_UTC_date_not_None():
gps = GPS(uart=UartMock())
exp_struct = time.struct_time((2021, 10, 2, 22, 14, 11, 0, 0, -1))
gps._update_timestamp_utc(time_utc=221411, date=21021)
gps._update_timestamp_utc(time_utc="221411", date="021021")
assert gps.timestamp_utc == exp_struct


Expand All @@ -157,7 +157,7 @@ def test_GPS_update_timestamp_timestamp_utc_was_not_none_new_date_none():
gps.timestamp_utc = time.struct_time((2021, 10, 2, 22, 10, 11, 0, 0, -1))
exp_struct = time.struct_time((2021, 10, 2, 22, 14, 11, 0, 0, -1))
# update the timestamp
gps._update_timestamp_utc(time_utc=221411)
gps._update_timestamp_utc(time_utc="221411")
assert gps.timestamp_utc == exp_struct


Expand Down