templatetags: replace dateutil.parser by a explicit computation (#48919)
This commit is contained in:
parent
ff52f172e8
commit
854d3e73a1
|
@ -124,18 +124,25 @@ def get_time_table_from_specification(specification):
|
||||||
return (weekday, time_table)
|
return (weekday, time_table)
|
||||||
|
|
||||||
|
|
||||||
def openinghours_to_datetime(codename, hour, minute):
|
def openinghours_to_datetime(codename, hour, minute, base_datetime):
|
||||||
"""
|
"""
|
||||||
return the next date and time after now()
|
return the next date and time after base_datetime
|
||||||
"""
|
"""
|
||||||
|
day_number = EN_ABBREV_WEEKDAYS_LIST.index(codename)
|
||||||
|
|
||||||
|
# set next weekday
|
||||||
|
days = (7 + day_number - base_datetime.weekday()) % 7
|
||||||
|
datetime_obj = base_datetime + datetime.timedelta(days=days)
|
||||||
|
if is_naive(datetime_obj):
|
||||||
|
datetime_obj = make_aware(datetime_obj)
|
||||||
|
|
||||||
|
# set time
|
||||||
try:
|
try:
|
||||||
weekday = EN_ABBREV_WEEKDAYS.get(codename, None)
|
time = datetime.time(hour=hour, minute=minute)
|
||||||
datetime_obj = dateutil_parse('%s %d:%d:00' % (weekday, hour, minute))
|
|
||||||
if is_naive(datetime_obj):
|
|
||||||
datetime_obj = make_aware(datetime_obj)
|
|
||||||
return datetime_obj
|
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return None
|
return None
|
||||||
|
datetime_obj = datetime.datetime.combine(datetime_obj, time)
|
||||||
|
return datetime_obj
|
||||||
|
|
||||||
|
|
||||||
def get_period_from_data(time_table):
|
def get_period_from_data(time_table):
|
||||||
|
@ -267,23 +274,23 @@ def get_slots_from_mairie_format(data, base_datetime):
|
||||||
for days_list, time_table in parse_opening_hours_data(data):
|
for days_list, time_table in parse_opening_hours_data(data):
|
||||||
for weekday in days_list:
|
for weekday in days_list:
|
||||||
start = openinghours_to_datetime(
|
start = openinghours_to_datetime(
|
||||||
weekday, int(time_table['start_hour']), int(time_table['start_minute']))
|
weekday, int(time_table['start_hour']), int(time_table['start_minute']), base_datetime)
|
||||||
end = openinghours_to_datetime(
|
end = openinghours_to_datetime(
|
||||||
weekday, int(time_table['end_hour']), int(time_table['end_minute']))
|
weekday, int(time_table['end_hour']), int(time_table['end_minute']), base_datetime)
|
||||||
|
|
||||||
# hours may belongs on next day
|
# hours may belongs on next day
|
||||||
if end < start:
|
if end < start:
|
||||||
day_number = EN_ABBREV_WEEKDAYS_LIST.index(weekday)
|
day_number = EN_ABBREV_WEEKDAYS_LIST.index(weekday)
|
||||||
tomorrow = EN_ABBREV_WEEKDAYS_LIST[(day_number + 1) % 7]
|
tomorrow = EN_ABBREV_WEEKDAYS_LIST[(day_number + 1) % 7]
|
||||||
end = openinghours_to_datetime(
|
end = openinghours_to_datetime(
|
||||||
tomorrow, int(time_table['end_hour']), int(time_table['end_minute']))
|
tomorrow, int(time_table['end_hour']), int(time_table['end_minute']), base_datetime)
|
||||||
if end < start:
|
if end < start:
|
||||||
# end time may be find this week whereas start time is picked on next week,
|
# end time may be find this week whereas start time is picked on next week,
|
||||||
# this occure if we are now past 24:00, on next day.
|
# this occure if we are now past 24:00, on next day.
|
||||||
# openinghours_to_datetime (return the next date and time after now())
|
# openinghours_to_datetime (return the next date and time after now())
|
||||||
# can't return a coherent start time.
|
# can't return a coherent start time.
|
||||||
# in this case we thrink the slot to the next day part.
|
# in this case we thrink the slot to the next day part.
|
||||||
start = openinghours_to_datetime(tomorrow, 0, 0)
|
start = openinghours_to_datetime(tomorrow, 0, 0, base_datetime)
|
||||||
|
|
||||||
timeslot = TimeSlot(start, end)
|
timeslot = TimeSlot(start, end)
|
||||||
# add to slots the opening hours in chronological order beginning from today
|
# add to slots the opening hours in chronological order beginning from today
|
||||||
|
|
Loading…
Reference in New Issue