Skip to content

Commit 272655c

Browse files
committed
Add support for local timezones in RFC3164 parsing
Fixes issue #6 Signed-off-by: Alex Bligh <[email protected]>
1 parent 812866d commit 272655c

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

rfc3164/rfc3164.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type Parser struct {
1414
version int
1515
header header
1616
message rfc3164message
17+
location *time.Location
1718
}
1819

1920
type header struct {
@@ -28,12 +29,17 @@ type rfc3164message struct {
2829

2930
func NewParser(buff []byte) *Parser {
3031
return &Parser{
31-
buff: buff,
32-
cursor: 0,
33-
l: len(buff),
32+
buff: buff,
33+
cursor: 0,
34+
l: len(buff),
35+
location: time.UTC,
3436
}
3537
}
3638

39+
func (p *Parser) Location(location *time.Location) {
40+
p.location = location
41+
}
42+
3743
func (p *Parser) Parse() error {
3844
pri, err := p.parsePriority()
3945
if err != nil {
@@ -137,7 +143,7 @@ func (p *Parser) parseTimestamp() (time.Time, error) {
137143
}
138144

139145
sub = p.buff[p.cursor : tsFmtLen+p.cursor]
140-
ts, err = time.Parse(tsFmt, string(sub))
146+
ts, err = time.ParseInLocation(tsFmt, string(sub), p.location)
141147
if err == nil {
142148
found = true
143149
break

rfc5424/rfc5424.go

+4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ func NewParser(buff []byte) *Parser {
7676
}
7777
}
7878

79+
func (p *Parser) Location(location *time.Location) {
80+
// Ignore as RFC5424 syslog always has a timezone
81+
}
82+
7983
func (p *Parser) Parse() error {
8084
hdr, err := p.parseHeader()
8185
if err != nil {

syslogparser.go

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"strconv"
66
"strings"
7+
"time"
78
)
89

910
const (
@@ -32,6 +33,7 @@ var (
3233
type LogParser interface {
3334
Parse() error
3435
Dump() LogParts
36+
Location(*time.Location)
3537
}
3638

3739
type ParserError struct {

0 commit comments

Comments
 (0)