func monitorGeo() { log.Printf("Fetching location...") geo, err := geoip.LookupIP("") if err != nil { log.Fatal(err) } log.Printf(" - City: %s, Lat: %f, Lon: %f", geo.City, geo.Lat, geo.Lon) log.Printf("Monitoring daylight settings...") var lastState *bool for { rise := astrotime.NextSunrise(time.Now(), geo.Lat, -geo.Lon) set := astrotime.NextSunset(time.Now(), geo.Lat, -geo.Lon) state := rise.Before(set) if lastState != nil && state == *lastState { time.Sleep(1 * time.Minute) continue } lastState = &state if state { log.Print("Good night!") sct.SetColorTemp(*nightTemp) } else { log.Print("Good morning!") sct.SetColorTemp(*dayTemp) } time.Sleep(1 * time.Minute) } }
func sendEverySunset(c chan<- sunEvent) { for { now := time.Now() sunset := astrotime.NextSunset(now, sflat, sflon).Add(-time.Duration(15) * time.Minute) log.Printf("next sunset at %s", sunset) time.Sleep(sunset.Sub(now)) c <- sunsetEvent } }
func main() { // t := time.Date(2015, time.December, 31, 23, 0, 0, 0, time.Local) t := time.Now() fmt.Printf("Go launched at %s\n", t.Local()) nextSunrise := astrotime.NextSunrise(t, latitude, longitude) fmt.Printf("next sunrise at %s\n", nextSunrise) nextSunset := astrotime.NextSunset(t, latitude, longitude) fmt.Printf("next sunset at %s\n", nextSunset) if nextSunset.Before(nextSunrise) { fmt.Printf("It is daytime\n") } else { fmt.Printf("It is nighttime\n") } }
func SunHandlerV0(w http.ResponseWriter, r *http.Request) { var latitude, longitude *float64 var lat, lon float64 var err error if r.Method != "GET" { http.Error(w, "Not Found", http.StatusNotFound) return } components := strings.Split(r.URL.Path[1:], "/") if len(components) != 4 { http.Error(w, "Bad Request", http.StatusBadRequest) return } for _, comp := range components { switch { case len(comp) > 3 && comp[:3] == "lat": lat, err = strconv.ParseFloat(comp[3:], 64) if err == nil { latitude = &lat } else { http.Error(w, "Bad Request", http.StatusBadRequest) return } case len(comp) > 3 && comp[:3] == "lon": lon, err = strconv.ParseFloat(comp[3:], 64) if err == nil { longitude = &lon } else { http.Error(w, "Bad Request", http.StatusBadRequest) return } } } if latitude == nil || longitude == nil { http.Error(w, "Bad Request", http.StatusBadRequest) return } if components[1] == "sunrise" { fmt.Fprint(w, astrotime.NextSunrise(time.Now(), *latitude, *longitude)) } if components[1] == "sunset" { fmt.Fprint(w, astrotime.NextSunset(time.Now(), *latitude, *longitude)) } }