34 lines
982 B
Python
34 lines
982 B
Python
import time
|
|
import ntptime
|
|
import machine
|
|
|
|
def setup_rtc(tz_offset):
|
|
ntptime.settime()
|
|
rtc = machine.RTC()
|
|
|
|
utc = time.localtime()
|
|
local = time.localtime(time.mktime(utc) + tz_offset * 3600)
|
|
|
|
rtc.datetime((
|
|
local[0], local[1], local[2], 0,
|
|
local[3], local[4], local[5], 0
|
|
))
|
|
return rtc
|
|
|
|
# Function to calculate the difference
|
|
def calculate_difference(dt1, dt2):
|
|
timestamp1 = rtc_to_timestamp(dt1)
|
|
timestamp2 = rtc_to_timestamp(dt2)
|
|
difference = abs(timestamp2 - timestamp1)
|
|
days = difference // (24 * 3600)
|
|
difference %= (24 * 3600)
|
|
hours = difference // 3600
|
|
difference %= 3600
|
|
minutes = difference // 60
|
|
seconds = difference % 60
|
|
return days, hours, minutes, seconds
|
|
|
|
# Function to convert RTC datetime tuple to a timestamp
|
|
def rtc_to_timestamp(dt):
|
|
year, month, day, weekday, hour, minute, second, subseconds = dt
|
|
return time.mktime((year, month, day, hour, minute, second, 0, 0)) |