Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/core/utils/text.py
2023-12-09 21:42:46 +08:00

39 lines
1.2 KiB
Python

import sys
import datetime
from typing import TypeVar
T = TypeVar("T", str, bytes, bytearray)
if sys.version_info.minor > 8: # PY39
def remove_suffix(string: T, suffix: T) -> T:
return string.removesuffix(suffix)
def remove_prefix(string: T, prefix: T) -> T:
return string.removeprefix(prefix)
else:
def remove_suffix(string: T, suffix: T) -> T:
return string[:-len(suffix)] if string.endswith(suffix) else string
def remove_prefix(string: T, prefix: T) -> T:
return string[len(prefix):] if string.startswith(prefix) else string
def parse_time_string(time_str):
try:
negative = False
if time_str[0] == '+':
time_str = time_str[1:]
elif time_str[0] == '-':
negative = True
time_str = time_str[1:]
tstr_split = time_str.split(':')
hour = int(tstr_split[0])
minute = 0
if len(tstr_split) == 2:
minute = int(tstr_split[1])
if negative:
hour = -hour
minute = -minute
return datetime.timedelta(hours=hour, minutes=minute)
except ValueError:
return datetime.timedelta()