forked from robpike/now
/
now.go
103 lines (96 loc) · 2.5 KB
/
now.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
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Now tells what time it is in other time zones. The first argument
// identifies a time zone either by shorthand (EST, NYC) or by time zone
// file base name, such as Yellowknife or Paris.
//
// % now Paris
// Thu Apr 12 15:55:55 CEST 2012 Paris
// % now Adelaide
// Thu Apr 12 23:26:14 CST 2012 Adelaide
// %
package main
import (
"fmt"
"os"
"path/filepath"
"time"
)
func main() {
zone := ""
t := time.Now()
if len(os.Args) > 1 {
zone = os.Args[1]
if tz, ok := timeZone[zone]; ok {
zone = tz
} else if tz, ok = timeZone[toUpper(zone)]; ok {
zone = tz
}
t = t.In(loadZone(zone))
}
fmt.Printf("%s %s\n", t.Format(time.UnixDate), zone)
}
func loadZone(zone string) *time.Location {
loc, err := time.LoadLocation(zone)
if err == nil {
return loc
}
// Pure ASCII, but OK. Allow us to say "paris" as well as "Paris".
if len(zone) > 0 && 'a' <= zone[0] && zone[0] <= 'z' {
zone = string(zone[0]+'A'-'a') + string(zone[1:])
}
// See if there's a file with that name in /usr/share/zoneinfo
files, _ := filepath.Glob("/usr/share/zoneinfo/*/" + zone)
if len(files) >= 1 {
if len(files) > 1 {
fmt.Fprintf(os.Stderr, "now: multiple time zones; using first of %v\n", files)
}
loc, err = time.LoadLocation(files[0][len("/usr/share/zoneinfo/"):])
if err == nil {
return loc
}
}
fmt.Fprintf(os.Stderr, "now: %s\n", err)
os.Exit(1)
return nil
}
// Pure ASCII
func toUpper(s string) string {
var b = make([]byte, len(s))
for i := range b {
c := s[i]
if 'a' <= c && c <= 'z' {
c -= ' '
}
b[i] = c
}
return string(b)
}
// from /usr/share/zoneinfo
var timeZone = map[string]string{
"GMT": "Europe/London",
"BST": "Europe/London",
"BSDT": "Europe/London",
"CET": "Europe/Paris",
"UTC": "",
"PST": "America/Los_Angeles",
"PDT": "America/Los_Angeles",
"LA": "America/Los_Angeles",
"LAX": "America/Los_Angeles",
"MST": "America/Denver",
"MDT": "America/Denver",
"CST": "America/Chicago",
"CDT": "America/Chicago",
"Chicago": "America/Chicago",
"EST": "America/New_York",
"EDT": "America/New_York",
"NYC": "America/New_York",
"NY": "America/New_York",
"AEST": "Australia/Sydney",
"AEDT": "Australia/Sydney",
"AWST": "Australia/Perth",
"AWDT": "Australia/Perth",
"ACST": "Australia/Adelaide",
"ACDT": "Australia/Adelaide",
}