Skip to content

Instantly share code, notes, and snippets.

@beatorizu
Last active April 8, 2021 14:41
Show Gist options
  • Save beatorizu/9f3c77a1cda474cdc419b05a7c96bf86 to your computer and use it in GitHub Desktop.
Save beatorizu/9f3c77a1cda474cdc419b05a7c96bf86 to your computer and use it in GitHub Desktop.
human-readable time interval strings
def show_time_elapsed_in_human_friendly(elapsed):
return f'{elapsed / 3600:.0f}h {(elapsed % 3600) / 60:.0f}m {elapsed % 60:0>5.2f}s'
# 3h 13m 07.00s
print(show_time_elapsed_in_human_friendly(timedelta(hours=3, minutes=13, seconds=7).total_seconds()))
# 11587.0
print(timedelta(hours=3, minutes=13, seconds=7).total_seconds())
# 14h 59m 07.00s
print(show_time_elapsed_in_human_friendly(timedelta(hours=13, minutes=59, seconds=7).total_seconds()))
# 50347.0
print(timedelta(hours=13, minutes=59, seconds=7).total_seconds())
# References
# https://arcpy.wordpress.com/2012/04/20/146/
# I made some changes like change print statement to print function and add f-strings
def timeit(func):
@wraps(func)
def wrapper(*args, **kwargs):
begin = time.time()
result = func(*args, **kwargs)
print(f'[I] {func.__name__}: {show_time_elapsed_in_human_friendly(time.time() - begin)}')
return result
return wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment