update
This commit is contained in:
22
RTC/read.py
22
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))
|
||||
|
||||
Reference in New Issue
Block a user