Skip to content

Commit

Permalink
feat!: ns precision
Browse files Browse the repository at this point in the history
  • Loading branch information
jshlbrd committed Oct 9, 2023
1 parent 4ad25dc commit da259f2
Show file tree
Hide file tree
Showing 12 changed files with 24 additions and 32 deletions.
2 changes: 1 addition & 1 deletion transform/meta_for_each_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}`),
},
},
}
Expand Down
8 changes: 4 additions & 4 deletions transform/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions transform/time_from_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions transform/time_from_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var timeFromStringTests = []struct {
},
[]byte(`2021-12-19T01:31:30.000Z`),
[][]byte{
[]byte(`1639877490000`),
[]byte(`1639877490000000000`),
},
},
{
Expand All @@ -41,7 +41,7 @@ var timeFromStringTests = []struct {
},
[]byte(`2021-12-19T01:31:30.000Z`),
[][]byte{
[]byte(`1639895490000`),
[]byte(`1639895490000000000`),
},
},
// object tests
Expand All @@ -58,7 +58,7 @@ var timeFromStringTests = []struct {
},
[]byte(`{"a":"2021-12-19T01:31:30.000Z"}`),
[][]byte{
[]byte(`{"a":1639877490000}`),
[]byte(`{"a":1639877490000000000}`),
},
},
}
Expand Down
10 changes: 4 additions & 6 deletions transform/time_from_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions transform/time_from_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var timeUnixFromTests = []struct {
},
[]byte(`1639895490`),
[][]byte{
[]byte(`1639895490000`),
[]byte(`1639895490000000000`),
},
nil,
},
Expand All @@ -43,7 +43,7 @@ var timeUnixFromTests = []struct {
},
[]byte(`{"a":1639877490}`),
[][]byte{
[]byte(`{"a":1639877490000}`),
[]byte(`{"a":1639877490000000000}`),
},
nil,
},
Expand Down
2 changes: 1 addition & 1 deletion transform/time_now.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 0 additions & 2 deletions transform/time_to_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions transform/time_to_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var timeToStringTests = []struct {
"format": timeDefaultFmt,
},
},
[]byte(`1639877490000`),
[]byte(`1639877490000000000`),
[][]byte{
[]byte(`2021-12-19T01:31:30.000Z`),
},
Expand All @@ -39,7 +39,7 @@ var timeToStringTests = []struct {
"location": "America/New_York",
},
},
[]byte(`1639895490000`),
[]byte(`1639895490000000000`),
[][]byte{
[]byte(`2021-12-19T01:31:30.000Z`),
},
Expand All @@ -56,7 +56,7 @@ var timeToStringTests = []struct {
"format": timeDefaultFmt,
},
},
[]byte(`{"a":1639877490000}`),
[]byte(`{"a":1639877490000000000}`),
[][]byte{
[]byte(`{"a":"2021-12-19T01:31:30.000Z"}`),
},
Expand Down
6 changes: 2 additions & 4 deletions transform/time_to_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions transform/time_to_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var timeToUnixTests = []struct {
config.Config{
Settings: map[string]interface{}{},
},
[]byte(`1639895490000`),
[]byte(`1639895490000000000`),
[][]byte{
[]byte(`1639895490`),
},
Expand All @@ -39,7 +39,7 @@ var timeToUnixTests = []struct {
},
},
},
[]byte(`{"a":1639877490000}`),
[]byte(`{"a":1639877490000000000}`),
[][]byte{
[]byte(`{"a":1639877490}`),
},
Expand Down
2 changes: 1 addition & 1 deletion transform/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`),
},
Expand Down

0 comments on commit da259f2

Please sign in to comment.