selfbot-legacy/utils/time_parser.py

21 lines
597 B
Python
Raw Permalink Normal View History

2025-05-07 16:44:50 +02:00
import re
2025-05-08 19:31:41 +02:00
# Define the time_regex that was missing
time_regex = re.compile(r'(\d+)([smhd])')
2025-05-07 16:44:50 +02:00
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