-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
130 lines (105 loc) · 2.86 KB
/
main.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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"time"
)
var progName string
func errMsg(err error, message string) {
if err != nil {
fmt.Printf("%s: %s\n", message, err)
os.Exit(1)
}
}
func getTz(filePath, timezoneFlag string) ([]string, error) {
if timezoneFlag != "" {
timezoneFlag = strings.Replace(timezoneFlag, "UTC-0", "UTC", 1)
return []string{timezoneFlag}, nil
}
tzFromEnv := os.Getenv("TZ")
if tzFromEnv != "" {
return []string{tzFromEnv}, nil
}
if filePath == "" {
filePath = getConfigPath()
}
return readTzFile(filePath)
}
func getConfigPath() string {
xdgConfigHome := os.Getenv("XDG_CONFIG_HOME")
homeDir, err := os.UserHomeDir()
errMsg(err, "failed to get user home directory")
configPath := filepath.Join(homeDir, ".config", "twc", "tz.conf")
if xdgConfigHome != "" {
configPath = filepath.Join(xdgConfigHome, "twc", "tz.conf")
}
return configPath
}
func readTzFile(filePath string) ([]string, error) {
fileContent, err := os.ReadFile(filePath)
if err != nil {
return []string{"UTC"}, nil
}
re := regexp.MustCompile(`(?m)^[ \t]*(#.*|\n)`)
fileContent = re.ReplaceAll(fileContent, []byte{})
fileContent = []byte(strings.ReplaceAll(string(fileContent), "UTC-0", "UTC"))
var timezones []string
for _, line := range strings.Split(string(fileContent), "\n") {
line = strings.TrimSpace(line)
if line != "" && !strings.HasPrefix(line, "#") {
timezones = append(timezones, line)
}
}
return timezones, nil
}
func usage() {
usage := `usage: %s [-h] [-f path] [-s format] [-t timezone] ...
Options:
-f path
Read config from path (default "$HOME/.config/twc/tz.conf")
-h Print in human-readable format
-s format
Set desired time format (e.g. "%%Y-%%m-%%d")
-t timezone
Set a specific timezone (e.g. "Asia/Tokyo")
`
usageStr := fmt.Sprintf(usage, progName)
fmt.Fprint(flag.CommandLine.Output(), usageStr)
}
func main() {
progName = filepath.Base(os.Args[0])
flag.Usage = usage
showHumanRead := flag.Bool("h", false, "Print human-readable format")
formatSpecifier := flag.String("s", time.RFC3339, "Specify time format")
filePath := flag.String("f", "", "Specify timezone file")
timezoneFlag := flag.String("t", "", "Specify timezone directly")
flag.Parse()
format := *formatSpecifier
if *showHumanRead {
format = "2006-01-02 15:04:05"
}
timezones, err := getTz(*filePath, *timezoneFlag)
errMsg(err, "failed to get timezones")
maxWidth := 0
for _, tz := range timezones {
tz = strings.TrimSpace(tz)
if len(tz) > maxWidth {
maxWidth = len(tz)
}
}
maxWidth++
for _, tz := range timezones {
tz = strings.TrimSpace(tz)
loc, err := time.LoadLocation(tz)
if err != nil {
fmt.Printf("error loading timezone %s: %s\n", tz, err)
continue
}
tzTime := time.Now().UTC().In(loc)
fmt.Printf("%-*s %s\n", maxWidth, tz, tzTime.Format(format))
}
}