-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (74 loc) · 2.21 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"fmt"
"time"
"flag"
"github.com/kelvins/sunrisesunset"
"math"
"os"
)
const DATE_FORMAT = "2006-01-02"
const OUTPUT_FORMAT = "2006-01-02 15:04:05 -0700 MST"
type CLIArgs struct {
lat, long float64
date string
nopause bool
}
func perror(msg string) {
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, msg)
}
func arg_fail(msg string) {
perror(msg)
perror("Usage:")
flag.PrintDefaults()
os.Exit(2)
}
func parseArgs() CLIArgs {
var args CLIArgs
flag.Float64Var(&args.lat, "lat", math.NaN(), "latitude")
flag.Float64Var(&args.long, "long", math.NaN(), "longitude")
flag.StringVar(&args.date, "date", time.Now().Format(DATE_FORMAT),
"date in YYYY.MM.DD format. Default is current date.")
flag.BoolVar(&args.nopause, "nopause", false, "don't wait for user to press Enter")
flag.Parse()
if math.IsNaN(args.lat) {
arg_fail("Latitude is not specified")
}
if math.IsNaN(args.long) {
arg_fail("Longitude is not specified")
}
if _, err := time.Parse(DATE_FORMAT, args.date) ; err != nil {
arg_fail("Bad date string: " + err.Error())
}
return args
}
func main() {
args := parseArgs()
date, err := time.Parse(DATE_FORMAT, args.date)
if err != nil {
panic(err)
}
p := sunrisesunset.Parameters{
Latitude: args.lat,
Longitude: args.long,
UtcOffset: 0.0,
Date: date,
}
// Calculate the sunrise and sunset times
sunrise, sunset, err := p.GetSunriseSunset()
sunrise = time.Date(date.Year(), date.Month(), date.Day(), sunrise.Hour(), sunrise.Minute(), sunrise.Second(), 0, time.UTC)
sunset = time.Date(date.Year(), date.Month(), date.Day(), sunset.Hour(), sunset.Minute(), sunset.Second(), 0, time.UTC)
// If no error has occurred, print the results
if err == nil {
fmt.Printf("Sunrise\t: %s\n", sunrise.Local().Format(OUTPUT_FORMAT))
fmt.Printf("Sunset\t: %s\n", sunset.Local().Format(OUTPUT_FORMAT))
} else {
fmt.Println(err)
}
var x string
if !args.nopause {
fmt.Fprintln(os.Stderr, "\nPress ENTER to continue...")
fmt.Scanln(&x)
}
}