Add test for timezone handling
This commit is contained in:
parent
aa6d7498a2
commit
46a87ee3a7
1 changed files with 33 additions and 2 deletions
35
test_ago.py
35
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"""
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue