From 2f08ecabbfe737be5eeb0c57c0b26312fa80b78c Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Tue, 29 Jul 2025 09:28:37 +0900 Subject: [PATCH] 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 --- src/corelibs/string_handling/timestamp_strings.py | 9 ++++++++- test-run/string_handling/timestamp_strings.py | 7 +++++++ uv.lock | 2 +- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/corelibs/string_handling/timestamp_strings.py b/src/corelibs/string_handling/timestamp_strings.py index 97baa26..59ecb72 100644 --- a/src/corelibs/string_handling/timestamp_strings.py +++ b/src/corelibs/string_handling/timestamp_strings.py @@ -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 diff --git a/test-run/string_handling/timestamp_strings.py b/test-run/string_handling/timestamp_strings.py index 128d640..39d63c6 100644 --- a/test-run/string_handling/timestamp_strings.py +++ b/test-run/string_handling/timestamp_strings.py @@ -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: diff --git a/uv.lock b/uv.lock index de0427d..774011c 100644 --- a/uv.lock +++ b/uv.lock @@ -44,7 +44,7 @@ wheels = [ [[package]] name = "corelibs" -version = "0.19.1" +version = "0.21.0" source = { editable = "." } dependencies = [ { name = "jmespath" },