Skip to content

Commit 98d505a

Browse files
authored
Merge pull request #76 from mrdalgaard/main
Enhanced parsing of timestamp_utc
2 parents b9e06e9 + ef300a2 commit 98d505a

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

adafruit_gps.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@
5050

5151
_SENTENCE_PARAMS = (
5252
# 0 - _GLL
53-
"dcdcfcC",
53+
"dcdcscC",
5454
# 1 - _RMC
55-
"fcdcdcffiDCC",
55+
"scdcdcffsDCC",
5656
# 2 - _GGA
57-
"fdcdciiffsfsIS",
57+
"sdcdciiffsfsIS",
5858
# 3 - _GSA
5959
"ciIIIIIIIIIIIIfff",
6060
# 4 - _GSA_4_11
@@ -68,7 +68,7 @@
6868
# 8 - _GSV19
6969
"iiiiiiIiiiIiiiIiiiI",
7070
# 9 - _RMC_4_1
71-
"fcdcdcffiDCCC",
71+
"scdcdcffsDCCC",
7272
)
7373

7474

@@ -394,9 +394,9 @@ def _parse_sentence(self):
394394
return (data_type, sentence[delimiter + 1 :])
395395

396396
def _update_timestamp_utc(self, time_utc, date=None):
397-
hours = time_utc // 10000
398-
mins = (time_utc // 100) % 100
399-
secs = time_utc % 100
397+
hours = int(time_utc[0:2])
398+
mins = int(time_utc[2:4])
399+
secs = int(time_utc[4:6])
400400
if date is None:
401401
if self.timestamp_utc is None:
402402
day, month, year = 0, 0, 0
@@ -405,9 +405,9 @@ def _update_timestamp_utc(self, time_utc, date=None):
405405
month = self.timestamp_utc.tm_mon
406406
year = self.timestamp_utc.tm_year
407407
else:
408-
day = date // 10000
409-
month = (date // 100) % 100
410-
year = 2000 + date % 100
408+
day = int(date[0:2])
409+
month = int(date[2:4])
410+
year = 2000 + int(date[4:6])
411411

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

431431
# UTC time of position
432-
self._update_timestamp_utc(int(data[4]))
432+
self._update_timestamp_utc(data[4])
433433

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

452452
# UTC time of position and date
453-
self._update_timestamp_utc(int(data[0]), data[8])
453+
self._update_timestamp_utc(data[0], data[8])
454454

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

496496
# UTC time of position
497-
self._update_timestamp_utc(int(data[0]))
497+
self._update_timestamp_utc(data[0])
498498

499499
# Latitude
500500
self.latitude = _read_degrees(data, 1, "s")

tests/adafruit_gps_test.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,14 @@ def test_GPS_update_timestamp_UTC_date_None():
140140
assert gps.datetime is None
141141
assert gps.timestamp_utc is None
142142
exp_struct = time.struct_time((0, 0, 0, 22, 14, 11, 0, 0, -1))
143-
gps._update_timestamp_utc(time_utc=221411)
143+
gps._update_timestamp_utc(time_utc="221411")
144144
assert gps.timestamp_utc == exp_struct
145145

146146

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

153153

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

163163

0 commit comments

Comments
 (0)