Skip to content

Commit

Permalink
Update pretty_date
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolargo committed Nov 17, 2024
1 parent e1e4e8b commit feefcd4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
26 changes: 15 additions & 11 deletions glances/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def build_str_when_more_than_seven_days(day_diff, unit):
return str(count) + " " + unit + maybe_add_plural(count)


def pretty_date(time=False):
def pretty_date(ref, now=None):
"""
Get a datetime object or a int() Epoch timestamp and return a
pretty string like 'an hour ago', 'Yesterday', '3 months ago',
Expand All @@ -306,12 +306,13 @@ def pretty_date(time=False):
Refactoring done in commit https://github.com/nicolargo/glances/commit/f6279baacd4cf0b27ca10df6dc01f091ea86a40a
break the function. Get back to the old fashion way.
"""
now = datetime.now()
if isinstance(time, int):
diff = now - datetime.fromtimestamp(time)
elif isinstance(time, datetime):
diff = now - time
elif not time:
if not now:
now = datetime.now()
if isinstance(ref, int):
diff = now - datetime.fromtimestamp(ref)
elif isinstance(ref, datetime):
diff = now - ref
elif not ref:
diff = 0
second_diff = diff.seconds
day_diff = diff.days
Expand All @@ -335,12 +336,15 @@ def pretty_date(time=False):
if day_diff == 1:
return "yesterday"
if day_diff < 7:
return str(day_diff) + " days"
return str(day_diff) + " days" if day_diff > 1 else "a day"
if day_diff < 31:
return str(day_diff // 7) + " weeks"
week = day_diff // 7
return str(week) + " weeks" if week > 1 else "a week"
if day_diff < 365:
return str(day_diff // 30) + " months"
return str(day_diff // 365) + " years"
month = day_diff // 30
return str(month) + " months" if month > 1 else "a month"
year = day_diff // 365
return str(year) + " years" if year > 1 else "an year"


def urlopen_auth(url, username, password):
Expand Down
6 changes: 3 additions & 3 deletions unittest-core.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,11 +571,11 @@ def test_021_pretty_date(self):
self.assertEqual(pretty_date(datetime(2024, 1, 1, 11, 55), datetime(2024, 1, 1, 12, 0)), '5 mins')
self.assertEqual(pretty_date(datetime(2024, 1, 1, 11, 0), datetime(2024, 1, 1, 12, 0)), 'an hour')
self.assertEqual(pretty_date(datetime(2024, 1, 1, 0, 0), datetime(2024, 1, 1, 12, 0)), '12 hours')
self.assertEqual(pretty_date(datetime(2023, 12, 20, 0, 0), datetime(2024, 1, 1, 12, 0)), '1 week')
self.assertEqual(pretty_date(datetime(2023, 12, 20, 0, 0), datetime(2024, 1, 1, 12, 0)), 'a week')
self.assertEqual(pretty_date(datetime(2023, 12, 5, 0, 0), datetime(2024, 1, 1, 12, 0)), '3 weeks')
self.assertEqual(pretty_date(datetime(2023, 12, 1, 0, 0), datetime(2024, 1, 1, 12, 0)), '1 month')
self.assertEqual(pretty_date(datetime(2023, 12, 1, 0, 0), datetime(2024, 1, 1, 12, 0)), 'a month')
self.assertEqual(pretty_date(datetime(2023, 6, 1, 0, 0), datetime(2024, 1, 1, 12, 0)), '7 months')
self.assertEqual(pretty_date(datetime(2023, 1, 1, 0, 0), datetime(2024, 1, 1, 12, 0)), '1 year')
self.assertEqual(pretty_date(datetime(2023, 1, 1, 0, 0), datetime(2024, 1, 1, 12, 0)), 'an year')
self.assertEqual(pretty_date(datetime(2020, 1, 1, 0, 0), datetime(2024, 1, 1, 12, 0)), '4 years')

def test_094_thresholds(self):
Expand Down

0 comments on commit feefcd4

Please sign in to comment.