-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP]beats @timestamp support nanoseconds #9818
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,21 +21,37 @@ import ( | |
"encoding/binary" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"hash" | ||
"time" | ||
) | ||
|
||
// TsLayout is the layout to be used in the timestamp marshaling/unmarshaling everywhere. | ||
// The timezone must always be UTC. | ||
const TsLayout = "2006-01-02T15:04:05.000Z" | ||
const ( | ||
// TsLayout is the seconds layout to be used in the timestamp marshaling/unmarshaling everywhere. | ||
// The timezone must always be UTC. | ||
TsLayout = "2006-01-02T15:04:05" | ||
) | ||
|
||
// Time is an abstraction for the time.Time type | ||
type Time time.Time | ||
|
||
func (t Time) generateTsLayout() string { | ||
nanoTime := time.Time(t).UTC().UnixNano() | ||
trailZero := "000000000" | ||
for i := 0; i < 2; i++ { | ||
if nanoTime%1000 != 0 { | ||
break | ||
} | ||
trailZero = trailZero[:len(trailZero)-3] | ||
nanoTime = nanoTime / 1000 | ||
} | ||
return fmt.Sprintf("%s.%sZ", TsLayout, trailZero) | ||
} | ||
|
||
// MarshalJSON implements json.Marshaler interface. | ||
// The time is a quoted string in the JsTsLayout format. | ||
func (t Time) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(time.Time(t).UTC().Format(TsLayout)) | ||
return json.Marshal(time.Time(t).UTC().Format(t.generateTsLayout())) | ||
} | ||
|
||
// UnmarshalJSON implements js.Unmarshaler interface. | ||
|
@@ -54,14 +70,28 @@ func (t Time) Hash32(h hash.Hash32) error { | |
return err | ||
} | ||
|
||
// ParseTime parses a time in the TsLayout format. | ||
// ParseTime parses a time in the NanoTsLayout format first, then use millisTsLayout format | ||
func ParseTime(timespec string) (Time, error) { | ||
t, err := time.Parse(TsLayout, timespec) | ||
var ( | ||
t time.Time | ||
err error | ||
tsLayout string | ||
trailZero string | ||
) | ||
|
||
for i := 0; i < 3; i++ { | ||
trailZero += "000" | ||
tsLayout = fmt.Sprintf("%s.%sZ", TsLayout, trailZero) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
t, err = time.Parse(tsLayout, timespec) | ||
if err == nil { | ||
break | ||
} | ||
} | ||
return Time(t), err | ||
} | ||
|
||
func (t Time) String() string { | ||
return time.Time(t).Format(TsLayout) | ||
return time.Time(t).Format(t.generateTsLayout()) | ||
} | ||
|
||
// MustParseTime is a convenience equivalent of the ParseTime function | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,7 @@ type unpaddedNumber struct { | |
|
||
type paddedNumber struct { | ||
ft fieldType | ||
div int | ||
divExp int | ||
minDigits, maxDigits int | ||
signed bool | ||
} | ||
|
@@ -123,12 +123,9 @@ func numRequires(c *ctxConfig, ft fieldType) error { | |
ftSecondOfMinute: | ||
c.enableClock() | ||
|
||
case ftMillisOfDay: | ||
c.enableClock() | ||
c.enableMillis() | ||
case ftNanoOfSecond: | ||
c.enableNano() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ftMillisX should be removed from the enum if they are not used anymore. |
||
|
||
case ftMillisOfSecond: | ||
c.enableMillis() | ||
} | ||
|
||
return nil | ||
|
@@ -191,10 +188,10 @@ func (n unpaddedNumber) compile() (prog, error) { | |
} | ||
|
||
func (n paddedNumber) compile() (prog, error) { | ||
if n.div == 0 { | ||
if n.divExp == 0 { | ||
return makeProg(opNumPadded, byte(n.ft), byte(n.maxDigits)) | ||
} | ||
return makeProg(opExtNumPadded, byte(n.ft), byte(n.div), byte(n.maxDigits)) | ||
return makeProg(opExtNumPadded, byte(n.ft), byte(n.divExp), byte(n.maxDigits)) | ||
} | ||
|
||
func (n twoDigitYear) compile() (prog, error) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like the git 3-way-merge did mess with the Changelog :)
I did skim code for other potential merge failures, but it looks all good.