Skip to content

Commit 51aa7e5

Browse files
author
Jérôme Renard
committed
Added alternative timestamp formats for rfc3164.
Fixes #1
1 parent cb5d0b7 commit 51aa7e5

File tree

2 files changed

+43
-22
lines changed

2 files changed

+43
-22
lines changed

rfc3164/rfc3164.go

+23-11
Original file line numberDiff line numberDiff line change
@@ -124,20 +124,32 @@ func (p *Parser) parsemessage() (rfc3164message, error) {
124124
func (p *Parser) parseTimestamp() (time.Time, error) {
125125
var ts time.Time
126126
var err error
127+
var tsFmtLen int
128+
var sub []byte
127129

128-
tsFmt := "Jan 02 15:04:05"
129-
// len(fmt)
130-
tsFmtLen := 15
130+
tsFmts := []string{
131+
"Jan 02 15:04:05",
132+
"Jan 2 15:04:05",
133+
}
134+
135+
found := false
136+
for _, tsFmt := range tsFmts {
137+
tsFmtLen = len(tsFmt)
138+
139+
if p.cursor+tsFmtLen > p.l {
140+
continue
141+
}
131142

132-
if p.cursor+tsFmtLen > p.l {
133-
p.cursor = p.l
134-
return ts, syslogparser.ErrEOL
143+
sub = p.buff[p.cursor : tsFmtLen+p.cursor]
144+
ts, err = time.Parse(tsFmt, string(sub))
145+
if err == nil {
146+
found = true
147+
break
148+
}
135149
}
136150

137-
sub := p.buff[p.cursor : tsFmtLen+p.cursor]
138-
ts, err = time.Parse(tsFmt, string(sub))
139-
if err != nil {
140-
p.cursor = len(sub)
151+
if !found {
152+
p.cursor = tsFmtLen
141153

142154
// XXX : If the timestamp is invalid we try to push the cursor one byte
143155
// XXX : further, in case it is a space
@@ -150,7 +162,7 @@ func (p *Parser) parseTimestamp() (time.Time, error) {
150162

151163
fixTimestampIfNeeded(&ts)
152164

153-
p.cursor += 15
165+
p.cursor += tsFmtLen
154166

155167
if (p.cursor < p.l) && (p.buff[p.cursor] == ' ') {
156168
p.cursor++

rfc3164/rfc3164_test.go

+20-11
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ func Test(t *testing.T) { TestingT(t) }
1414
type Rfc3164TestSuite struct {
1515
}
1616

17-
var _ = Suite(&Rfc3164TestSuite{})
17+
var (
18+
_ = Suite(&Rfc3164TestSuite{})
19+
20+
// XXX : corresponds to the length of the last tried timestamp format
21+
// XXX : Jan 2 15:04:05
22+
lastTriedTimestampLen = 14
23+
)
1824

1925
func (s *Rfc3164TestSuite) TestParser_Valid(c *C) {
2026
buff := []byte("<34>Oct 11 22:14:15 mymachine su: 'su root' failed for lonvick on /dev/pts/8")
@@ -62,7 +68,7 @@ func (s *Rfc3164TestSuite) TestParseHeader_InvalidTimestamp(c *C) {
6268
buff := []byte("Oct 34 32:72:82 mymachine ")
6369
hdr := header{}
6470

65-
s.assertRfc3164Header(c, hdr, buff, 16, syslogparser.ErrTimestampUnknownFormat)
71+
s.assertRfc3164Header(c, hdr, buff, lastTriedTimestampLen, syslogparser.ErrTimestampUnknownFormat)
6672
}
6773

6874
func (s *Rfc3164TestSuite) TestParsemessage_Valid(c *C) {
@@ -76,19 +82,11 @@ func (s *Rfc3164TestSuite) TestParsemessage_Valid(c *C) {
7682
s.assertRfc3164message(c, hdr, buff, len(buff), syslogparser.ErrEOL)
7783
}
7884

79-
func (s *Rfc3164TestSuite) TestParseTimestamp_TooLong(c *C) {
80-
// XXX : <15 chars
81-
buff := []byte("aaa")
82-
ts := new(time.Time)
83-
84-
s.assertTimestamp(c, *ts, buff, len(buff), syslogparser.ErrEOL)
85-
}
86-
8785
func (s *Rfc3164TestSuite) TestParseTimestamp_Invalid(c *C) {
8886
buff := []byte("Oct 34 32:72:82")
8987
ts := new(time.Time)
9088

91-
s.assertTimestamp(c, *ts, buff, len(buff), syslogparser.ErrTimestampUnknownFormat)
89+
s.assertTimestamp(c, *ts, buff, lastTriedTimestampLen, syslogparser.ErrTimestampUnknownFormat)
9290
}
9391

9492
func (s *Rfc3164TestSuite) TestParseTimestamp_TrailingSpace(c *C) {
@@ -102,6 +100,17 @@ func (s *Rfc3164TestSuite) TestParseTimestamp_TrailingSpace(c *C) {
102100
s.assertTimestamp(c, ts, buff, len(buff), nil)
103101
}
104102

103+
func (s *Rfc3164TestSuite) TestParseTimestamp_OneDigitForMonths(c *C) {
104+
// XXX : no year specified. Assumed current year
105+
// XXX : no timezone specified. Assume UTC
106+
buff := []byte("Oct 1 22:14:15")
107+
108+
now := time.Now()
109+
ts := time.Date(now.Year(), time.October, 1, 22, 14, 15, 0, time.UTC)
110+
111+
s.assertTimestamp(c, ts, buff, len(buff), nil)
112+
}
113+
105114
func (s *Rfc3164TestSuite) TestParseTimestamp_Valid(c *C) {
106115
// XXX : no year specified. Assumed current year
107116
// XXX : no timezone specified. Assume UTC

0 commit comments

Comments
 (0)