20 lines
585 B
Python
20 lines
585 B
Python
import re
|
|
|
|
time_regex = re.compile(r'(\d+)([smhd])') # Matches 4m2s, 1h30m, etc.
|
|
|
|
def parse_time(time_str):
|
|
"""
|
|
Parse time strings like "4m2s", "1h30m" into seconds.
|
|
|
|
Args:
|
|
time_str: String in format like "4m2s", "1h30m"
|
|
|
|
Returns:
|
|
Integer of total seconds or None if invalid
|
|
"""
|
|
units = {'s': 1, 'm': 60, 'h': 3600, 'd': 86400}
|
|
try:
|
|
total_seconds = sum(int(amount) * units[unit] for amount, unit in time_regex.findall(time_str))
|
|
return total_seconds if total_seconds > 0 else None
|
|
except:
|
|
return None |