This commit is contained in:
PaulVua
2025-01-30 11:58:59 +01:00
parent 6e0dc1b257
commit 970f62658b

View File

@@ -38,16 +38,21 @@ def main():
bus = smbus2.SMBus(1) bus = smbus2.SMBus(1)
# Try to read RTC time # Try to read RTC time
rtc_time = read_time(bus) rtc_time = read_time(bus)
# If RTC is not connected, set default message
if rtc_time is None:
rtc_time_str = "not connected"
else:
rtc_time_str = rtc_time.strftime('%Y-%m-%d %H:%M:%S')
# Get current system time # Get current system time
system_time = datetime.now() #local system_time = datetime.now() #local
utc_time = datetime.utcnow() #UTC utc_time = datetime.utcnow() #UTC
# If RTC is not connected, set default message
# Calculate time difference (in seconds) if RTC is connected
if rtc_time:
rtc_time_str = rtc_time.strftime('%Y-%m-%d %H:%M:%S')
time_difference = int((system_utc_time - rtc_time).total_seconds()) # Convert to int
else:
rtc_time_str = "not connected"
time_difference = "N/A" # Not applicable
# Print both times # Print both times
#print(f"RTC module Time: {rtc_time.strftime('%Y-%m-%d %H:%M:%S')}") #print(f"RTC module Time: {rtc_time.strftime('%Y-%m-%d %H:%M:%S')}")
#print(f"Sys local Time: {system_time.strftime('%Y-%m-%d %H:%M:%S')}") #print(f"Sys local Time: {system_time.strftime('%Y-%m-%d %H:%M:%S')}")
@@ -57,7 +62,8 @@ def main():
time_data = { time_data = {
"rtc_module_time":rtc_time_str, "rtc_module_time":rtc_time_str,
"system_local_time": system_time.strftime('%Y-%m-%d %H:%M:%S'), "system_local_time": system_time.strftime('%Y-%m-%d %H:%M:%S'),
"system_utc_time": utc_time.strftime('%Y-%m-%d %H:%M:%S') "system_utc_time": utc_time.strftime('%Y-%m-%d %H:%M:%S'),
"time_difference_seconds": time_difference
} }
print(json.dumps(time_data, indent=4)) print(json.dumps(time_data, indent=4))