Skip to content

Instantly share code, notes, and snippets.

@hydrastro
Last active July 23, 2021 16:00
Show Gist options
  • Select an option

  • Save hydrastro/a5f111c705f6e9deb023b29004c84352 to your computer and use it in GitHub Desktop.

Select an option

Save hydrastro/a5f111c705f6e9deb023b29004c84352 to your computer and use it in GitHub Desktop.
Time ago/Convert time
import time
def convert_time(conv_time, ago=False):
"""Returns time ago/stuff lol"""
periods = ["second", "minute", "hour", "day", "week", "month", "year", "decade", "century", "millennium"]
lengths = [60, 60, 24, 7, 4.35, 12, 10, 10, 10]
if ago:
now = time.time()
difference = now - conv_time
else:
difference = conv_time
j = 0
while difference >= lengths[j] and j < len(lengths) - 1:
difference /= lengths[j]
j += 1
difference = round(difference)
if difference != 1:
periods[j] += "s"
output = f"{difference} {periods[j]}"
if ago:
output = "now" if (difference == 0) else f"{difference} {periods[j]} ago"
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment