-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
106 lines (89 loc) · 1.68 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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"time"
)
var now = time.Now()
var timeformat = time.RFC1123
var timejsonurl = "https://m120.github.io/timezone-json/timezone.json"
type result struct {
Timezones []struct {
TZ string `json:"tz"`
} `json:"timezones"`
}
func flagUsage() {
usageText := `
i18n timezone
Usage:
-------------------------------------------
- Default(No ARG): "Local & GMT"
$ go run main.go
- i18n: World Timezone List
$ go run main.go i18n
- "{TZ}": Specified Timezone
$ go run main.go "{TZ}"
- Ex: "America/Chicago"
$ go run main.go America/Chicago
- help: This message. :-)
$ go run main.go help
`
fmt.Fprintf(os.Stderr, "%s\n\n", usageText)
}
func localtime() {
fmt.Println(now.Format(timeformat), "\t:", now.Location())
}
func loadlocation(tz string) {
loc, _ := time.LoadLocation(tz)
nowloc := now.In(loc)
fmt.Println(nowloc.Format(timeformat), "\t:", loc)
}
func tz(x string) {
switch x {
case "localgmt":
// Local
localtime()
// UTC(GMT)
loadlocation("Etc/UTC")
case "i18n":
// i18n: json get
resp, err := http.Get(timejsonurl)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if err != nil {
log.Fatal(err)
}
respbody, err := ioutil.ReadAll(resp.Body)
var tzd result
json.Unmarshal(respbody, &tzd)
for _, tzs := range tzd.Timezones {
loadlocation(tzs.TZ)
}
default:
localtime()
loadlocation(x)
}
}
func main() {
flag.Usage = flagUsage
if len(os.Args) > 1 {
osargs := os.Args[1]
switch osargs {
case "i18n":
tz("i18n")
case "help":
flag.Usage()
default:
tz(osargs)
}
} else {
tz("localgmt")
}
}