Skip to content

Commit

Permalink
wrote validation code for datetime format check
Browse files Browse the repository at this point in the history
  • Loading branch information
binni979 committed Apr 8, 2024
1 parent d8c6b5c commit 14b8c64
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions pyQuARC/code/datetime_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ def compare(first, second, relation):

@staticmethod
def validate_datetime_against_granules(
datetime, collection_shortname, version, sort_key, time_key
datetime_string, collection_shortname, version, sort_key, time_key
):
"""
Validates the collection datetime against the datetime of the last granule in the collection
Args:
datetime (str): datetime string
datetime_string (str): datetime string
collection_shortname (str): ShortName of the parent collection
sort_key (str): choice of start_date and end_date
time_key (str): choice of time_end and time_start
Expand All @@ -143,13 +143,39 @@ def validate_datetime_against_granules(

validity = True
last_granule_datetime = None

date_time = None

# Define the formats in decreasing order of precision
formats = [
"%Y-%m-%dT%H:%M:%S.%f", # Year to microsecond
"%Y-%m-%dT%H:%M:%S", # Year to second
"%Y-%m-%dT%H:%M", # Year to minute
"%Y-%m-%dT%H", # Year to hour
"%Y-%m-%d", # Year to day
"%Y-%m", # Year to month
"%Y", # Year
]

# Function to determine the precision of a datetime string
def get_precision(dt_str):
for fmt in formats:
try:
date_time = datetime.strptime(dt_str, fmt)
return date_time, fmt
except ValueError:
continue
return None, False

# Compare the precision of the two datetime strings
# return get_precision(datetime_str1) == get_precision(datetime_str2)
if len(granules["feed"]["entry"]) > 0:
last_granule = granules["feed"]["entry"][0]
last_granule_datetime = last_granule.get(time_key)
validity = datetime == last_granule_datetime
date_time, date_time_format = get_precision(datetime_string)
last_granule_datetime, lgd_format = get_precision(last_granule_datetime)
validity = date_time == last_granule_datetime

return {"valid": validity, "value": (datetime, last_granule_datetime)}
return {"valid": validity, "value": (date_time, last_granule_datetime)}

@staticmethod
@if_arg
Expand Down

0 comments on commit 14b8c64

Please sign in to comment.