-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsun.go
55 lines (39 loc) · 1.1 KB
/
sun.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
package hal
import (
"log/slog"
"time"
"github.com/nathan-osman/go-sunrise"
)
type SunTimes struct {
location LocationConfig
}
func NewSunTimes(cfg LocationConfig) *SunTimes {
sunTimes := &SunTimes{
location: cfg,
}
slog.Info("Sun times for location",
"lat", cfg.Latitude,
"lng", cfg.Longitude,
"sunrise", sunTimes.Sunrise().Local().Format(time.Kitchen),
"sunset", sunTimes.Sunset().Local().Format(time.Kitchen),
)
return sunTimes
}
func (s *SunTimes) IsDayTime() bool {
now := time.Now()
rise, set := sunrise.SunriseSunset(s.location.Latitude, s.location.Longitude, now.Year(), now.Month(), now.Day())
return now.After(rise) && now.Before(set)
}
func (s *SunTimes) IsNightTime() bool {
return !s.IsDayTime()
}
func (s *SunTimes) Sunrise() time.Time {
now := time.Now()
rise, _ := sunrise.SunriseSunset(s.location.Latitude, s.location.Longitude, now.Year(), now.Month(), now.Day())
return rise
}
func (s *SunTimes) Sunset() time.Time {
now := time.Now()
_, set := sunrise.SunriseSunset(s.location.Latitude, s.location.Longitude, now.Year(), now.Month(), now.Day())
return set
}