Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added dynamic sunrise/sunset calculations #19

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func LoadConfig() (*Config, error) {
zones := make([]*Zone, len(tzConfigs)+1)

// Setup with Local time zone
now := time.Now()
now := clock()
localZoneName, offset := now.Zone()
zones[0] = &Zone{
Name: fmt.Sprintf("(%s) Local", localZoneName),
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module github.com/oz/tz
go 1.15

require (
github.com/WIZARDISHUNGRY/tzcoords v0.0.0-20210325205433-561af37617d6 // indirect
github.com/charmbracelet/bubbletea v0.13.1
github.com/kelvins/sunrisesunset v0.0.0-20210220141756-39fa1bd816d5 // indirect
github.com/muesli/termenv v0.8.0
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
github.com/WIZARDISHUNGRY/tzcoords v0.0.0-20210325205433-561af37617d6 h1:4/I6WWUNgwK2sskEO+NlZNcZT6XCHDXU8s4egPUeoC0=
github.com/WIZARDISHUNGRY/tzcoords v0.0.0-20210325205433-561af37617d6/go.mod h1:SxhOS/aSnR1fwsbsOSMjIvFiaR1F5D4Y+DqoUu5H4iQ=
github.com/charmbracelet/bubbletea v0.13.1 h1:huvX8mPaeMZ8DLulT50iEWRF+iitY5FNEDqDVLu69nM=
github.com/charmbracelet/bubbletea v0.13.1/go.mod h1:tp9tr9Dadh0PLhgiwchE5zZJXm5543JYjHG9oY+5qSg=
github.com/containerd/console v1.0.1 h1:u7SFAJyRqWcG6ogaMAx3KjSTy1e3hT9QxqX7Jco7dRc=
github.com/containerd/console v1.0.1/go.mod h1:XUsP6YE/mKtz6bxc+I8UiKKTP04qjQL4qcS3XoQ5xkw=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f/go.mod h1:nOFQdrUlIlx6M6ODdSpBj1NVA+VgLC6kmw60mkw34H4=
github.com/kelvins/sunrisesunset v0.0.0-20210220141756-39fa1bd816d5 h1:ouekCqYkMw4QXFCaLyYqjBe99/MUW4Qf3DJhCRh1G18=
github.com/kelvins/sunrisesunset v0.0.0-20210220141756-39fa1bd816d5/go.mod h1:3oZ7G+fb8Z8KF+KPHxeDO3GWpEjgvk/f+d/yaxmDRT4=
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
Expand All @@ -18,9 +23,12 @@ github.com/muesli/termenv v0.8.0 h1:HxhDOZbpf9nSUhOiMUNk5jsL3XaKWAbHucoGYZf+nag=
github.com/muesli/termenv v0.8.0/go.mod h1:kzt/D/4a88RoheZmwfqorY3A+tnsSMA9HJC/fQSFKo0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee h1:4yd7jl+vXjalO5ztz6Vc1VADv+S/80LGJmyl1ROJ2AI=
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
Expand All @@ -32,3 +40,5 @@ golang.org/x/sys v0.0.0-20200916030750-2334cc1a136f/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634 h1:bNEHhJCnrwMKNMmOx3yAynp5vs5/gRy+XWFtZFu7NBM=
golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
17 changes: 14 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,21 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, nil
}

var clock func() time.Time = time.Now

func main() {
now := time.Now()
exitQuick := flag.Bool("q", false, "exit immediately")
when := flag.Int64("when", 0, "time in seconds since unix epoch")
flag.Parse()

if *when != 0 {
t := time.Unix(*when, 0)
clock = func() time.Time {
return t
}
}

now := clock()
config, err := LoadConfig()
if err != nil {
fmt.Fprintf(os.Stderr, "Config error: %s\n", err)
Expand All @@ -105,8 +118,6 @@ func main() {
showDates: false,
}

exitQuick := flag.Bool("q", false, "exit immediately")
flag.Parse()
initialModel.interactive = !*exitQuick

p := tea.NewProgram(initialModel)
Expand Down
22 changes: 8 additions & 14 deletions view.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ func (m model) View() string {
}

dateChanged := false
c := zone.LightCycle()
for i := startHour; i < startHour+24; i++ {
hour := ((i % 24) + 24) % 24 // mod 24
out := termenv.String(fmt.Sprintf("%2d", hour))

out = out.Foreground(term.Color(hourColorCode(hour)))
out = out.Foreground(term.Color(hourColorCode(hour, c)))
// Cursor
if m.hour == i-startHour {
out = out.Background(term.Color("#00B67F"))
Expand Down Expand Up @@ -117,40 +118,33 @@ func formatDayChange(m *model, z *Zone) string {
}

// Return a color matching the time of the day at a given hour.
func hourColorCode(hour int) (color string) {
switch hour {
// Morning
case 7, 8:
func hourColorCode(hour int, c LightCycle) (color string) {
if _, ok := c.morning[hour]; ok {
if hasDarkBackground {
color = "#98E1D8"
} else {
color = "#35B6A6"
}

// Day
case 9, 10, 11, 12, 13, 14, 15, 16, 17:
} else if _, ok := c.day[hour]; ok {
if hasDarkBackground {
color = "#E8C64D"
} else {
color = "#FA8F2D"
}

// Evening
case 18, 19:
} else if _, ok := c.evening[hour]; ok {
if hasDarkBackground {
color = "#C95F48"
} else {
color = "#FC6442"
}

// Night
default:
} else if _, ok := c.night[hour]; ok {
if hasDarkBackground {
color = "#5957C9"
} else {
color = "#664FC3"
}
}

return color
}

Expand Down
98 changes: 96 additions & 2 deletions zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
**/
package main

import "time"
import (
"strconv"
"time"

"github.com/WIZARDISHUNGRY/tzcoords"
"github.com/kelvins/sunrisesunset"
)

var name, offset = time.Now().Zone()
var DefaultZones = []*Zone{
Expand Down Expand Up @@ -71,7 +77,7 @@ func (z Zone) ShortDT() string {
}

func (z Zone) currentTime() time.Time {
now := time.Now()
now := clock()
zName, _ := now.Zone()
if z.DbName != zName {
loc, err := time.LoadLocation(z.DbName)
Expand All @@ -82,3 +88,91 @@ func (z Zone) currentTime() time.Time {
}
return now
}

// SunriseSunset returns the sunrise and sunset for a zone
func (z Zone) SunriseSunset() (time.Time, time.Time, error) {
lat, lon, err := tzcoords.ByString(z.DbName)
now := z.currentTime()
if err != nil {
return now, now, err
}
utc, err := strconv.Atoi(now.Format("-0700"))
if err != nil {
return now, now, err
}
utcFloat := float64(utc) / 100.0
p := sunrisesunset.Parameters{
Latitude: lat,
Longitude: lon,
UtcOffset: utcFloat,
Date: now,
}
return p.GetSunriseSunset()
}

// LightCycle returns a color schedule
func (z Zone) LightCycle() LightCycle {
rise, set, err := z.SunriseSunset()
if err != nil {
return defaultHourHashes()
}
c := LightCycle{
morning: make(map[int]struct{}),
day: make(map[int]struct{}),
evening: make(map[int]struct{}),
night: make(map[int]struct{}),
}
var next map[int]struct{}
for hour := 0; hour < 24; hour++ {
if hour < rise.Hour() {
next = c.night
} else if hour == rise.Hour() {
next = c.morning
} else if hour > rise.Hour() {
if hour < set.Hour() {
next = c.day
} else if hour == set.Hour() {
next = c.evening
} else {
next = c.night
}
}
next[hour] = struct{}{}
}
return c
}

// LightCycle defines the different periods of the day
type LightCycle struct {
morning, day, evening, night map[int]struct{}
}

func defaultHourHashes() LightCycle {
c := LightCycle{
morning: make(map[int]struct{}),
day: make(map[int]struct{}),
evening: make(map[int]struct{}),
night: make(map[int]struct{}),
}
for hour := 0; hour < 24; hour++ {
switch hour {
// Morning
case 7, 8:
c.morning[hour] = struct{}{}

// Day
case 9, 10, 11, 12, 13, 14, 15, 16, 17:
c.day[hour] = struct{}{}

// Evening
case 18, 19:
c.evening[hour] = struct{}{}

// Night
default:
c.night[hour] = struct{}{}

}
}
return c
}