-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
83 lines (70 loc) · 1.66 KB
/
main.go
File metadata and controls
83 lines (70 loc) · 1.66 KB
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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os/user"
"sort"
"time"
)
type config struct {
Zones map[string]string `json:"zones"`
ReferenceTime string `json:"reference_time"`
}
type result struct {
name string
zone string
time time.Time
}
func getConfig() (config, error) {
locationList := config{}
user, err := user.Current()
if err != nil {
return locationList, fmt.Errorf("error getting current user: %s", err)
}
locData, err := ioutil.ReadFile(user.HomeDir + "/.worldclock.json")
if err != nil {
return locationList, fmt.Errorf("error reading worldclock.json file: %s", err)
}
if err := json.Unmarshal(locData, &locationList); err != nil {
return locationList, fmt.Errorf("error parsing location map: %s", err)
}
return locationList, nil
}
func main() {
ref := flag.String("a", "", "print time using reference date instead of now")
flag.Parse()
config, err := getConfig()
if err != nil {
log.Fatal(err)
}
var rt time.Time
if *ref != "" {
timeLayout := config.ReferenceTime
var err error
rt, err = time.Parse(timeLayout, *ref)
if err != nil {
log.Fatal("error parsing ref time: ", err)
}
} else {
rt = time.Now()
}
output := make([]result, 0)
for name, l := range config.Zones {
loc, err := time.LoadLocation(l)
if err != nil {
log.Fatal("error loading timezone: ", err)
}
output = append(output, result{name, l, rt.In(loc).Truncate(1 * time.Second)})
}
sort.Slice(output, func(i, j int) bool {
_, oi := output[i].time.Zone()
_, oj := output[j].time.Zone()
return oi < oj
})
for _, t := range output {
fmt.Printf("%-10s %-20s %s\n", t.name, t.zone, t.time)
}
}