diff --git a/src/corelibs/logging_handling/log.py b/src/corelibs/logging_handling/log.py index 854c721..9710ed9 100644 --- a/src/corelibs/logging_handling/log.py +++ b/src/corelibs/logging_handling/log.py @@ -598,7 +598,7 @@ class Log(LogParent): # log path, remove them stem (".log"), then add the datetime and add .log again now = datetime.now() # we add microseconds part to get milli seconds - new_stem=f"{log_path.stem}.{now.strftime('%Y-%m-%d_%H-%M-%S')}.{str(now.microsecond)[:3]}" + new_stem = f"{log_path.stem}.{now.strftime('%Y-%m-%d_%H-%M-%S')}.{str(now.microsecond)[:3]}" file_handler = logging.FileHandler( filename=log_path.with_name(f"{new_stem}{log_path.suffix}"), encoding="utf-8", diff --git a/src/corelibs/string_handling/datetime_helpers.py b/src/corelibs/string_handling/datetime_helpers.py index 8119c32..1694cd0 100644 --- a/src/corelibs/string_handling/datetime_helpers.py +++ b/src/corelibs/string_handling/datetime_helpers.py @@ -3,7 +3,9 @@ Various string based date/time helpers """ from math import floor -import time +import time as time_t +from datetime import datetime +from zoneinfo import ZoneInfo, ZoneInfoNotFoundError def convert_timestamp(timestamp: float | int, show_micro: bool = True) -> str: @@ -58,6 +60,77 @@ def create_time(timestamp: float, timestamp_format: str = "%Y-%m-%d %H:%M:%S") - Returns: str -- _description_ """ - return time.strftime(timestamp_format, time.localtime(timestamp)) + return time_t.strftime(timestamp_format, time_t.localtime(timestamp)) + + +def get_system_timezone(): + """Get system timezone using datetime's automatic detection""" + # Get current time with system timezone + local_time = datetime.now().astimezone() + + # Extract timezone info + system_tz = local_time.tzinfo + timezone_name = str(system_tz) + + return system_tz, timezone_name + + +def parse_timezone_data(timezone_tz: str = '') -> ZoneInfo: + """ + parses a string to get the ZoneInfo + If not set or not valid gets local time, + if that is not possible get UTC + + Keyword Arguments: + timezone_tz {str} -- _description_ (default: {''}) + + Returns: + ZoneInfo -- _description_ + """ + try: + return ZoneInfo(timezone_tz) + except (ZoneInfoNotFoundError, ValueError, TypeError): + # use default + time_tz, time_tz_str = get_system_timezone() + if time_tz is None: + return ZoneInfo('UTC') + # TODO build proper TZ lookup + tz_mapping = { + 'JST': 'Asia/Tokyo', + 'KST': 'Asia/Seoul', + 'IST': 'Asia/Kolkata', + 'CST': 'Asia/Shanghai', # Default to China for CST + 'AEST': 'Australia/Sydney', + 'AWST': 'Australia/Perth', + 'EST': 'America/New_York', + 'EDT': 'America/New_York', + 'CDT': 'America/Chicago', + 'MST': 'America/Denver', + 'MDT': 'America/Denver', + 'PST': 'America/Los_Angeles', + 'PDT': 'America/Los_Angeles', + 'GMT': 'UTC', + 'UTC': 'UTC', + 'CET': 'Europe/Berlin', + 'CEST': 'Europe/Berlin', + 'BST': 'Europe/London', + } + try: + return ZoneInfo(tz_mapping[time_tz_str]) + except (ZoneInfoNotFoundError, IndexError) as e: + raise ValueError(f"No mapping for {time_tz_str}: {e}") from e + + +def get_datetime_iso8601(timezone_tz: str | ZoneInfo = '', sep: str = 'T', timespec: str = 'microseconds') -> str: + """ + set a datetime in the iso8601 format with microseconds + + Returns: + str -- _description_ + """ + # parse if this is a string + if isinstance(timezone_tz, str): + timezone_tz = parse_timezone_data(timezone_tz) + return datetime.now(timezone_tz).isoformat(sep=sep, timespec=timespec) # __END__