From e29d8c0981dfb04650e63e895ae2192f0dcfeb7a Mon Sep 17 00:00:00 2001 From: akrherz Date: Fri, 17 Nov 2023 09:30:58 -0600 Subject: [PATCH] fix: date parsing --- htdocs/json/sbw_by_point.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/htdocs/json/sbw_by_point.py b/htdocs/json/sbw_by_point.py index 8ace5801f7..f303dc1f5c 100644 --- a/htdocs/json/sbw_by_point.py +++ b/htdocs/json/sbw_by_point.py @@ -8,6 +8,7 @@ import numpy as np import pandas as pd +from pyiem.exceptions import IncompleteWebRequest from pyiem.nws.vtec import VTEC_PHENOMENA, VTEC_SIGNIFICANCE, get_ps_string from pyiem.util import get_sqlalchemy_conn, utc from pyiem.webutil import iemapp @@ -121,6 +122,12 @@ def try_valid(ctx, fields): ctx["valid"] = utc(ts.year, ts.month, ts.day, ts.hour, ts.minute) +def parse_date(val): + """convert string to date.""" + fmt = "%Y/%m/%d" if "/" in val else "%Y-%m-%d" + return datetime.datetime.strptime(val, fmt) + + @iemapp() def application(environ, start_response): """Answer request.""" @@ -128,16 +135,10 @@ def application(environ, start_response): try: ctx["lat"] = float(environ.get("lat", 41.99)) ctx["lon"] = float(environ.get("lon", -92.0)) - ctx["sdate"] = datetime.datetime.strptime( - environ.get("sdate", "2002/1/1"), "%Y/%m/%d" - ) - ctx["edate"] = datetime.datetime.strptime( - environ.get("edate", "2099/1/1"), "%Y/%m/%d" - ) + ctx["sdate"] = parse_date(environ.get("sdate", "2002-01-01")) + ctx["edate"] = parse_date(environ.get("edate", "2099-01-01")) except ValueError: - headers = [("Content-type", "text/plain")] - start_response("404 File Not Found", headers) - return [b"Failed to parse inputs."] + raise IncompleteWebRequest("Invalid Parameters") fmt = environ.get("fmt", "json") try: