src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

commit cc703aded3f352760a56c4b79e5600614fe9ffaa
parent b28407f5c22220027e4d81e85991cc4ebc979ae2
Author: dwrz <dwrz@dwrz.net>
Date:   Sun, 25 Feb 2024 18:10:12 +0000

Add sun cmd

Diffstat:
Acmd/sun/main.go | 60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 60 insertions(+), 0 deletions(-)

diff --git a/cmd/sun/main.go b/cmd/sun/main.go @@ -0,0 +1,60 @@ +package main + +import ( + "flag" + "fmt" + "log" + "time" + + "github.com/nathan-osman/go-sunrise" + + "code.dwrz.net/src/pkg/geo" +) + +var ( + name = flag.String("n", "", "location name") + coordinates = flag.String("c", "", "coordinates") + date = flag.String("d", "", "date in YYYY-MM-DD format") + tz = flag.String("z", "", "timezone") +) + +func main() { + flag.Parse() + if *coordinates == "" { + log.Fatalf("missing coordinates") + } + + c, err := geo.ParseCoordinates(*coordinates) + if err != nil { + log.Fatalf("failed to parse coordinates: %v", err) + } + + var now = time.Now() + if *date != "" { + var err error + now, err = time.Parse(time.DateOnly, *date) + if err != nil { + log.Fatalf("failed to parse date: %v", err) + } + } + + var loc *time.Location = time.UTC + if *tz != "" { + var err error + loc, err = time.LoadLocation(*tz) + if err != nil { + log.Fatalf("failed to parse timezone: %v", err) + } + } + + // https://en.wikipedia.org/wiki/Sunrise_equation + rise, set := sunrise.SunriseSunset( + c.Latitude, c.Longitude, + now.Year(), now.Month(), now.Day(), + ) + + fmt.Println( + rise.In(loc).Format(time.RFC3339), + set.In(loc).Format(time.RFC3339), + ) +}