convert_to_seconds allow negative time strings and add pytests

This commit is contained in:
Clemens Schwaighofer
2025-09-24 15:25:53 +09:00
parent 7c72d99619
commit ef5981b473
6 changed files with 224 additions and 1 deletions

View File

@@ -11,6 +11,7 @@ from types import TracebackType
# _typeshed.OptExcInfo
OptExcInfo = Tuple[None, None, None] | Tuple[Type[BaseException], BaseException, TracebackType]
def call_stack(
start: int = 0,
skip_last: int = -1,

View File

@@ -22,6 +22,7 @@ def array_search(
"""depreacted, old call order"""
return find_in_array_from_list(data, search_params, return_index)
def find_in_array_from_list(
data: list[dict[str, Any]],
search_params: list[ArraySearchList],

View File

@@ -28,6 +28,7 @@ def default(obj: Any) -> str | None:
return obj.isoformat()
return None
def json_dumps(data: Any):
"""
wrapper for json.dumps with sure dump without throwing Exceptions

View File

@@ -60,6 +60,11 @@ def convert_to_seconds(time_string: str | int | float) -> int:
return int(round(float(time_string)))
time_string = str(time_string)
# Check if the time string is negative
negative = time_string.startswith('-')
if negative:
time_string = time_string[1:] # Remove the negative sign for processing
# Define time unit conversion factors
unit_factors: dict[str, int] = {
'Y': 31536000, # 365 days * 86400 seconds/day
@@ -109,7 +114,7 @@ def convert_to_seconds(time_string: str | int | float) -> int:
seen_units.append(unit)
return total_seconds
return -total_seconds if negative else total_seconds
def seconds_to_string(seconds: str | int | float, show_microseconds: bool = False) -> str: