From 1a842267ede8dadc8c5ebcb8e6795e9a86164108 Mon Sep 17 00:00:00 2001 From: David Kim <46164193+daehwankim112@users.noreply.github.com> Date: Mon, 20 Oct 2025 14:39:41 -0700 Subject: [PATCH 1/4] chore: use datetime instead of time struct to include milliseconds information in gps.timestamp_utc --- adafruit_gps.py | 15 +++++++++------ examples/gps_displayio_simpletest.py | 2 +- examples/gps_simpletest.py | 15 ++++++++------- examples/gps_time_source.py | 7 ++++--- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/adafruit_gps.py b/adafruit_gps.py index 57a087e..93e9eda 100644 --- a/adafruit_gps.py +++ b/adafruit_gps.py @@ -28,6 +28,7 @@ """ import time +from datetime import datetime from micropython import const @@ -412,8 +413,8 @@ def has_3d_fix(self) -> bool: return self.fix_quality_3d is not None and self.fix_quality_3d >= 2 @property - def datetime(self) -> Optional[time.struct_time]: - """Return struct_time object to feed rtc.set_time_source() function""" + def datetime(self) -> Optional[datetime]: + """Return datetime object to feed rtc.set_time_source() function""" return self.timestamp_utc @property @@ -494,19 +495,21 @@ def _update_timestamp_utc(self, time_utc: str, date: Optional[str] = None) -> No hours = int(time_utc[0:2]) mins = int(time_utc[2:4]) secs = int(time_utc[4:6]) + milisecs = int(time_utc[7:10]) if date is None: if self.timestamp_utc is None: day, month, year = 0, 0, 0 + return else: - day = self.timestamp_utc.tm_mday - month = self.timestamp_utc.tm_mon - year = self.timestamp_utc.tm_year + day = self.timestamp_utc.day + month = self.timestamp_utc.month + year = self.timestamp_utc.year - 2000 else: 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)) + self.timestamp_utc = time.struct_time((year, month, day, hours, mins, secs, milisecs, 0, -1)) def _parse_vtg(self, data: List[str]) -> bool: # VTG - Course Over Ground and Ground Speed diff --git a/examples/gps_displayio_simpletest.py b/examples/gps_displayio_simpletest.py index 984655d..78c2024 100644 --- a/examples/gps_displayio_simpletest.py +++ b/examples/gps_displayio_simpletest.py @@ -66,6 +66,6 @@ # Update the label.text property to change the text on the display display_output_label.text = f"Timestamp (UTC): \ - \n{t.tm_mday}/{t.tm_mon}/{t.tm_year} {t.tm_hour}:{t.tm_min:02}:{t.tm_sec:02}\ + \n{t.day}/{t.month}/{t.year} {t.hour}:{t.minute:02}:{t.second:02}\ \nLat: {gps.latitude:.6f}\ \nLong: {gps.longitude:.6f}" diff --git a/examples/gps_simpletest.py b/examples/gps_simpletest.py index 49b3b1d..82895b6 100644 --- a/examples/gps_simpletest.py +++ b/examples/gps_simpletest.py @@ -77,13 +77,14 @@ # Print out details about the fix like location, date, etc. print("=" * 40) # Print a separator line. print( - "Fix timestamp: {}/{}/{} {:02}:{:02}:{:02}".format( # noqa: UP032 - gps.timestamp_utc.tm_mon, # Grab parts of the time from the - gps.timestamp_utc.tm_mday, # struct_time object that holds - gps.timestamp_utc.tm_year, # the fix time. Note you might - gps.timestamp_utc.tm_hour, # not get all data like year, day, - gps.timestamp_utc.tm_min, # month! - gps.timestamp_utc.tm_sec, + "Fix timestamp: {}/{}/{} {:02}:{:02}:{:02}.{06}".format( # noqa: UP032 + gps.timestamp_utc.month, # Grab parts of the time from the + gps.timestamp_utc.day, # struct_time object that holds + gps.timestamp_utc.year, # the fix time. Note you might + gps.timestamp_utc.hour, # not get all data like year, day, + gps.timestamp_utc.minute, # month! + gps.timestamp_utc.second, + gps.timestamp_utc.microsecond ) ) print(f"Latitude: {gps.latitude:.6f} degrees") diff --git a/examples/gps_time_source.py b/examples/gps_time_source.py index bd3742b..813661a 100644 --- a/examples/gps_time_source.py +++ b/examples/gps_time_source.py @@ -6,6 +6,7 @@ # time while there is powersource (ie coin cell battery) import time +from datetime import datetime import board import busio @@ -20,7 +21,7 @@ # gps = adafruit_gps.GPS_GtopI2C(i2c, debug=False) # Use I2C interface gps.send_command(b"PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0") -gps.send_command(b"PMTK220,1000") +gps.send_command(b"PMTK220,100") print("Set GPS as time source") rtc.set_time_source(gps) @@ -28,8 +29,8 @@ def _format_datetime(datetime): - date_part = f"{datetime.tm_mon:02}/{datetime.tm_mday:02}/{datetime.tm_year}" - time_part = f"{datetime.tm_hour:02}:{datetime.tm_min:02}:{datetime.tm_sec:02}" + date_part = f"{datetime.month:02}/{datetime.day:02}/{datetime.year}" + time_part = f"{datetime.hour:02}:{datetime.minute:02}:{datetime.second:02}.{datetime.microsecond:06}" return f"{date_part} {time_part}" From 3969844825b6cdb19405ce1eed832d8c1c41cf9e Mon Sep 17 00:00:00 2001 From: David Kim <46164193+daehwankim112@users.noreply.github.com> Date: Mon, 20 Oct 2025 15:15:25 -0700 Subject: [PATCH 2/4] chore: run pre commits --- adafruit_gps.py | 4 +++- examples/gps_simpletest.py | 2 +- examples/gps_time_source.py | 4 +++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/adafruit_gps.py b/adafruit_gps.py index 93e9eda..dbe9325 100644 --- a/adafruit_gps.py +++ b/adafruit_gps.py @@ -509,7 +509,9 @@ def _update_timestamp_utc(self, time_utc: str, date: Optional[str] = None) -> No month = int(date[2:4]) year = 2000 + int(date[4:6]) - self.timestamp_utc = time.struct_time((year, month, day, hours, mins, secs, milisecs, 0, -1)) + self.timestamp_utc = time.struct_time( + (year, month, day, hours, mins, secs, milisecs, 0, -1) + ) def _parse_vtg(self, data: List[str]) -> bool: # VTG - Course Over Ground and Ground Speed diff --git a/examples/gps_simpletest.py b/examples/gps_simpletest.py index 82895b6..c328ea7 100644 --- a/examples/gps_simpletest.py +++ b/examples/gps_simpletest.py @@ -84,7 +84,7 @@ gps.timestamp_utc.hour, # not get all data like year, day, gps.timestamp_utc.minute, # month! gps.timestamp_utc.second, - gps.timestamp_utc.microsecond + gps.timestamp_utc.microsecond, ) ) print(f"Latitude: {gps.latitude:.6f} degrees") diff --git a/examples/gps_time_source.py b/examples/gps_time_source.py index 813661a..9c8eaa3 100644 --- a/examples/gps_time_source.py +++ b/examples/gps_time_source.py @@ -30,7 +30,9 @@ def _format_datetime(datetime): date_part = f"{datetime.month:02}/{datetime.day:02}/{datetime.year}" - time_part = f"{datetime.hour:02}:{datetime.minute:02}:{datetime.second:02}.{datetime.microsecond:06}" + time_part = ( + f"{datetime.hour:02}:{datetime.minute:02}:{datetime.second:02}.{datetime.microsecond:06}" + ) return f"{date_part} {time_part}" From 378e74fabe03ee6fc96cc93e42300db177c5e8ac Mon Sep 17 00:00:00 2001 From: David Kim <46164193+daehwankim112@users.noreply.github.com> Date: Mon, 20 Oct 2025 15:22:44 -0700 Subject: [PATCH 3/4] fix: check if the milliseconds is empty --- adafruit_gps.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/adafruit_gps.py b/adafruit_gps.py index dbe9325..226896e 100644 --- a/adafruit_gps.py +++ b/adafruit_gps.py @@ -495,7 +495,7 @@ def _update_timestamp_utc(self, time_utc: str, date: Optional[str] = None) -> No hours = int(time_utc[0:2]) mins = int(time_utc[2:4]) secs = int(time_utc[4:6]) - milisecs = int(time_utc[7:10]) + milisecs = int(time_utc[7:10]) if len(time_utc) >= 10 and time_utc[7:10].isdigit() else 0 if date is None: if self.timestamp_utc is None: day, month, year = 0, 0, 0 @@ -509,8 +509,9 @@ def _update_timestamp_utc(self, time_utc: str, date: Optional[str] = None) -> No month = int(date[2:4]) year = 2000 + int(date[4:6]) - self.timestamp_utc = time.struct_time( - (year, month, day, hours, mins, secs, milisecs, 0, -1) + self.timestamp_utc = datetime.strptime( + str("%d/%d/%d %d:%d:%d.%d" % (day, month, year, hours, mins, secs, milisecs)), + "%d/%m/%y %H:%M:%S.%f", ) def _parse_vtg(self, data: List[str]) -> bool: From a3b8d9826bfd3b6820805e0e0d102a26e9f195fe Mon Sep 17 00:00:00 2001 From: David Kim <46164193+daehwankim112@users.noreply.github.com> Date: Mon, 20 Oct 2025 15:40:05 -0700 Subject: [PATCH 4/4] fix: remove 2000 to fit into UTC time --- adafruit_gps.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_gps.py b/adafruit_gps.py index 226896e..acee2b4 100644 --- a/adafruit_gps.py +++ b/adafruit_gps.py @@ -507,7 +507,7 @@ def _update_timestamp_utc(self, time_utc: str, date: Optional[str] = None) -> No else: day = int(date[0:2]) month = int(date[2:4]) - year = 2000 + int(date[4:6]) + year = int(date[4:6]) self.timestamp_utc = datetime.strptime( str("%d/%d/%d %d:%d:%d.%d" % (day, month, year, hours, mins, secs, milisecs)),