func processForm(rw http.ResponseWriter, req *http.Request) { req.ParseForm() log.Println("req.Form", req.Form) date, err := time.Parse("2006/1/2", req.Form["Date"][0]) tz, tzFlag := validZone(req.Form["Zone"][0]) _, zipFlag := validZip(req.Form["Zipcode"][0]) lat, long, coordFlag := validCoords(req.Form["Latitude"][0], req.Form["Longitude"][0]) if err != nil { log.Println("Invalid date") panic(nil) } if !tzFlag { log.Println("Invalid time zone") panic(nil) } log.Println(zipFlag, coordFlag) if zipFlag { zipLat, zipLong, err := ptime.GetCoords("/usr/share/weather-util/zctas", req.Form["Zipcode"][0]) if err != nil { log.Println("Invalid zip code") panic(nil) } loc := ptime.Location{zipLat, zipLong, tz} log.Println(date, loc) times := ptime.GenTimes(date, loc, "ISNA") updatePage(rw, req, times) //ptime.DispTimes(times) log.Println("Valid zip") return } else if coordFlag { loc := ptime.Location{lat, long, tz} times := ptime.GenTimes(date, loc, "ISNA") updatePage(rw, req, times) log.Println("Valid coordinates") return } else if !coordFlag { fmt.Fprintf(rw, "Invalid coordinates") panic(nil) } }
func main() { todayDate, defZone := nowDate() defDate := todayDate.Format("01/02/2006") tz := flag.Int("tz", defZone, "Time zone of location") dateString := flag.String("date", defDate, "Date of calculation") long := flag.Float64("long", math.NaN(), "Longitude of position") lat := flag.Float64("lat", math.NaN(), "Latitude of position") zip := flag.String("zip", "-1", "Zip Code of location") flag.Parse() loc := ptime.Location{} latF := validLatitude(*lat) longF := validLongitude(*long) zipF := validZip(*zip) zoneF := validZone(*tz) date, err := time.Parse("01/02/2006", *dateString) if err != nil { fmt.Printf("Invalid date %s\n", *dateString) return } else if math.IsNaN(*lat) && math.IsNaN(*long) && !zipF { fmt.Printf("No location defined\n") return } else if (latF || longF) && zipF { fmt.Printf("Conflicting coordinates and zip\n") return } else if !latF && longF { fmt.Printf("Invalid latitude, %f\n", lat) return } else if latF && !longF { fmt.Printf("Invalid longitude, %f\n", long) return } else if math.IsNaN(*lat) && !math.IsNaN(*long) { fmt.Printf("Missing latitude\n") return } else if !math.IsNaN(*lat) && math.IsNaN(*long) { fmt.Printf("Missing longitude\n") return } else if !zoneF { fmt.Printf("Invalid time zone, %d\n", tz) return } else if !zipF && *zip != "-1" { fmt.Printf("Invalid zip, %d\n", zip) return } else if latF && longF { loc.Lat = *lat loc.Long = *long loc.Tz = *tz } else if zipF { latitude, longitude, err := ptime.GetCoords("/usr/share/weather-util/zctas", *zip) if err != nil { fmt.Printf("Zip code %s is not available\n", *zip) return } loc.Lat = latitude loc.Long = longitude loc.Tz = *tz } fmt.Println(date, loc) times := ptime.GenTimes(date, loc, "ISNA") ptime.DispTimes(times) }