11import datetime as dt
22from inspect import getmembers , ismethod
3- from typing import Any , Dict , List , Optional , Tuple , Type
3+ from typing import Any , Dict , List , Optional , Tuple , Type , Union
44
55import dateutil .parser as parser # type: ignore
66from cmr import CollectionQuery , GranuleQuery # type: ignore
@@ -262,31 +262,36 @@ def get(self, limit: int = 2000) -> list:
262262 return results
263263
264264 def temporal (
265- self , date_from : str , date_to : str , exclude_boundary : bool = False
265+ self ,
266+ date_from : Optional [Union [str , dt .datetime ]] = None ,
267+ date_to : Optional [Union [str , dt .datetime ]] = None ,
268+ exclude_boundary : bool = False ,
266269 ) -> Type [CollectionQuery ]:
267270 """Filter by an open or closed date range. Dates can be provided as datetime objects
268271 or ISO 8601 formatted strings. Multiple ranges can be provided by successive calls.
269272 to this method before calling execute().
270273
271274 Parameters:
272- date_from (String): earliest date of temporal range
273- date_to (string ): latest date of temporal range
275+ date_from (String or Datetime object ): earliest date of temporal range
276+ date_to (String or Datetime object ): latest date of temporal range
274277 exclude_boundary (Boolean): whether or not to exclude the date_from/to in the matched range
275278 """
276279 DEFAULT = dt .datetime (1979 , 1 , 1 )
277- if date_from is not None :
280+ if date_from is not None and not isinstance ( date_from , dt . datetime ) :
278281 try :
279- parsed_from = parser .parse (date_from , default = DEFAULT ).isoformat () + "Z"
282+ date_from = parser .parse (date_from , default = DEFAULT ).isoformat () + "Z"
280283 except Exception :
281284 print ("The provided start date was not recognized" )
282- parsed_from = ""
283- if date_to is not None :
285+ date_from = ""
286+
287+ if date_to is not None and not isinstance (date_to , dt .datetime ):
284288 try :
285- parsed_to = parser .parse (date_to , default = DEFAULT ).isoformat () + "Z"
289+ date_to = parser .parse (date_to , default = DEFAULT ).isoformat () + "Z"
286290 except Exception :
287291 print ("The provided end date was not recognized" )
288- parsed_to = ""
289- super ().temporal (parsed_from , parsed_to , exclude_boundary )
292+ date_to = ""
293+
294+ super ().temporal (date_from , date_to , exclude_boundary )
290295 return self
291296
292297
@@ -614,8 +619,8 @@ def debug(self, debug: bool = True) -> Type[GranuleQuery]:
614619
615620 def temporal (
616621 self ,
617- date_from : Optional [str ] = None ,
618- date_to : Optional [str ] = None ,
622+ date_from : Optional [Union [ str , dt . datetime ] ] = None ,
623+ date_to : Optional [Union [ str , dt . datetime ] ] = None ,
619624 exclude_boundary : bool = False ,
620625 ) -> Type [GranuleQuery ]:
621626 """Filter by an open or closed date range.
@@ -628,19 +633,21 @@ def temporal(
628633 exclude_boundary (Boolean): whether or not to exclude the date_from/to in the matched range
629634 """
630635 DEFAULT = dt .datetime (1979 , 1 , 1 )
631- if date_from is not None :
636+ if date_from is not None and not isinstance ( date_from , dt . datetime ) :
632637 try :
633- parsed_from = parser .parse (date_from , default = DEFAULT ).isoformat () + "Z"
638+ date_from = parser .parse (date_from , default = DEFAULT ).isoformat () + "Z"
634639 except Exception :
635640 print ("The provided start date was not recognized" )
636- parsed_from = ""
637- if date_to is not None :
641+ date_from = ""
642+
643+ if date_to is not None and not isinstance (date_to , dt .datetime ):
638644 try :
639- parsed_to = parser .parse (date_to , default = DEFAULT ).isoformat () + "Z"
645+ date_to = parser .parse (date_to , default = DEFAULT ).isoformat () + "Z"
640646 except Exception :
641647 print ("The provided end date was not recognized" )
642- parsed_to = ""
643- super ().temporal (parsed_from , parsed_to , exclude_boundary )
648+ date_to = ""
649+
650+ super ().temporal (date_from , date_to , exclude_boundary )
644651 return self
645652
646653 def version (self , version : str = "" ) -> Type [GranuleQuery ]:
0 commit comments