Support PYSON comparison of date and datetime

issue4879
review270061002
This commit is contained in:
Cédric Krier committed 2020-09-05 22:59:38 +02:00
1 parent 6a9226eba0
commit 982990fe4d
2 files changed
+21 -5

No files matched your search

+2
View File
@@ -1,3 +1,5 @@
* Support PYSON comparison of date and datetime
Version 5.6.0 - 2020-05-04
* Bug fixes (see mercurial logs for details)
* Add support for Python 3.8
+19 -5
View File
@@ -302,11 +302,15 @@ class Greater(PYSON):
super(Greater, self).__init__()
for i in (statement1, statement2):
if isinstance(i, PYSON):
assert i.types().issubset({int, float, type(None)}), \
'statement must be an integer or a float'
assert i.types().issubset(
{int, float, type(None),
datetime.datetime, datetime.date}), \
'statement must be an integer, float, date or datetime'
else:
assert isinstance(i, (int, float, type(None))), \
'statement must be an integer or a float'
assert isinstance(i, (
int, float, type(None),
datetime.datetime, datetime.date)), \
'statement must be an integer, float, date or datetime'
if isinstance(equal, PYSON):
if equal.types() != {bool}:
equal = Bool(equal)
@@ -338,11 +342,19 @@ class Greater(PYSON):
dct[i] = 0.0
if not isinstance(dct[i], (int, float)):
dct = dct.copy()
dct[i] = float(dct[i])
stmt = dct[i]
if isinstance(stmt, datetime.datetime):
stmt = stmt.timestamp()
elif isinstance(stmt, datetime.date):
time = datetime.time(0, 0)
stmt = datetime.datetime.combine(stmt, time).timestamp()
dct[i] = float(stmt)
return dct
@staticmethod
def eval(dct, context):
if dct['s1'] is None or dct['s2'] is None:
return False
dct = Greater._convert(dct)
if dct['e']:
return dct['s1'] >= dct['s2']
@@ -359,6 +371,8 @@ class Less(Greater):
@staticmethod
def eval(dct, context):
if dct['s1'] is None or dct['s2'] is None:
return False
dct = Less._convert(dct)
if dct['e']:
return dct['s1'] <= dct['s2']