From 069c68db512e80ac1f2bc77e3063288197786fff Mon Sep 17 00:00:00 2001 From: David Michaels Date: Fri, 19 Apr 2024 15:03:30 -0400 Subject: [PATCH] cleaned up some datetime_utils stuff --- dcicutils/datetime_utils.py | 17 ++++++++++------- pyproject.toml | 2 +- test/test_datetime_utils.py | 8 +++++--- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/dcicutils/datetime_utils.py b/dcicutils/datetime_utils.py index 5b812c427..c28e7553f 100644 --- a/dcicutils/datetime_utils.py +++ b/dcicutils/datetime_utils.py @@ -235,8 +235,8 @@ def format_datetime(value: datetime, return value.strftime(f"%Y-%m-%d") else: return value.strftime(f"%Y-%m-%dT%H:%M") - tz = value.strftime("%z") - tz = tz[:3] + ":" + tz[3:] + if len(tz := value.strftime("%z")) > 3: + tz = tz[:3] + ":" + tz[3:] if nodate is True: return value.strftime(f"%H:%M") + tz elif notime is True: @@ -244,7 +244,11 @@ def format_datetime(value: datetime, else: return value.strftime(f"%Y-%m-%dT%H:%M") + tz if nodate is True: - return value.strftime(f"%H:%M:%S{f'.%f' if ms is True else ''}") + if (not (notz is True)) and len(tz := value.strftime("%z")) > 3: + tz = tz[:3] + ":" + tz[3:] + else: + tz = "" + return value.strftime(f"%H:%M:%S{f'.%f' if ms is True else ''}") + tz elif notime is True: return value.strftime(f"%Y-%m-%d") else: @@ -278,21 +282,20 @@ def format_datetime(value: datetime, def format_date(value: datetime, utc: bool = False, - iso: bool = False, tz: Optional[Union[timezone, bool]] = None, verbose: bool = False, noday: bool = False) -> str: - return format_datetime(value, utc=utc, iso=iso, tz=tz, verbose=verbose, noday=noday, notime=True) + return format_datetime(value, utc=utc, tz=tz, verbose=verbose, noday=noday, notime=True) def format_time(value: datetime, utc: bool = False, iso: bool = False, - ms: bool = False, tz: Optional[Union[timezone, bool]] = None, + ms: bool = False, notz: bool = False, noseconds: bool = False, verbose: bool = False, noday: bool = False) -> str: - return format_datetime(value, utc=utc, iso=iso, ms=ms, tz=tz, notz=notz, + return format_datetime(value, utc=utc, tz=tz, iso=iso, ms=ms, notz=notz, noseconds=noseconds, verbose=verbose, nodate=True) diff --git a/pyproject.toml b/pyproject.toml index 67a667e82..ea70db818 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "dcicutils" -version = "8.8.3.1b19" # TODO: To become 8.8.4 +version = "8.8.3.1b20" # TODO: To become 8.8.4 description = "Utility package for interacting with the 4DN Data Portal and other 4DN resources" authors = ["4DN-DCIC Team "] license = "MIT" diff --git a/test/test_datetime_utils.py b/test/test_datetime_utils.py index 6ddaf53e3..116f39d61 100644 --- a/test/test_datetime_utils.py +++ b/test/test_datetime_utils.py @@ -116,9 +116,11 @@ def _test_parse_datetime_a(ms: Optional[int] = None): assert format_date(parsed, verbose=True) == f"Wednesday, April 17, 2024" assert format_date(parsed, verbose=True, noday=True) == f"April 17, 2024" assert format_date(parsed) == f"2024-04-17" - assert format_date(parsed, iso=True) == f"2024-04-17" - assert format_time(parsed, iso=True) == f"15:04:16" - assert format_time(parsed, iso=True, ms=ms is not None) == f"15:04:16{ms_suffix}" + assert format_date(parsed) == f"2024-04-17" + assert format_time(parsed, iso=True) == f"15:04:16{TZLOCAL_SUFFIX}" + assert format_time(parsed, iso=True, ms=ms is not None) == f"15:04:16{ms_suffix}{TZLOCAL_SUFFIX}" + assert format_time(parsed, iso=True, notz=True, ms=ms is not None) == f"15:04:16{ms_suffix}" + assert format_time(parsed, iso=True, notz=True) == f"15:04:16" value = f"2024-04-17T15:04:16{ms_suffix}" parsed = parse_datetime(value, utc=True)