Skip to content

Commit

Permalink
filter.py: Make sure that ee.Filter.date can take a datetime.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 659560085
  • Loading branch information
schwehr authored and Google Earth Engine Authors committed Aug 5, 2024
1 parent d1a124a commit 1f28ca7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
5 changes: 4 additions & 1 deletion python/ee/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from __future__ import annotations

import datetime
from typing import Any, List, Optional, Tuple, Union

from ee import _utils
Expand All @@ -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,
Expand Down
44 changes: 42 additions & 2 deletions python/ee/tests/filter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1f28ca7

Please sign in to comment.