in datasets/epa_historical_air_quality/pipelines/_images/run_csv_transform_kub/csv_transform.py [0:0]
def convert_dt_format(dt_str: str, from_format: str, include_time: bool = True) -> str:
if not dt_str or str(dt_str).lower() == "nan" or str(dt_str).lower() == "nat":
rtnval = ""
elif len(dt_str.strip()) == 10:
if include_time:
# if there is no time value
rtnval = dt_str + " 00:00:00"
else:
# exclude time value
rtnval = dt_str
elif len(dt_str.strip()) == 16:
if include_time:
# if there is no time value
rtnval = dt_str + ":00"
else:
# exclude time value
rtnval = dt_str
elif len(dt_str.strip().split(" ")[1]) == 8:
# if format of time portion is 00:00:00 then use 00:00 format
dt_str = dt_str[:-3]
rtnval = datetime.datetime.strptime(dt_str, from_format).strftime(
"%Y-%m-%d %H:%M:%S"
)
elif (len(dt_str.strip().split("-")[0]) == 4) and (
len(from_format.strip().split("/")[0]) == 2
):
# if the format of the date portion of the data is in YYYY-MM-DD format
# and from_format is in MM-DD-YYYY then resolve this by modifying the from_format
# to use the YYYY-MM-DD. This resolves mixed date formats in files
from_format = "%Y-%m-%d " + from_format.strip().split(" ")[1]
else:
dt_str = ""
return rtnval