diff --git a/adafruit_gps.py b/adafruit_gps.py index 57a087e..acee2b4 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,24 @@ 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 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 + 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]) + year = int(date[4:6]) - self.timestamp_utc = time.struct_time((year, month, day, hours, mins, secs, 0, 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: # 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..c328ea7 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..9c8eaa3 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,10 @@ 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}"