68 lines
1.6 KiB
Python
68 lines
1.6 KiB
Python
# 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()
|