Skip to content

Commit

Permalink
Error in handling date text in autoevent
Browse files Browse the repository at this point in the history
  • Loading branch information
geirawsm committed Oct 3, 2023
1 parent 857d07a commit 2bd69c9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
4 changes: 3 additions & 1 deletion sausage_bot/cogs/autoevent.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ async def event_add(
tournament = scr['tournament']
stadium = scr['stadium']
_dt = scr['datetime']
start_text = _dt['start_dt'].strftime('%-d. %B, %H:%M')
start_text = _dt['start_dt'].format(
'd. MMMM, HH:mm', locale=datetime_handling.locale
)
rel_start = _dt['rel_start']
start_event = _dt['start_event']
end_dt = _dt['end_dt']
Expand Down
30 changes: 19 additions & 11 deletions sausage_bot/util/datetime_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def make_dt(date_in):
- 17.05.20 22, 11.22
- 2022-05-17T11:22:00Z
- 2023-08-05T10:00:00+02:00
- 1122
- 11.22
'''
if any(marker in str(date_in) for marker in ['Z', 'T', '+']):
log.debug('Found a Z/T/+ in `date_in`')
Expand All @@ -43,21 +45,27 @@ def make_dt(date_in):
log.log_more('`date_in` is {}'.format(date_in))
log.log_more(f'Got `d_len` {d_len}')
log.log_more(f'd_split: {d_split}')
if d_len == 3:
if d_len <= 2:
# Expecting `HHmm` or `HH( |:|-|_|.)mm`
date_in = date_in.replace(' ', '')
return pendulum.from_format(
date_in, 'HHmm'
).in_tz(tz)
elif d_len == 3:
# Expecting `DD MM YYYY`, `YYY MM DD` or `DD MM YY`
if len(d_split[2]) == 4:
return pendulum.from_format(
date_in, 'DD MM YYYY'
)
).in_tz(tz)
elif len(d_split[0]) == 4:
return pendulum.from_format(
date_in, 'YYYY MM DD'
)
).in_tz(tz)
elif all(len(timeunit) == 2 for timeunit in d_split):
# We have to assume that this is DD MM YY
return pendulum.from_format(
date_in, 'DD MM YY'
)
).in_tz(tz)
elif d_len == 4:
# Expecting a wrong space or separator somewhere
# If all units have 2 in len, then it could be a split in YYYY,
Expand All @@ -66,7 +74,7 @@ def make_dt(date_in):
date_in = f'{d[0]} {d[1]} {d[2]}{d[3]}'
return pendulum.from_format(
date_in, 'DD MM YYYY'
)
).in_tz(tz)
# If the fourth and last unit has a len of 4, it probably
# is the time with a missing separator
elif all(len(timeunit) == 2 for timeunit in d_split[0:2])\
Expand All @@ -85,32 +93,32 @@ def make_dt(date_in):
log.log_more('date_in: {}'.format(date_in))
return pendulum.from_format(
date_in, 'DD MM YYYY HH mm'
)
).in_tz(tz)
elif d_len == 5:
if len(d_split[2]) == 4:
return pendulum.from_format(
date_in, 'DD MM YYYY HH mm'
)
).in_tz(tz)
elif len(d_split[0]) == 4:
return pendulum.from_format(
date_in, 'YYYY MM DD HH mm'
)
).in_tz(tz)
elif len(d_split[2]) == 2:
return pendulum.from_format(
date_in, 'DD MM YY HH mm'
)
).in_tz(tz)
elif len(d_split[0]) == 2:
return pendulum.from_format(
date_in, 'YY MM DD HH mm'
)
).in_tz(tz)
elif d_len == 6:
# A split of 6 is most likely a split in YYYY
if all(len(timeunit) == 2 for timeunit in d_split):
d = d_split
date_in = f'{d[0]} {d[1]} {d[2]}{d[3]} {d[4]} {d[5]}'
return pendulum.from_format(
date_in, 'DD MM YYYY HH mm'
)
).in_tz(tz)
pass
else:
return None
Expand Down

0 comments on commit 2bd69c9

Please sign in to comment.