(core) Allow using MIN and MAX functions with dates/datetimes

Summary:
The MIN and MAX functions for formulas previously only considered numbers, ignoring other types, including dates. An example of this being a problem is here: https://community.getgrist.com/t/last-field-circularreferror-what-is-it/1114/4 . Using `MIN` on a column of dates would return 0 (the default) which gets converted to 1970-01-01. Users have to use `min` instead, which is confusing, and doesn't work when some values are empty.

This diff lets the functions operate on date and datetime values. A mixture of dates and datetimes is allowed, even though these cannot usually be compared in Python. Mixing dates and numbers will raise an exception.

Test Plan: Extended doctests

Reviewers: jarek, paulfitz

Reviewed By: jarek

Subscribers: paulfitz

Differential Revision: https://phab.getgrist.com/D3560
This commit is contained in:
Alex Hall
2022-08-04 14:24:23 +02:00
parent 787b70f0f6
commit 083a0ec000
2 changed files with 54 additions and 19 deletions

View File

@@ -1,6 +1,8 @@
# pylint: disable=unused-argument
from __future__ import absolute_import
import datetime
import math as _math
import operator
import os
@@ -38,6 +40,13 @@ def _chain_numeric_a(*values_or_iterables):
yield int(v) if ISLOGICAL(v) else v if ISNUMBER(v) else 0
# Iterates through iterable or other arguments, only including numbers, dates, and datetimes.
def _chain_numeric_or_date(*values_or_iterables):
for v in _chain(*values_or_iterables):
if ISNUMBER(v) and not ISLOGICAL(v) or isinstance(v, (datetime.date, datetime.datetime)):
yield v
def _round_toward_zero(value):
return _math.floor(value) if value >= 0 else _math.ceil(value)