Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0bd7c1f685 | ||
|
|
2f08ecabbf |
@@ -1,7 +1,7 @@
|
|||||||
# MARK: Project info
|
# MARK: Project info
|
||||||
[project]
|
[project]
|
||||||
name = "corelibs"
|
name = "corelibs"
|
||||||
version = "0.21.0"
|
version = "0.21.1"
|
||||||
description = "Collection of utils for Python scripts"
|
description = "Collection of utils for Python scripts"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
requires-python = ">=3.13"
|
requires-python = ">=3.13"
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ Current timestamp strings and time zones
|
|||||||
import re
|
import re
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
|
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
|
||||||
|
from corelibs.var_handling.var_helpers import is_float
|
||||||
|
|
||||||
|
|
||||||
class TimeParseError(Exception):
|
class TimeParseError(Exception):
|
||||||
@@ -35,7 +36,7 @@ class TimestampStrings:
|
|||||||
self.timestamp_file = self.timestamp_now.strftime("%Y-%m-%d_%H%M%S")
|
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
|
Conver a string with time units into a seconds string
|
||||||
The following units are allowed
|
The following units are allowed
|
||||||
@@ -53,6 +54,12 @@ def convert_to_seconds(time_string: str) -> int:
|
|||||||
int -- _description_
|
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
|
# Define time unit conversion factors
|
||||||
unit_factors: dict[str, int] = {
|
unit_factors: dict[str, int] = {
|
||||||
'Y': 31536000, # 365 days * 86400 seconds/day
|
'Y': 31536000, # 365 days * 86400 seconds/day
|
||||||
|
|||||||
@@ -31,6 +31,13 @@ def main() -> None:
|
|||||||
"30m 45 minutes", # minutes appears twice
|
"30m 45 minutes", # minutes appears twice
|
||||||
"1Y 2 years", # years appears twice
|
"1Y 2 years", # years appears twice
|
||||||
"1x 2 yrs", # invalid names
|
"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:
|
for time_string in test_cases:
|
||||||
|
|||||||
Reference in New Issue
Block a user