dont even know if it works lol didnt even test it
This commit is contained in:
commit
5a9c6a45ed
15 changed files with 905 additions and 0 deletions
0
utils/__init__.py
Normal file
0
utils/__init__.py
Normal file
40
utils/time_parser.py
Normal file
40
utils/time_parser.py
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import re
|
||||
|
||||
time_regex = re.compile(r'(\d+)\s*([smhd]|seconds?|minutes?|hours?|days?)')
|
||||
|
||||
|
||||
def parse_time(time_str):
|
||||
"""
|
||||
Parse time strings like "4m2s", "1h30m" into seconds.
|
||||
|
||||
Args:
|
||||
time_str: String in format like "4m2s", "1h30m", "15m"
|
||||
|
||||
Returns:
|
||||
Integer of total seconds or None if invalid
|
||||
"""
|
||||
units = {
|
||||
'second': 1, 'seconds': 1, 's': 1,
|
||||
'minute': 60, 'minutes': 60, 'm': 60,
|
||||
'hour': 3600, 'hours': 3600, 'h': 3600,
|
||||
'day': 86400, 'days': 86400, 'd': 86400,
|
||||
'week': 604800, 'weeks': 604800, 'w': 604800
|
||||
}
|
||||
|
||||
try:
|
||||
matches = time_regex.findall(time_str)
|
||||
if not matches:
|
||||
return None
|
||||
|
||||
total_seconds = 0
|
||||
for amount, unit in matches:
|
||||
if unit not in units and len(unit) > 0:
|
||||
unit = unit[0]
|
||||
|
||||
multiplier = units.get(unit.lower(), 1)
|
||||
total_seconds += int(amount) * multiplier
|
||||
|
||||
return total_seconds if total_seconds > 0 else None
|
||||
except Exception as e:
|
||||
print(f"Time parsing error: {e}")
|
||||
return None
|
||||
Loading…
Add table
Add a link
Reference in a new issue