Skip to content

Commit

Permalink
patch format_date to handle if datetime objects are passed
Browse files Browse the repository at this point in the history
  • Loading branch information
2320sharon committed Apr 9, 2024
1 parent 2db5a4a commit 8d26598
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/coastsat/SDS_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -1097,17 +1097,37 @@ 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:
pass
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
Expand Down

0 comments on commit 8d26598

Please sign in to comment.