1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Timezone labels supported

This commit is contained in:
Chris Cummer 2018-04-15 15:08:57 -07:00 committed by Chris Cummer
parent 0b20c4939e
commit f0caaf3890
4 changed files with 24 additions and 12 deletions

View File

@ -4,16 +4,17 @@ import (
"time"
)
func Timezones(zones []string) []time.Time {
times := []time.Time{}
func Timezones(timezones map[string]interface{}) map[string]time.Time {
times := make(map[string]time.Time)
for label, timezone := range timezones {
tzloc, err := time.LoadLocation(timezone.(string))
for _, zone := range zones {
loc, err := time.LoadLocation(zone)
if err != nil {
continue
}
times = append(times, time.Now().In(loc))
times[label] = time.Now().In(tzloc)
}
return times

View File

@ -2,6 +2,7 @@ package status
import (
"fmt"
"sort"
"strings"
"time"
@ -61,16 +62,25 @@ func (widget *Widget) animation() string {
}
func (widget *Widget) timezones() string {
times := Timezones(wtf.ToStrs(Config.UList("wtf.mods.status.timezones")))
timezones := Timezones(Config.UMap("wtf.mods.status.timezones"))
if len(times) == 0 {
if len(timezones) == 0 {
return ""
}
formattedTimes := []string{}
for _, time := range times {
formattedTimes = append(formattedTimes, time.Format(wtf.TimeFormat))
// All this is to display the time entries in alphabetical order
labels := []string{}
for label, _ := range timezones {
labels = append(labels, label)
}
return strings.Join(formattedTimes, " • ")
sort.Strings(labels)
tzs := []string{}
for _, label := range labels {
zoneStr := fmt.Sprintf("%s %s", label, timezones[label].Format(wtf.TimeFormat))
tzs = append(tzs, zoneStr)
}
return strings.Join(tzs, " ◦ ")
}

View File

@ -28,6 +28,7 @@ func LoadConfigFile(filePath string) *config.Config {
cfg, err := config.ParseYamlFile(absPath)
if err != nil {
fmt.Println("\n\n\033[1m ERROR:\033[0m Could not load '\033[0;33mconfig.yml\033[0m'.\n Please add a \033[0;33mconfig.yml\033[0m file to your \033[0;33m~/.wtf\033[0m directory.\n See \033[1;34mhttps://github.com/senorprogrammer/wtf\033[0m for details.\n\n")
fmt.Println(err.Error())
os.Exit(1)
}

View File

@ -10,7 +10,7 @@ import (
// DateFormat defines the format we expect to receive dates from BambooHR in
const DateFormat = "2006-01-02"
const TimeFormat = "15:04 MST"
const TimeFormat = "15:04"
func CenterText(str string, width int) string {
return fmt.Sprintf("%[1]*s", -width, fmt.Sprintf("%[1]*s", (width+len(str))/2, str))