Skip to content

Instantly share code, notes, and snippets.

@SurendraTamang
Created December 28, 2023 23:54
Show Gist options
  • Save SurendraTamang/c3444f57914e2aa6bc2ab7ea260fe35d to your computer and use it in GitHub Desktop.
Save SurendraTamang/c3444f57914e2aa6bc2ab7ea260fe35d to your computer and use it in GitHub Desktop.
Conversion of seconds to Hours, Minutes and Seconds format
def seconds_to_hms(seconds):
""" Convert seconds to a time format (HH:MM:SS).
Args:
seconds (float): The time duration in seconds.
Returns:
str: The time formatted as HH:MM:SS.
"""
# Ensure the input is a float and round it to the nearest whole number
seconds = int(round(seconds))
# Calculate hours, minutes, and seconds
hours = seconds // 3600
minutes = (seconds % 3600) // 60
seconds = seconds % 60
# Format the time in HH:MM:SS
return f"{hours:02d}:{minutes:02d}:{seconds:02d}"
# Example usage
example_seconds = 323.3185454491682
formatted_time = seconds_to_hms(example_seconds)
print("Formatted time:", formatted_time)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment