Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Filter now works on timezone aware datetimes

  • Loading branch information...
commit 550f8c31b659b992737b12ee76bf842692eb569f 1 parent 1feaacb
@rory-geoghegan-ecometrica rory-geoghegan-ecometrica authored
Showing with 53 additions and 4 deletions.
  1. +25 −2 grandfatherson/filters.py
  2. +28 −2 test/test_filters.py
View
27 grandfatherson/filters.py
@@ -3,7 +3,22 @@
"""
import calendar
-from datetime import date, datetime, time, timedelta
+from datetime import datetime, time, timedelta, tzinfo
+
+
+# As gleefully stolen from the python datetime docs
+class UTC(tzinfo):
+ """UTC"""
+ ZERO = timedelta(0)
+
+ def utcoffset(self, dt):
+ return self.ZERO
+
+ def tzname(self, dt):
+ return "UTC"
+
+ def dst(self, dt):
+ return self.ZERO
class Filter(object):
@@ -40,8 +55,16 @@ def filter(cls, datetimes, number, now=None, **options):
if number < 0 or not isinstance(number, (int, long)):
raise ValueError('Invalid number: %s' % number)
+ datetimes = tuple(datetimes)
+
if now is None:
- now = datetime.now()
+ # Sample the first datetime to see if it is timezone-aware
+ tzinfo = None
+ if datetimes and datetimes[0].tzinfo is not None:
+ tzinfo = UTC()
+
+ now = datetime.now(tzinfo)
+
if not hasattr(now, 'second'):
# now looks like a date, so convert it into a datetime
now = datetime.combine(now, time(23, 59, 59, 999999))
View
30 test/test_filters.py
@@ -4,9 +4,12 @@
from grandfatherson import (MONDAY, TUESDAY, WEDNESDAY, THURSDAY,
FRIDAY, SATURDAY, SUNDAY)
from grandfatherson.filters import (Seconds, Minutes, Hours, Days, Weeks,
- Months, Years)
+ Months, Years, UTC)
+def utcdatetime(*args):
+ return datetime(*args, tzinfo=UTC())
+
class TestSeconds(unittest.TestCase):
def setUp(self):
self.now = datetime(2000, 1, 1, 0, 0, 1, 1)
@@ -94,6 +97,30 @@ def test_all_input(self):
datetime(2000, 1, 1, 0, 0, 0, 0),
datetime(2000, 1, 1, 0, 0, 1, 0)]))
+ def test_with_tzinfo(self):
+ utcnow = utcdatetime(2000, 1, 1, 0, 0, 1, 1)
+ tzinfo_datetimes = [
+ utcdatetime(2000, 1, 1, 0, 0, 1, 0),
+ utcdatetime(2000, 1, 1, 0, 0, 0, 1),
+ utcdatetime(2000, 1, 1, 0, 0, 0, 0),
+ utcdatetime(1999, 12, 31, 23, 59, 59, 999999),
+ utcdatetime(1999, 12, 31, 23, 59, 57, 0),
+ ]
+
+ self.assertEqual(Seconds.filter(tzinfo_datetimes, number=5,
+ now=utcnow),
+ set([utcdatetime(1999, 12, 31, 23, 59, 57, 0),
+ utcdatetime(1999, 12, 31, 23, 59, 59, 999999),
+ utcdatetime(2000, 1, 1, 0, 0, 0, 0),
+ utcdatetime(2000, 1, 1, 0, 0, 1, 0)]))
+
+ self.assertEqual(Seconds.filter(tzinfo_datetimes, number=6,
+ now=utcnow),
+ set([utcdatetime(1999, 12, 31, 23, 59, 57, 0),
+ utcdatetime(1999, 12, 31, 23, 59, 59, 999999),
+ utcdatetime(2000, 1, 1, 0, 0, 0, 0),
+ utcdatetime(2000, 1, 1, 0, 0, 1, 0)]))
+
class TestMinutes(unittest.TestCase):
def setUp(self):
@@ -182,7 +209,6 @@ def test_all_input(self):
datetime(2000, 1, 1, 0, 0, 0, 0),
datetime(2000, 1, 1, 0, 1, 0, 0)]))
-
class TestHours(unittest.TestCase):
def setUp(self):
self.now = datetime(2000, 1, 1, 1, 1, 1, 1)
Please sign in to comment.
Something went wrong with that request. Please try again.