-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
135 lines (120 loc) · 3.37 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
131
132
133
134
135
package main
import (
"bufio"
"flag"
"fmt"
"os"
"sort"
"strconv"
"strings"
"time"
)
const version = "0.3.6"
func printUsage() {
fmt.Fprintln(os.Stderr, "Usage")
fmt.Fprintln(os.Stderr, " timeconverter [options] <command>")
fmt.Fprintln(os.Stderr, "Version")
fmt.Fprintln(os.Stderr, " "+version)
fmt.Fprintln(os.Stderr, "Options")
flag.PrintDefaults()
fmt.Fprintln(os.Stderr, "Commands")
fmt.Fprintln(os.Stderr, " help -> show this help")
fmt.Fprintln(os.Stderr, " version -> print version number and exit")
fmt.Fprintln(os.Stderr, " now -> print current timestamp for location and format")
fmt.Fprintln(os.Stderr, " <value> -> string with timestamps in it")
fmt.Fprintln(os.Stderr, " - -> pipe input with timestamps from stdin")
}
func getBuiltInFormatKeys() []string {
keys := make([]string, 0)
for key := range TimeFormats {
keys = append(keys, key)
}
return keys
}
func getLocationAliasKeys() []string {
keys := make([]string, 0)
for key := range LocationAliases {
keys = append(keys, key)
}
return keys
}
func getFormat(formatString string) string {
builtInFormat, ok := TimeFormats[formatString]
if ok {
return builtInFormat
}
return formatString
}
func getLocation(locationString string) *time.Location {
locationFromAlias, ok := LocationAliases[locationString]
if ok {
location, err := time.LoadLocation(locationFromAlias)
if err == nil {
return location
}
panic(err.Error())
} else {
location, err := time.LoadLocation(locationString)
if err == nil {
return location
}
panic(err.Error())
}
}
func main() {
builtinFormatKeys := getBuiltInFormatKeys()
sort.Strings(builtinFormatKeys)
locationPtr := flag.String("location", "Local", "tzdata location to convert to (aliases \""+strings.Join(getLocationAliasKeys(), "\", \"")+"\")")
formatPtr := flag.String("format", "Mon 2006 Jan 02 03:04pm MST", "format to use (options \""+strings.Join(builtinFormatKeys, "\", \"")+"\"")
versionPtr := flag.Bool("version", false, "print version number and exit")
typePtr := flag.String("type", "iso8601", "what type of timestamps in the input (options \"iso8601\", \"unix\")")
flag.Parse()
if *versionPtr {
fmt.Println(version)
os.Exit(0)
}
arg := flag.Arg(0)
switch arg {
case "", "h", "-h", "--h", "/h", "/?", "help", "-help", "--help", "/help":
printUsage()
os.Exit(1)
case "version", "-version", "--version", "/version":
fmt.Println(version)
os.Exit(0)
case "-":
// pipe
loc := getLocation(*locationPtr)
format := getFormat(*formatPtr)
scanner := bufio.NewScanner(os.Stdin)
switch *typePtr {
case "unix":
replacer := NewUnixTimestampReplacer()
for scanner.Scan() {
fmt.Println(replacer.ReplaceDates(scanner.Text(), format, loc))
}
default:
replacer := NewIso8601Replacer()
for scanner.Scan() {
fmt.Println(replacer.ReplaceDates(scanner.Text(), format, loc))
}
}
if err := scanner.Err(); err != nil {
panic(err.Error())
}
default:
loc := getLocation(*locationPtr)
format := getFormat(*formatPtr)
switch {
case arg == "now":
if format == "UnixSeconds" {
fmt.Println(strconv.FormatInt(time.Now().Unix(), 10))
} else {
fmt.Println(time.Now().In(loc).Format(format))
}
case *typePtr == "unix":
fmt.Println(NewUnixTimestampReplacer().ReplaceDates(arg, format, loc))
default:
fmt.Println(NewIso8601Replacer().ReplaceDates(arg, format, loc))
}
}
}