Skip to content

Commit

Permalink
cleaned up some datetime_utils stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
dmichaels-harvard committed Apr 19, 2024
1 parent 38d1f84 commit 069c68d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
17 changes: 10 additions & 7 deletions dcicutils/datetime_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,20 @@ 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:
return value.strftime(f"%Y-%m-%d") + tz
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:
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
license = "MIT"
Expand Down
8 changes: 5 additions & 3 deletions test/test_datetime_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 069c68d

Please sign in to comment.