From 46a87ee3a7e8ec13481009783a6bc45ba9cc6d4a Mon Sep 17 00:00:00 2001 From: Chad Birch Date: Sat, 6 Oct 2018 12:36:04 -0600 Subject: [PATCH] Add test for timezone handling --- test_ago.py | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/test_ago.py b/test_ago.py index d40ae31..43cfb18 100644 --- a/test_ago.py +++ b/test_ago.py @@ -1,7 +1,6 @@ from __future__ import print_function -from datetime import datetime -from datetime import timedelta +from datetime import datetime, timedelta, tzinfo from ago import human from ago import delta2dict @@ -17,6 +16,25 @@ FUTURE_DELTA = PRESENT - FUTURE ONE_YEAR_FOUR_HOURS_DELTA = timedelta(365, 14400, 0) +class FixedOffset(tzinfo): + """Fixed offset in minutes east from UTC. + + Adapted from Python's documentation about tzinfo objects: + https://docs.python.org/2.7/library/datetime.html#tzinfo-objects + """ + def __init__(self, offset): + self._offset = timedelta(minutes=offset) + + def utcoffset(self, dt): + return self._offset + + def tzname(self, dt): + return "Test" + + def dst(self, dt): + return timedelta(0) + + def test_human_passed_datetime_is_string(): assert isinstance(human(PAST), str) @@ -125,6 +143,19 @@ def test_valid_future_dict(): assert past_dict["microsecond"] == 742 +def test_timezone_support(): + # test a naive datetime with no timezone + # (add an extra minute to ensure it stays over an hour ahead during test) + dt = datetime.now() + timedelta(minutes=61) + output = human(dt) + assert output.startswith("in 1 hour") + + # test a timezone-aware datetime with a UTC-2 timezone + dt = datetime.now(tz=FixedOffset(-120)) + timedelta(minutes=61) + output = human(dt) + assert output.startswith("in 1 hour") + + def example_usage(): """Test and example usage"""