From 970f62658bc129b2af86f5edd027738fe8b44c19 Mon Sep 17 00:00:00 2001 From: PaulVua Date: Thu, 30 Jan 2025 11:58:59 +0100 Subject: [PATCH] update --- RTC/read.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/RTC/read.py b/RTC/read.py index 601ccaa..4612223 100644 --- a/RTC/read.py +++ b/RTC/read.py @@ -38,16 +38,21 @@ def main(): bus = smbus2.SMBus(1) # Try to read RTC time 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 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(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')}") @@ -57,7 +62,8 @@ def main(): time_data = { "rtc_module_time":rtc_time_str, "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))