From 8d26598adbb0f6503b8cdf94d85a359e0427eb78 Mon Sep 17 00:00:00 2001 From: Sharon Fitzpatrick Date: Tue, 9 Apr 2024 16:37:52 -0700 Subject: [PATCH] patch format_date to handle if datetime objects are passed --- src/coastsat/SDS_download.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/coastsat/SDS_download.py b/src/coastsat/SDS_download.py index 1eadd18..12a9550 100644 --- a/src/coastsat/SDS_download.py +++ b/src/coastsat/SDS_download.py @@ -1097,10 +1097,29 @@ def read_metadata_file(filepath: str) -> Dict[str, Union[str, int, float]]: return metadata def format_date(date_str: str) -> datetime: + """ + Converts a date string to a datetime object in UTC timezone. + + Args: + date_str (str): The date string to be converted. + + Returns: + datetime: The converted datetime object. + + Raises: + ValueError: If the date string is in an invalid format. + """ + date_formats = ["%Y-%m-%d", "%Y-%m-%dT%H:%M:%S"] + # convert datetime object to string + if isinstance(date_str, datetime) == True: + # converts the datetime object to a string + date_str = date_str.strftime("%Y-%m-%dT%H:%M:%S") + # format the string to a datetime object for date_format in date_formats: try: + # creates a datetime object from a string with the date in UTC timezone start_date = datetime.strptime(date_str, date_format).replace(tzinfo=timezone.utc) return start_date except ValueError: @@ -1108,6 +1127,7 @@ def format_date(date_str: str) -> datetime: else: raise ValueError(f"Invalid date format: {date_str}") + def get_metadata(inputs): """ Gets the metadata from the downloaded images by parsing .txt files located