For convert time string, skip convert if incoming value is a number of any type

Any float number will be rounded, and everything that is any kind of number will be then converted to int and returned
The rest will be converted to string and normal convert is run
This commit is contained in:
Clemens Schwaighofer
2025-07-29 09:28:37 +09:00
parent 12af1c80dc
commit 2f08ecabbf
3 changed files with 16 additions and 2 deletions

View File

@@ -5,6 +5,7 @@ Current timestamp strings and time zones
import re
from datetime import datetime
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
from corelibs.var_handling.var_helpers import is_float
class TimeParseError(Exception):
@@ -35,7 +36,7 @@ class TimestampStrings:
self.timestamp_file = self.timestamp_now.strftime("%Y-%m-%d_%H%M%S")
def convert_to_seconds(time_string: str) -> int:
def convert_to_seconds(time_string: str | int | float) -> int:
"""
Conver a string with time units into a seconds string
The following units are allowed
@@ -53,6 +54,12 @@ def convert_to_seconds(time_string: str) -> int:
int -- _description_
"""
# skip out if this is a number of any type
# numbers will br made float, rounded and then converted to int
if is_float(time_string):
return int(round(float(time_string)))
time_string = str(time_string)
# Define time unit conversion factors
unit_factors: dict[str, int] = {
'Y': 31536000, # 365 days * 86400 seconds/day

View File

@@ -31,6 +31,13 @@ def main() -> None:
"30m 45 minutes", # minutes appears twice
"1Y 2 years", # years appears twice
"1x 2 yrs", # invalid names
123, # int
789.12, # float
456.56, # float, high
"4566", # int as string
"5551.12", # float as string
"5551.56", # float, high as string
]
for time_string in test_cases:

2
uv.lock generated
View File

@@ -44,7 +44,7 @@ wheels = [
[[package]]
name = "corelibs"
version = "0.19.1"
version = "0.21.0"
source = { editable = "." }
dependencies = [
{ name = "jmespath" },