Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Have it working when passing in a regular date.

Also pep8'd and pyflaked the files in question.
  • Loading branch information...
commit 0e3d550af3210290d5fe4ba2e8ea1d1e94fb37cb 1 parent 550f8c3
@rory-geoghegan-ecometrica rory-geoghegan-ecometrica authored
Showing with 37 additions and 11 deletions.
  1. +6 −6 grandfatherson/filters.py
  2. +31 −5 test/test_filters.py
View
12 grandfatherson/filters.py
@@ -57,17 +57,17 @@ def filter(cls, datetimes, number, now=None, **options):
datetimes = tuple(datetimes)
- if now is None:
- # Sample the first datetime to see if it is timezone-aware
- tzinfo = None
- if datetimes and datetimes[0].tzinfo is not None:
- tzinfo = UTC()
+ # Sample the first datetime to see if it is timezone-aware
+ tzinfo = None
+ if datetimes and datetimes[0].tzinfo is not None:
+ tzinfo = UTC()
+ if now is None:
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))
+ now = datetime.combine(now, time(23, 59, 59, 999999, tzinfo=tzinfo))
# Always keep datetimes from the future
future = set(dt for dt in datetimes if dt > now)
View
36 test/test_filters.py
@@ -1,14 +1,14 @@
-from datetime import datetime
+from datetime import datetime, date
import unittest
-from grandfatherson import (MONDAY, TUESDAY, WEDNESDAY, THURSDAY,
- FRIDAY, SATURDAY, SUNDAY)
+from grandfatherson import (FRIDAY, SATURDAY, SUNDAY)
from grandfatherson.filters import (Seconds, Minutes, Hours, Days, Weeks,
Months, Years, UTC)
def utcdatetime(*args):
- return datetime(*args, tzinfo=UTC())
+ return datetime(*args, tzinfo=UTC())
+
class TestSeconds(unittest.TestCase):
def setUp(self):
@@ -209,6 +209,7 @@ 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)
@@ -259,7 +260,7 @@ def test_duplicates(self):
# Ensure we get the oldest per-hour datetime when there are
# duplicates: i.e. not datetime(2000, 1, 1, 0, 1, 0, 0)
self.assertEqual(Hours.filter(self.datetimes, number=2,
- now=self.now),
+ now=self.now),
set([datetime(2000, 1, 1, 0, 0, 0, 0),
datetime(2000, 1, 1, 1, 0, 0, 0)]))
@@ -408,6 +409,31 @@ def test_not_leap_year(self):
datetime(1900, 2, 28, 0, 0, 0, 0),
datetime(1900, 3, 1, 0, 0, 0, 0)]))
+ def test_with_tzinfo_and_date(self):
+ tzinfo_datetimes = [
+ utcdatetime(2000, 1, 1, 1, 0, 0, 0),
+ utcdatetime(2000, 1, 1, 0, 0, 0, 0),
+ utcdatetime(1999, 12, 31, 23, 59, 59, 999999),
+ utcdatetime(1999, 12, 30, 0, 0, 0, 0),
+ utcdatetime(1999, 12, 28, 0, 0, 0, 0),
+ ]
+ today = date(2000, 1, 1)
+
+ self.assertEqual(Days.filter(tzinfo_datetimes, number=5, now=today),
+ set([utcdatetime(1999, 12, 28, 0, 0, 0, 0),
+ utcdatetime(1999, 12, 30, 0, 0, 0, 0),
+ utcdatetime(1999, 12, 31, 23, 59, 59, 999999),
+ utcdatetime(2000, 1, 1, 0, 0, 0, 0)]))
+
+ def test_with_date(self):
+ today = date(2000, 1, 1)
+
+ self.assertEqual(Days.filter(self.datetimes, number=5, now=today),
+ set([datetime(1999, 12, 28, 0, 0, 0, 0),
+ datetime(1999, 12, 30, 0, 0, 0, 0),
+ datetime(1999, 12, 31, 23, 59, 59, 999999),
+ datetime(2000, 1, 1, 0, 0, 0, 0)]))
+
class TestWeeks(unittest.TestCase):
def setUp(self):
Please sign in to comment.
Something went wrong with that request. Please try again.