diff --git a/.vscode/settings.json b/.vscode/settings.json index 03be782..7f3849e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -10,6 +10,7 @@ "~/.micropico-stubs/included" ], "python.analysis.extraPaths": [ - "~/.micropico-stubs/included" + "~/.micropico-stubs/included", + "./lib" ] } \ No newline at end of file diff --git a/lib/localtime_fr.py b/lib/localtime_fr.py new file mode 100644 index 0000000..6caeb8c --- /dev/null +++ b/lib/localtime_fr.py @@ -0,0 +1,67 @@ +# localtime_fr.py +import time +import ntptime + +# Fuseaux Europe/Paris +TZ_STD = 1 # UTC+1 en hiver +TZ_DST = 2 # UTC+2 en été + +# Trouve le dernier dimanche d'un mois +def last_sunday(year, month): + # On cherche depuis le dernier jour du mois + for day in range(31, 0, -1): + try: + wd = time.localtime(time.mktime((year, month, day, 0, 0, 0, 0, 0)))[6] + if wd == 6: # 6 = dimanche + return day + except: + pass + return None + +# Détecte si on est en heure d'été (DST) +def is_dst_europe(tm): + year, month, mday, hour = tm[0], tm[1], tm[2], tm[3] + + # Hors périodes + if month < 3 or month > 10: + return False + if month > 3 and month < 10: + return True + + # Mois de transition : Mars + if month == 3: + ls = last_sunday(year, 3) + return (mday > ls) or (mday == ls and hour >= 2) + + # Mois de transition : Octobre + if month == 10: + ls = last_sunday(year, 10) + return not ((mday > ls) or (mday == ls and hour >= 3)) + + return False + +# Fonction principale +def localtime(): + """ + Retourne la date locale Europe/Paris (UTC+1/UTC+2) + après application de l'heure d'été/hiver. + """ + t = list(time.localtime()) # UTC + + if is_dst_europe(t): + t[3] += TZ_DST + else: + t[3] += TZ_STD + + # Normalisation + return time.localtime(time.mktime(tuple(t))) + +# Wrapper simple pour datetime-like +def now(): + y, m, d, hh, mm, ss, wd, yd = localtime() + return f"{y:04d}-{m:02d}-{d:02d} {hh:02d}:{mm:02d}:{ss:02d}" + +# Sync NTP (facultatif) +def sync(): + ntptime.host = "fr.pool.ntp.org" + ntptime.settime() diff --git a/main.py b/main.py index ee6c3bf..97c703a 100644 --- a/main.py +++ b/main.py @@ -3,6 +3,7 @@ import network import time import ntptime import urequests +import localtime_fr led_jaune = Pin(0, Pin.OUT) led_bleue = Pin(2, Pin.OUT) @@ -55,8 +56,8 @@ def set_relays(mask): """mask = bits 0..3 (1=ON, 0=OFF)""" i2c.writeto_mem(I2C_ADDR, 0x10, bytes([mask])) -def HC() -> bool: - heure, minute = rtc.datetime()[4], rtc.datetime()[5] +def HC(date_heure) -> bool: + heure, minute = date_heure[4], date_heure[5] nb_minutes = heure * 60 + minute _21h30 = 21 * 60 + 30 # TODO : Ne pas faire le calcul à chaque appel _23h30 = 23 * 60 + 30 @@ -68,12 +69,12 @@ def ntp(): global ntp_ok, ntp_ttl, ntp_attente if (wifi.isconnected() and (not ntp_ok or ntp_attente < 0)): print("WIFI OK, call NTP") - ntptime.settime() # TODO : gérer TZ et h été/hivers + ntptime.settime() ntp_ok = True ntp_attente = ntp_ttl if (ntp_ok): - date_heure = time.localtime() - print(f"Date et heure : {date_heure[3]}:{date_heure[4]}:{date_heure[5]} ({ntp_attente}, {HC()})") + date_heure = localtime_fr.localtime() + print(f"Date et heure : {date_heure[3]}:{date_heure[4]}:{date_heure[5]} ({ntp_attente}, {HC(date_heure)})") ntp_attente = ntp_attente - pas_temps def charge() -> int: