diff --git a/transform/meta_for_each_test.go b/transform/meta_for_each_test.go index ac666667..96a00e9d 100644 --- a/transform/meta_for_each_test.go +++ b/transform/meta_for_each_test.go @@ -202,7 +202,7 @@ var metaForEachTests = []struct { }, []byte(`{"a":["2021-03-06T00:02:57Z","2021-03-06T00:03:57Z","2021-03-06T00:04:57Z"]}`), [][]byte{ - []byte(`{"a":["2021-03-06T00:02:57Z","2021-03-06T00:03:57Z","2021-03-06T00:04:57Z"],"b":["1614988977000","1614989037000","1614989097000"]}`), + []byte(`{"a":["2021-03-06T00:02:57Z","2021-03-06T00:03:57Z","2021-03-06T00:04:57Z"],"b":["1614988977000000000","1614989037000000000","1614989097000000000"]}`), }, }, } diff --git a/transform/time.go b/transform/time.go index 84c545b5..3a894379 100644 --- a/transform/time.go +++ b/transform/time.go @@ -60,12 +60,12 @@ func (c *timePatternConfig) Validate() error { } func timeUnixToBytes(t time.Time) []byte { - return []byte(fmt.Sprintf("%d", t.UnixMilli())) + return []byte(fmt.Sprintf("%d", t.UnixNano())) } -// timeUnixToStr converts a UnixMilli timestamp to a string. -func timeUnixToStr(timeInt64 int64, timeFmt string, loc string) (string, error) { - timeDate := time.UnixMilli(timeInt64) +// timeUnixToStr converts a UnixNano timestamp to a string. +func timeUnixToStr(ts int64, timeFmt string, loc string) (string, error) { + timeDate := time.Unix(0, ts) if loc != "" { ll, err := time.LoadLocation(loc) diff --git a/transform/time_from_string.go b/transform/time_from_string.go index 154a588c..bf6b37f2 100644 --- a/transform/time_from_string.go +++ b/transform/time_from_string.go @@ -27,8 +27,6 @@ func newTimeFromString(_ context.Context, cfg config.Config) (*timeFromString, e return &tf, nil } -// timeFromString is a transform that converts a pattern-based string -// format to a UnixMilli timestamp. type timeFromString struct { conf timePatternConfig isObject bool @@ -56,7 +54,7 @@ func (tf *timeFromString) Transform(ctx context.Context, msg *message.Message) ( } if tf.isObject { - if err := msg.SetValue(tf.conf.Object.SetKey, date.UnixMilli()); err != nil { + if err := msg.SetValue(tf.conf.Object.SetKey, date.UnixNano()); err != nil { return nil, fmt.Errorf("transform: time_from_string: %v", err) } } else { diff --git a/transform/time_from_string_test.go b/transform/time_from_string_test.go index 61ea265b..e6e9e1f5 100644 --- a/transform/time_from_string_test.go +++ b/transform/time_from_string_test.go @@ -27,7 +27,7 @@ var timeFromStringTests = []struct { }, []byte(`2021-12-19T01:31:30.000Z`), [][]byte{ - []byte(`1639877490000`), + []byte(`1639877490000000000`), }, }, { @@ -41,7 +41,7 @@ var timeFromStringTests = []struct { }, []byte(`2021-12-19T01:31:30.000Z`), [][]byte{ - []byte(`1639895490000`), + []byte(`1639895490000000000`), }, }, // object tests @@ -58,7 +58,7 @@ var timeFromStringTests = []struct { }, []byte(`{"a":"2021-12-19T01:31:30.000Z"}`), [][]byte{ - []byte(`{"a":1639877490000}`), + []byte(`{"a":1639877490000000000}`), }, }, } diff --git a/transform/time_from_unix.go b/transform/time_from_unix.go index 4cc4963e..49af4fc0 100644 --- a/transform/time_from_unix.go +++ b/transform/time_from_unix.go @@ -28,8 +28,6 @@ func newTimeFromUnix(_ context.Context, cfg config.Config) (*timeFromUnix, error return &tf, nil } -// TimeFromUnix is a transform that converts a UnixMilli timestamp to a -// Unix timestamp. type timeFromUnix struct { conf timeUnixConfig isObject bool @@ -51,16 +49,16 @@ func (tf *timeFromUnix) Transform(ctx context.Context, msg *message.Message) ([] return []*message.Message{msg}, nil } - // Convert Unix to UnixMilli. + // Convert Unix to UnixNano. date := time.Unix(value.Int(), 0) - milli := date.UnixMilli() + ns := date.UnixNano() if tf.isObject { - if err := msg.SetValue(tf.conf.Object.SetKey, milli); err != nil { + if err := msg.SetValue(tf.conf.Object.SetKey, ns); err != nil { return nil, fmt.Errorf("transform: time_from_unix: %v", err) } } else { - value := []byte(fmt.Sprintf("%d", milli)) + value := []byte(fmt.Sprintf("%d", ns)) msg.SetData(value) } diff --git a/transform/time_from_unix_test.go b/transform/time_from_unix_test.go index b700b907..868503de 100644 --- a/transform/time_from_unix_test.go +++ b/transform/time_from_unix_test.go @@ -26,7 +26,7 @@ var timeUnixFromTests = []struct { }, []byte(`1639895490`), [][]byte{ - []byte(`1639895490000`), + []byte(`1639895490000000000`), }, nil, }, @@ -43,7 +43,7 @@ var timeUnixFromTests = []struct { }, []byte(`{"a":1639877490}`), [][]byte{ - []byte(`{"a":1639877490000}`), + []byte(`{"a":1639877490000000000}`), }, nil, }, diff --git a/transform/time_now.go b/transform/time_now.go index f205c038..73e032ef 100644 --- a/transform/time_now.go +++ b/transform/time_now.go @@ -54,7 +54,7 @@ func (tf *timeNow) Transform(ctx context.Context, msg *message.Message) ([]*mess date := time.Now() if tf.hasObjectSetKey { - if err := msg.SetValue(tf.conf.Object.SetKey, date.UnixMilli()); err != nil { + if err := msg.SetValue(tf.conf.Object.SetKey, date.UnixNano()); err != nil { return nil, fmt.Errorf("time: now: %v", err) } diff --git a/transform/time_to_string.go b/transform/time_to_string.go index 6e056336..d7728832 100644 --- a/transform/time_to_string.go +++ b/transform/time_to_string.go @@ -27,8 +27,6 @@ func newTimeToString(_ context.Context, cfg config.Config) (*timeToString, error return &tf, nil } -// timeToString is a transform that converts a Unix timestamp to a -// pattern-based string format. type timeToString struct { conf timePatternConfig isObject bool diff --git a/transform/time_to_string_test.go b/transform/time_to_string_test.go index dc20864c..395140f5 100644 --- a/transform/time_to_string_test.go +++ b/transform/time_to_string_test.go @@ -25,7 +25,7 @@ var timeToStringTests = []struct { "format": timeDefaultFmt, }, }, - []byte(`1639877490000`), + []byte(`1639877490000000000`), [][]byte{ []byte(`2021-12-19T01:31:30.000Z`), }, @@ -39,7 +39,7 @@ var timeToStringTests = []struct { "location": "America/New_York", }, }, - []byte(`1639895490000`), + []byte(`1639895490000000000`), [][]byte{ []byte(`2021-12-19T01:31:30.000Z`), }, @@ -56,7 +56,7 @@ var timeToStringTests = []struct { "format": timeDefaultFmt, }, }, - []byte(`{"a":1639877490000}`), + []byte(`{"a":1639877490000000000}`), [][]byte{ []byte(`{"a":"2021-12-19T01:31:30.000Z"}`), }, diff --git a/transform/time_to_unix.go b/transform/time_to_unix.go index a83bcb9c..60564aa6 100644 --- a/transform/time_to_unix.go +++ b/transform/time_to_unix.go @@ -28,8 +28,6 @@ func newTimeToUnix(_ context.Context, cfg config.Config) (*timeToUnix, error) { return &tf, nil } -// timeToUnix is a transform that converts a Unix timestamp to a -// UnixMilli timestamp. type timeToUnix struct { conf timeUnixConfig isObject bool @@ -51,8 +49,8 @@ func (tf *timeToUnix) Transform(ctx context.Context, msg *message.Message) ([]*m return []*message.Message{msg}, nil } - // Convert UnixMilli to Unix. - date := time.UnixMilli(value.Int()) + // Convert UnixNano to Unix. + date := time.Unix(0, value.Int()) unix := date.Unix() if tf.isObject { diff --git a/transform/time_to_unix_test.go b/transform/time_to_unix_test.go index 9cae50aa..3dcf042f 100644 --- a/transform/time_to_unix_test.go +++ b/transform/time_to_unix_test.go @@ -23,7 +23,7 @@ var timeToUnixTests = []struct { config.Config{ Settings: map[string]interface{}{}, }, - []byte(`1639895490000`), + []byte(`1639895490000000000`), [][]byte{ []byte(`1639895490`), }, @@ -39,7 +39,7 @@ var timeToUnixTests = []struct { }, }, }, - []byte(`{"a":1639877490000}`), + []byte(`{"a":1639877490000000000}`), [][]byte{ []byte(`{"a":1639877490}`), }, diff --git a/transform/transform_test.go b/transform/transform_test.go index d4b3751d..bf41756e 100644 --- a/transform/transform_test.go +++ b/transform/transform_test.go @@ -74,7 +74,7 @@ var transformTests = []struct { "format": "2006-01-02T15:04:05.000000Z", }, }, - []byte(`1639877490000`), + []byte(`1639877490000000000`), [][]byte{ []byte(`2021-12-19T01:31:30.000000Z`), },