Skip to content

Commit

Permalink
Parse TZ_LIST env. var to show zones
Browse files Browse the repository at this point in the history
  • Loading branch information
oz committed Feb 6, 2021
1 parent 092a39a commit 7782dab
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 26 deletions.
44 changes: 42 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,55 @@
**/
package main

import (
"fmt"
"os"
"strings"
"time"
)

// Config stores app configuration
type Config struct {
Zones []Zone
}

// LoadConfig from os.UserConfigDir
func LoadConfig() (*Config, error) {
cfg := Config{
conf := Config{
Zones: DefaultZones,
}
return &cfg, nil

zoneEnv := os.Getenv("TZ_LIST")
if zoneEnv == "" {
return &conf, nil
}
zoneNames := strings.Split(zoneEnv, ",")
if len(zoneNames) == 0 {
return &conf, nil
}
zones := make([]Zone, len(zoneNames)+1)

now := time.Now()
localZoneName, offset := now.Zone()

zones[0] = Zone{
Name: fmt.Sprintf("(%s) Local", localZoneName),
DbName: localZoneName,
Offset: offset / 3600,
}
for i, name := range zoneNames {
loc, err := time.LoadLocation(name)
if err != nil {
return nil, fmt.Errorf("looking up zone %s: %w", name, err)
}
then := now.In(loc)
shortName, offset := then.Zone()
zones[i+1] = Zone{
DbName: loc.String(),
Name: fmt.Sprintf("(%s) %s", shortName, loc),
Offset: offset / 3600,
}
}
conf.Zones = zones
return &conf, nil
}
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (m model) View() string {
hours := strings.Builder{}
startAt := 0
if zi > 0 {
startAt = (z.offset - m.zones[0].offset) % 24
startAt = (z.Offset - m.zones[0].Offset) % 24
}

// A list of hours
Expand Down Expand Up @@ -125,7 +125,7 @@ func main() {
now := time.Now()
config, err := LoadConfig()
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading config: %s", err)
fmt.Fprintf(os.Stderr, "Config error: %s\n", err)
os.Exit(2)
}
var initialModel = model{
Expand Down
34 changes: 12 additions & 22 deletions zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,14 @@ import "time"
var name, offset = time.Now().Zone()
var DefaultZones = []Zone{
{
name: "Local",
dbName: name,
offset: offset / 3600,
Name: "Local",
DbName: name,
Offset: offset / 3600,
},
{
name: "Paris",
dbName: "Europe/Paris",
offset: 1,
},
{
name: "New-York",
dbName: "America/New_York",
offset: -5,
},
{
name: "UTC",
dbName: "UTC",
offset: 0,
Name: "UTC",
DbName: "UTC",
Offset: 0,
},
}

Expand All @@ -60,13 +50,13 @@ var EmojiClocks = map[int]string{

// Zone stores the name of a time zone and its integer offset from UTC.
type Zone struct {
dbName string // tz db name
name string // custom name
offset int // Integer offset from UTC
DbName string // Name in tzdata
Name string // Short name
Offset int // Integer offset from UTC, in hours.
}

func (z Zone) String() string {
return z.name
return z.Name
}

// ClockEmoji returns the corresponding emoji clock for a given hour
Expand All @@ -83,8 +73,8 @@ func (z Zone) ShortDT() string {
func (z Zone) currentTime() time.Time {
now := time.Now()
zName, _ := now.Zone()
if z.dbName != zName {
loc, err := time.LoadLocation(z.dbName)
if z.DbName != zName {
loc, err := time.LoadLocation(z.DbName)
if err != nil {
return now
}
Expand Down

0 comments on commit 7782dab

Please sign in to comment.