Skip to content
Open
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
27 changes: 27 additions & 0 deletions utils/time_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from datetime import datetime
import pytz

def current_time_as_string() -> str:
"""
Returns the current time as a formatted string in HH:MM:SS format.

The function gets the current time as a datetime object, converts it to the
desired timezone (if not provided, uses the local timezone), and formats it
as a string in HH:MM:SS format.

Returns:
str: The current time in HH:MM:SS format.
"""
# Get the current time as a datetime object
now = datetime.now()

# If a timezone is not provided, use the local timezone
timezone = pytz.timezone("UTC") if now.tzinfo is None else now.tzinfo

# Convert the datetime object to the desired timezone
aware_now = now.astimezone(timezone)

# Format the datetime object to the desired HH:MM:SS format
formatted_time = aware_now.strftime("%H:%M:%S")

return formatted_time