--- a/src/pyams_catalog/index.py Tue Jun 27 13:41:57 2017 +0200
+++ b/src/pyams_catalog/index.py Wed Jul 12 11:38:51 2017 +0200
@@ -17,7 +17,7 @@
from datetime import date, datetime
# import interfaces
-from pyams_catalog.interfaces import NO_RESOLUTION, DATE_RESOLUTION
+from pyams_catalog.interfaces import NO_RESOLUTION, DATE_RESOLUTION, MONTH_RESOLUTION
# import packages
from hypatia.facet import FacetIndex
@@ -75,21 +75,57 @@
def get_resolution(value, resolution):
- """Set resolution of given date or datetime"""
+ """Set resolution of given date or datetime
+
+ >>> from pyams_catalog.interfaces import *
+ >>> from pyams_catalog.index import get_resolution
+ >>> from datetime import date, datetime
+
+ Starting with dates:
+
+ >>> today = date(2017, 7, 11)
+ >>> get_resolution(today, YEAR_RESOLUTION)
+ datetime.date(2017, 1, 1)
+ >>> get_resolution(today, MONTH_RESOLUTION)
+ datetime.date(2017, 7, 1)
+ >>> get_resolution(today, DATE_RESOLUTION)
+ datetime.date(2017, 7, 11)
+
+ Asking for a resolution higher than DATE with a date input only returns date:
+
+ >>> get_resolution(today, MINUTE_RESOLUTION)
+ datetime.date(2017, 7, 11)
+
+ Same examples with datetimes:
+
+ >>> now = datetime(2017, 7, 11, 13, 22, 10)
+ >>> get_resolution(now, YEAR_RESOLUTION)
+ datetime.datetime(2017, 1, 1, 0, 0)
+ >>> get_resolution(now, MONTH_RESOLUTION)
+ datetime.datetime(2017, 7, 1, 0, 0)
+ >>> get_resolution(now, DATE_RESOLUTION)
+ datetime.datetime(2017, 7, 11, 0, 0)
+ >>> get_resolution(now, HOUR_RESOLUTION)
+ datetime.datetime(2017, 7, 11, 13, 0)
+ >>> get_resolution(now, MINUTE_RESOLUTION)
+ datetime.datetime(2017, 7, 11, 13, 22)
+ >>> get_resolution(now, SECOND_RESOLUTION)
+ datetime.datetime(2017, 7, 11, 13, 22, 10)
+ """
if not value:
return value
if resolution < NO_RESOLUTION:
args = []
- if isinstance(value, date):
- resolution = max(resolution, DATE_RESOLUTION)
+ if not isinstance(value, datetime):
+ resolution = min(resolution, DATE_RESOLUTION)
args.extend(value.timetuple()[:resolution+1])
- if isinstance(value, date):
- args.extend([0] * (2 - resolution))
- value = date(*args)
- else:
- args.extend([0] * (5 - resolution))
+ if isinstance(value, datetime):
+ args.extend(([1] * (DATE_RESOLUTION - resolution) + [0] * 5)[:7-len(args)])
args.append(value.tzinfo)
value = datetime(*args)
+ else:
+ args.extend([1] * (DATE_RESOLUTION - resolution))
+ value = date(*args)
return value