Last active
July 23, 2021 16:00
-
-
Save hydrastro/a5f111c705f6e9deb023b29004c84352 to your computer and use it in GitHub Desktop.
Time ago/Convert time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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