modernise conversion date

This commit is contained in:
Emmanuel Viennet 2022-10-03 11:59:59 +02:00
parent 84b02edd48
commit 8d72229e8b
2 changed files with 9 additions and 37 deletions

View File

@ -459,49 +459,22 @@ def dictfilter(d, fields, filter_nulls=True):
# --- Misc Tools
def DateDMYtoISO(dmy, null_is_empty=False):
def DateDMYtoISO(dmy: str, null_is_empty=False) -> str:
"convert date string from french format to ISO"
if not dmy:
if null_is_empty:
return ""
else:
return None
if not isinstance(dmy, str):
if hasattr(dmy, "strftime"):
return dmy.strftime("%Y-%m-%d")
t = dmy.split("/")
if len(t) != 3:
raise ScoValueError('Format de date (j/m/a) invalide: "%s"' % str(dmy))
day, month, year = t
year = int(year)
month = int(month)
day = int(day)
# accept years YYYY or YY, uses 1970 as pivot
if year < 100:
if year > 70:
year += 1900
else:
year += 2000
if month < 1 or month > 12:
raise ScoValueError("mois de la date invalide ! (%s)" % month)
# compute nb of day in month:
mo = month
if mo > 7:
mo = mo + 1
if mo % 2:
MonthNbDays = 31
elif mo == 2:
if year % 4 == 0 and ((year % 100 != 0) or (year % 400 == 0)):
MonthNbDays = 29 # leap
else:
MonthNbDays = 28
else:
MonthNbDays = 30
if day < 1 or day > MonthNbDays:
raise ScoValueError("jour de la date invalide ! (%s)" % day)
return "%04d-%02d-%02d" % (year, month, day)
if not isinstance(dmy, str):
raise ScoValueError(f'Date (j/m/a) invalide: "{dmy}"')
try:
dt = datetime.datetime.strptime(dmy, "%d/%m/%Y")
except ValueError as exc:
raise ScoValueError(f'Date (j/m/a) invalide: "{dmy}"') from exc
return dt.date().isoformat()
def DateISOtoDMY(isodate):

View File

@ -1991,7 +1991,6 @@ def formsemestre_bulletins_mailetuds(
"""
prefer_mail_perso = int(prefer_mail_perso)
formsemestre = FormSemestre.query.get_or_404(formsemestre_id)
nt: NotesTableCompat = res_sem.load_formsemestre_results(formsemestre)
inscriptions = [
inscription
for inscription in formsemestre.inscriptions