Add sun cmd
This commit is contained in:
@@ -0,0 +1,69 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/nathan-osman/go-sunrise"
|
||||||
|
|
||||||
|
"code.chimeric.al/dwrz/sun/pkg/geo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
coordinates = flag.String("c", "", "coordinates")
|
||||||
|
date = flag.String("d", "", "date in YYYY-MM-DD format")
|
||||||
|
tz = flag.String("z", "", "timezone")
|
||||||
|
f = flag.String("f", "", "output time format")
|
||||||
|
)
|
||||||
|
|
||||||
|
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(),
|
||||||
|
)
|
||||||
|
|
||||||
|
if *f == "" {
|
||||||
|
fmt.Println(
|
||||||
|
rise.In(loc).Format(time.RFC3339),
|
||||||
|
set.In(loc).Format(time.RFC3339),
|
||||||
|
set.Sub(rise),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
fmt.Println(
|
||||||
|
rise.In(loc).Format(*f),
|
||||||
|
set.In(loc).Format(*f),
|
||||||
|
set.Sub(rise),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user