Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(framework) Add utility functions in date.py #4489

Merged
merged 3 commits into from
Nov 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/py/flwr/common/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,21 @@
def now() -> datetime.datetime:
"""Construct a datetime from time.time() with time zone set to UTC."""
return datetime.datetime.now(tz=datetime.timezone.utc)


def format_timedelta(td: datetime.timedelta) -> str:
"""Format a timedelta as a string."""
days = td.days
hours, remainder = divmod(td.seconds, 3600)
minutes, seconds = divmod(remainder, 60)

if days > 0:
return f"{days}d {hours:02}:{minutes:02}:{seconds:02}"
return f"{hours:02}:{minutes:02}:{seconds:02}"


def isoformat8601_utc(dt: datetime.datetime) -> str:
"""Return the datetime formatted as an ISO 8601 string with a trailing 'Z'."""
if dt.tzinfo != datetime.timezone.utc:
raise ValueError("Expected datetime with timezone set to UTC")
return dt.isoformat(timespec="seconds").replace("+00:00", "Z")