src

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

main.go (1368B)


      1 package main
      2 
      3 import (
      4 	"flag"
      5 	"fmt"
      6 	"log"
      7 	"time"
      8 
      9 	"github.com/nathan-osman/go-sunrise"
     10 
     11 	"code.dwrz.net/src/pkg/geo"
     12 )
     13 
     14 var (
     15 	name        = flag.String("n", "", "location name")
     16 	coordinates = flag.String("c", "", "coordinates")
     17 	date        = flag.String("d", "", "date in YYYY-MM-DD format")
     18 	tz          = flag.String("z", "", "timezone")
     19 	f           = flag.String("f", "", "output time format")
     20 )
     21 
     22 func main() {
     23 	flag.Parse()
     24 	if *coordinates == "" {
     25 		log.Fatalf("missing coordinates")
     26 	}
     27 
     28 	c, err := geo.ParseCoordinates(*coordinates)
     29 	if err != nil {
     30 		log.Fatalf("failed to parse coordinates: %v", err)
     31 	}
     32 
     33 	var now = time.Now()
     34 	if *date != "" {
     35 		var err error
     36 		now, err = time.Parse(time.DateOnly, *date)
     37 		if err != nil {
     38 			log.Fatalf("failed to parse date: %v", err)
     39 		}
     40 	}
     41 
     42 	var loc *time.Location = time.UTC
     43 	if *tz != "" {
     44 		var err error
     45 		loc, err = time.LoadLocation(*tz)
     46 		if err != nil {
     47 			log.Fatalf("failed to parse timezone: %v", err)
     48 		}
     49 	}
     50 
     51 	// https://en.wikipedia.org/wiki/Sunrise_equation
     52 	rise, set := sunrise.SunriseSunset(
     53 		c.Latitude, c.Longitude,
     54 		now.Year(), now.Month(), now.Day(),
     55 	)
     56 
     57 	if *f == "" {
     58 		fmt.Println(
     59 			rise.In(loc).Format(time.RFC3339),
     60 			set.In(loc).Format(time.RFC3339),
     61 			set.Sub(rise),
     62 		)
     63 	} else {
     64 		fmt.Println(
     65 			rise.In(loc).Format(*f),
     66 			set.In(loc).Format(*f),
     67 			set.Sub(rise),
     68 		)
     69 	}
     70 }