From 1f28ca7aaaa3d0e4fe28b7250972996f2101e7cc Mon Sep 17 00:00:00 2001 From: Kurt Schwehr Date: Mon, 5 Aug 2024 08:16:34 -0700 Subject: [PATCH] filter.py: Make sure that ee.Filter.date can take a datetime. PiperOrigin-RevId: 659560085 --- python/ee/filter.py | 5 +++- python/ee/tests/filter_test.py | 44 ++++++++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/python/ee/filter.py b/python/ee/filter.py index 7b7ffb317..078afec54 100644 --- a/python/ee/filter.py +++ b/python/ee/filter.py @@ -12,6 +12,7 @@ from __future__ import annotations +import datetime from typing import Any, List, Optional, Tuple, Union from ee import _utils @@ -23,7 +24,9 @@ from ee import ee_string from ee import errormargin -_DateType = Union[float, str, 'ee_date.Date', computedobject.ComputedObject] +_DateType = Union[ + datetime.datetime, float, str, 'ee_date.Date', computedobject.ComputedObject +] _EeAnyType = Union[Any, computedobject.ComputedObject] _ErrorMarginType = Union[ float, diff --git a/python/ee/tests/filter_test.py b/python/ee/tests/filter_test.py index e9e82130a..d2f5067b2 100644 --- a/python/ee/tests/filter_test.py +++ b/python/ee/tests/filter_test.py @@ -77,8 +77,8 @@ def test_logical_combinations(self): def test_date(self): """Verifies that date filters work.""" - d1 = datetime.datetime.strptime('1/1/2000', '%m/%d/%Y') - d2 = datetime.datetime.strptime('1/1/2001', '%m/%d/%Y') + d1 = datetime.datetime(2000, 1, 1) + d2 = datetime.datetime(2001, 1, 1) instant_range = ee.ApiFunction.call_('DateRange', d1, None) long_range = ee.ApiFunction.call_('DateRange', d1, d2) @@ -256,6 +256,46 @@ def test_contains(self): result = json.loads(expression.serialize()) self.assertEqual(expect, result) + def test_date_with_datetime(self): + start = datetime.datetime(2024, 8, 3) + end = datetime.datetime(2024, 8, 10) + expect = make_expression_graph({ + 'functionName': 'Filter.dateRangeContains', + 'arguments': { + 'leftValue': { + 'functionInvocationValue': { + 'functionName': 'DateRange', + 'arguments': { + 'start': { + 'functionInvocationValue': { + 'functionName': 'Date', + 'arguments': { + 'value': {'constantValue': 1722643200000} + }, + } + }, + 'end': { + 'functionInvocationValue': { + 'functionName': 'Date', + 'arguments': { + 'value': {'constantValue': 1723248000000} + }, + } + }, + }, + } + }, + 'rightField': {'constantValue': 'system:time_start'}, + }, + }) + expression = ee.Filter.date(start, end) + result = json.loads(expression.serialize()) + self.assertEqual(expect, result) + + expression = ee.Filter.date(start=start, end=end) + result = json.loads(expression.serialize()) + self.assertEqual(expect, result) + def test_date_range_contains(self): left_field = 'a' right_value = 1