Skip to content

Commit

Permalink
fix: date parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
akrherz committed Nov 17, 2023
1 parent e086772 commit e29d8c0
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions htdocs/json/sbw_by_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -121,23 +122,23 @@ 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."""
ctx = {}
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:
Expand Down

0 comments on commit e29d8c0

Please sign in to comment.