Last active
June 20, 2021 07:23
-
-
Save jaredyam/6b1ef945c9abce38e1cf08a2ca9cbea5 to your computer and use it in GitHub Desktop.
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
from datetime import datetime | |
def relative_history_date(history_datetime): | |
"""Format history datetime to relative representation. | |
Parameters | |
---------- | |
history_datetime : datetime | |
Returns | |
------- | |
str | |
A string represents history datetime in a specific context. | |
""" | |
assert type(history_datetime) == datetime | |
current_datetime = datetime.now() | |
assert history_datetime <= current_datetime | |
delta = current_datetime - history_datetime | |
# two adjacent days | |
if delta.days < 2 and current_datetime.day - history_datetime.day < 2: | |
if history_datetime.day == current_datetime.day: | |
if delta.seconds < 60: | |
return f'{delta.seconds} seconds ago' | |
elif delta.seconds < 3600: | |
return f'{delta.seconds // 60} minutes ago' | |
else: | |
return f'{history_datetime:%H:%M:%S} at today' | |
else: | |
return f'{history_datetime:%H:%M:%S} at yesterday' | |
# In the same week | |
elif delta.days < 7 and history_datetime.weekday() <= current_datetime.weekday(): | |
return f'{history_datetime:%H:%M:%S} at {history_datetime:%A}' | |
elif history_datetime.year < current_datetime.year: | |
return f'{history_datetime:%H:%M:%S} at {history_datetime:%B %d}, {history_datetime:%Y}' | |
else: | |
return f'{history_datetime:%H:%M:%S} at {history_datetime:%B %d}' | |
def test_relative_history_date(): | |
from datetime import timedelta | |
current_datetime = datetime.now() | |
history_datetime = current_datetime - timedelta(seconds=1) | |
assert relative_history_date(history_datetime) == '1 seconds ago' | |
history_datetime = current_datetime - timedelta(seconds=60) | |
assert relative_history_date(history_datetime) == '1 minutes ago' | |
history_datetime = current_datetime - timedelta(seconds=3600) | |
assert relative_history_date(history_datetime) == f'{history_datetime:%H:%M:%S} at today' | |
date = current_datetime.date() | |
history_datetime = datetime(date.year, date.month, date.day) | |
assert relative_history_date(history_datetime) == '00:00:00 at today' | |
history_datetime = datetime( | |
date.year, date.month, date.day) - timedelta(seconds=1) | |
assert relative_history_date(history_datetime) == '23:59:59 at yesterday' | |
history_datetime = datetime( | |
date.year, date.month, date.day) - timedelta(days=1, seconds=1) | |
if current_datetime.weekday() == 0: | |
assert relative_history_date(history_datetime) == f'23:59:59 at {history_datetime:%B %d}' | |
else: | |
assert relative_history_date(history_datetime) == f'23:59:59 at {history_datetime:%A}' | |
history_datetime = datetime(date.year - 1, 1, 1) | |
assert relative_history_date(history_datetime) == f'00:00:00 at January 01, {history_datetime:%Y}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment