FEAT : Gestion heure Paris
This commit is contained in:
parent
b2d458f637
commit
4fb489036f
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@ -10,6 +10,7 @@
|
||||
"~/.micropico-stubs/included"
|
||||
],
|
||||
"python.analysis.extraPaths": [
|
||||
"~/.micropico-stubs/included"
|
||||
"~/.micropico-stubs/included",
|
||||
"./lib"
|
||||
]
|
||||
}
|
||||
67
lib/localtime_fr.py
Normal file
67
lib/localtime_fr.py
Normal file
@ -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()
|
||||
11
main.py
11
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:
|
||||
|
||||
Loading…
Reference in New Issue
Block a user