-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
224 lines (208 loc) · 5.65 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
// cattz ('cat' + 'tz') reads files or stdin and converts timestamps from one timezone to another
// a common use case would be a log file with timestamps in UTC and converting those to your local timezone
import (
"bufio"
"bytes"
"flag"
"fmt"
"io"
"os"
"regexp"
"time"
"github.com/billhathaway/strftime"
)
type controller struct {
// srcLoc is TZ for input
srcLoc *time.Location
// destLoc is TZ for output
destLoc *time.Location
// format is output format in time.Format() style
format string
// re is a regular expression used to search for timestamps
re *regexp.Regexp
// buf is a buffer used when re-assembling output
buf *bytes.Buffer
// matchLimit is used to control how many times we try to find timestamps in a given line
// it will either be set to -1 (unlimited) or 1 (only handle first timestamp)
matchLimit int
// debug enables debugging output
debug bool
}
func (c *controller) debugf(format string, v ...interface{}) {
if c.debug {
fmt.Printf(format, v...)
}
}
// map between strftime conversions and regular expression components
var strfmtimeRE = map[byte]string{
'Y': `\d{4}`,
'm': `\d{2}`,
'b': `[A-Z][a-z]{2}`,
'd': `\d{2}`,
'a': `[A-Z][a-z]{2}`,
'e': `\d?\d`,
'H': `\d{2}`,
'M': `\d{2}`,
'I': `\d{2}`,
'p': `[AP]M`,
'P': `[ap]m`,
'r': `\d{2}:\d{2}:\d{2} [AP]M`,
'S': `\d{2}`,
'T': `\d{2}:\d{2}:\d{2}`,
'y': `\d{2}`,
'z': `(-|\+)\d{4}`,
'Z': `[A-Za-z_]+`,
'%': `%`,
}
// strftimeToRE is given a strftime() format and builds a regex for parsing
// %Y-%m-%d would turn into
// \d{4}-\d{2}-\d{2}
// TODO: check for completeness
func (c *controller) strftimeToRE(format string) error {
regexBuf := &bytes.Buffer{}
var inPercent bool
for i := range format {
ch := format[i]
if inPercent {
val, ok := strfmtimeRE[ch]
if !ok {
return fmt.Errorf("unsupported conversion char %c", ch)
}
regexBuf.WriteString(val)
inPercent = false
} else if ch == '%' {
inPercent = true
} else {
// deal with literal
regexBuf.WriteByte(ch)
}
}
c.debugf("strftime regexString=[%s]\n", regexBuf.String())
var err error
c.re, err = regexp.Compile(regexBuf.String())
return err
}
// replaceTimeOffset is passed a timestamp in a source TZ and dest TZ offset in hours
// currently used only for benchmark purposes
func (c *controller) replaceTimeOffset(token string, hoursOffset int) (string, error) {
source, err := time.ParseInLocation(c.format, token, c.srcLoc)
if err != nil {
return token, err
}
return source.Add(time.Duration(hoursOffset) * time.Hour).Format(c.format), nil
}
// replaceTime is passed a timestamp in a source TZ and returns the timestamp in the dest TZ
func (c *controller) replaceTime(token string) (string, error) {
source, err := time.ParseInLocation(c.format, token, c.srcLoc)
if err != nil {
return token, err
}
return source.In(c.destLoc).Format(c.format), nil
}
// replaceLine returns the line with all the timestamps converted
func (c *controller) replaceLine(line string) string {
matches := c.re.FindAllStringIndex(line, c.matchLimit)
if matches == nil {
c.debugf("DEBUG: no match for regex in line [%s]\n", line)
return line
}
c.buf.Reset()
offset := 0
for i, match := range matches {
c.debugf("in match %d match=[%v]\n", i, match)
prefix := line[offset:match[0]]
c.debugf("prefix=[%s]\n", prefix)
c.buf.WriteString(prefix)
token := line[match[0]:match[1]]
c.debugf("token=[%s]\n", token)
replaced, err := c.replaceTime(token)
if err != nil {
fmt.Printf("ERROR parsing orig=[%s] time: %s\n", token, err)
return line
}
c.buf.WriteString(replaced)
c.debugf("replaced=[%s]\n", replaced)
offset = match[1]
}
if offset < len(line) {
c.buf.WriteString(line[offset:])
}
return c.buf.String()
}
func (c *controller) parse(r io.Reader) {
s := bufio.NewScanner(r)
for s.Scan() {
line := s.Text()
fmt.Println(c.replaceLine(line))
}
}
func (c *controller) execute() int {
var errCount int
if len(flag.Args()) > 0 {
for _, file := range flag.Args() {
fh, err := os.Open(file)
if err != nil {
fmt.Fprintf(os.Stderr, "%s %s %s\n", os.Args[0], file, err)
errCount++
continue
}
c.parse(fh)
fh.Close()
}
return errCount
}
c.parse(os.Stdin)
return 0
}
func main() {
tz := "US/Pacific"
if cattz, ok := os.LookupEnv("CATZ"); ok {
tz = cattz
} else {
stdtz, ok := os.LookupEnv("TZ")
if ok {
tz = stdtz
}
}
srcTZ := flag.String("srctz", "UTC", "input time zone")
destTZ := flag.String("outtz", tz, "output time zone (defaults to $CATZ or $TZ env if available)")
debug := flag.Bool("d", false, "enable debug logging")
timeFormat := flag.String("t", "%Y-%m-%d %H", "strftime format")
first := flag.Bool("first", false, "only replace first timestamp match per line")
flag.Parse()
sourceLocation, err := time.LoadLocation(*srcTZ)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR input TZ %s not known\n", *srcTZ)
os.Exit(1)
}
destLocation, err := time.LoadLocation(*destTZ)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR output TZ %s not known\n", *destTZ)
os.Exit(1)
}
c := &controller{
srcLoc: sourceLocation,
destLoc: destLocation,
buf: &bytes.Buffer{},
debug: *debug,
}
// verify we know how to handle the time format
c.format, err = strftime.New(*timeFormat)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR time format [%s] had problem converting to go format%s\n", *timeFormat, err)
os.Exit(1)
}
err = c.strftimeToRE(*timeFormat)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR time format [%s] had problem to regex %s\n", *timeFormat, err)
os.Exit(1)
}
// only replace first timestamp occurance, or replace all
if *first {
c.matchLimit = 1
} else {
c.matchLimit = -1
}
os.Exit(c.execute())
}