Last active
January 11, 2023 16:25
-
-
Save Rob-McCormack/9fe570122fb6fadb5de0406a7bc9844d to your computer and use it in GitHub Desktop.
Converts a date string to a human readable date string.
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
""" | |
This program converts a date string to a human readable date string. | |
Example: `2022-11-5 7:00:10` --> 2 months ago | |
""" | |
import datetime | |
def calculate_time_difference(date_input): | |
now = datetime.datetime.now() | |
return now - date_input | |
def convert_time_difference_to_string(time_difference): | |
seconds = time_difference.total_seconds() | |
# Calculate the approximate number of seconds in each period and set variable. | |
time_units = [ | |
(365.25 * 24 * 60 * 60, 'year'), | |
(30 * 24 * 60 * 60, 'month'), | |
(24 * 60 * 60, 'day'), | |
(60 * 60, 'hour'), | |
(60, 'minute'), | |
(1, 'second') | |
] | |
# Remove conditional if you want `3` seconds ago` instead of `Just now`. | |
if seconds < 60: | |
return "Just now" | |
for value, unit in time_units: | |
if seconds >= value: | |
num_units = int(seconds // value) | |
time_string = f'{num_units} {unit}' | |
if num_units > 1: | |
time_string += 's' | |
break | |
else: | |
time_string = '0 seconds' | |
time_string += ' ago' | |
return time_string | |
def get_how_long_ago(date_input): | |
try: | |
# Convert the date_string to a datetime object. | |
date = datetime.datetime.strptime(date_input, '%Y-%m-%d %H:%M:%S') | |
except ValueError: | |
return '! Invalid date format.' | |
now = datetime.datetime.now() | |
if date > now: | |
return '! Date is in the future.' | |
time_difference = calculate_time_difference(date) | |
return convert_time_difference_to_string(time_difference) | |
def main(): | |
line_width = 60 | |
separator = '=' | |
print(f"{' [ Generated From now() ] '.center(line_width, separator)}") | |
for i in range(10): | |
generated_date_in_past = datetime.datetime.now( | |
) + datetime.timedelta(seconds=-(2**(i+1))**3) | |
generated_date_in_past = generated_date_in_past.strftime( | |
"%Y-%m-%d %H:%M:%S") | |
print('- ', get_how_long_ago(generated_date_in_past)) | |
print(f"{' [ Static Examples ] '.center(line_width, separator)}") | |
print(get_how_long_ago('1954-05-05 9:33:10')) | |
print(get_how_long_ago('2022-11-5 7:00:10')) | |
print(get_how_long_ago('2023-01-1 11:13:37')) | |
print(get_how_long_ago('2023-01-9 12:22:50')) | |
print(get_how_long_ago('2033-09-06 22:45:50')) | |
print(f"{' [ Illegal Examples ] '.center(line_width, separator)}") | |
print(get_how_long_ago('2023-01-32 19:33:30')) | |
print(get_how_long_ago('2018-02-29 19:33:30')) | |
print(get_how_long_ago('2018-01-01 25:00:00')) | |
print(get_how_long_ago('2018-01-01 19:00:70')) | |
print(f"{' [] '.center(line_width, separator)}") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment