Skip to content

Commit

Permalink
Support RFC3339Nano and unix epoch time in millisecond for CLI timest…
Browse files Browse the repository at this point in the history
…amp query (#360)

* Support RFC3339Nano and unix epoch time in millisecond for timestamp query

* add tests, add negative unix ts checking
  • Loading branch information
MaxJTRice authored Aug 29, 2018
1 parent 6c7ef23 commit b598c59
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
12 changes: 10 additions & 2 deletions cmd/dosa/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,17 @@ func strToFieldValue(t dosa.Type, s string) (dosa.FieldValue, error) {
}
return dosa.FieldValue(d), nil
case dosa.Timestamp:
t, err := time.Parse(time.RFC3339, s)
t, err := time.Parse(time.RFC3339Nano, s)
if err != nil {
return nil, errors.Wrapf(err, "timestamp should be in form 2006-01-02T15:04:05Z")
// check if the input is a unix epoch timestamp
i, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return nil, errors.Wrapf(err, "timestamp should be in form 2006-01-02T15:04:05.1Z (RFC3339Nano) or Unix epoch time in millisecond")
}
if i < 0 {
return nil, errors.Errorf("timestamp should not be negative")
}
return dosa.FieldValue(time.Unix(0, i*int64(time.Millisecond)).UTC()), nil
}
return dosa.FieldValue(t), nil
case dosa.TUUID:
Expand Down
23 changes: 20 additions & 3 deletions cmd/dosa/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,11 @@ func TestPrintResultsEmpty(t *testing.T) {
}

func TestStrToFieldValue(t *testing.T) {
tStr := "2018-06-11T13:03:01Z"
ts, _ := time.Parse(time.RFC3339, tStr)
// happy cases
tDateSec := "2018-06-11T13:03:01Z"
tDateMsec := "2018-06-11T13:03:01.000Z"
tUnixMsec := "1528722181000"
ts, _ := time.Parse(time.RFC3339Nano, tDateSec)
cases := []struct {
inType dosa.Type
inStr string
Expand All @@ -120,7 +123,9 @@ func TestStrToFieldValue(t *testing.T) {
{dosa.Bool, "false", dosa.FieldValue(false)},
{dosa.String, "42", dosa.FieldValue("42")},
{dosa.Double, "42.00", dosa.FieldValue(float64(42))},
{dosa.Timestamp, tStr, dosa.FieldValue(ts)},
{dosa.Timestamp, tDateSec, dosa.FieldValue(ts)},
{dosa.Timestamp, tDateMsec, dosa.FieldValue(ts)},
{dosa.Timestamp, tUnixMsec, dosa.FieldValue(ts)},
{dosa.TUUID, "3e4befa0-69d3-11e8-95b0-d55aa227a290", dosa.FieldValue(dosa.UUID("3e4befa0-69d3-11e8-95b0-d55aa227a290"))},
}

Expand All @@ -129,4 +134,16 @@ func TestStrToFieldValue(t *testing.T) {
assert.Equal(t, c.expFv, outFv)
assert.Nil(t, err)
}

// sad case for negative unix timestamp
tUnixMsecNeg := "-1528722181000"
fv, err := strToFieldValue(dosa.Timestamp, tUnixMsecNeg)
assert.Nil(t, fv)
assert.Contains(t, err.Error(), "timestamp should not be negative")

// sad case for overflow unix timestamp
tUnixMsecOverflow := "15287221810000000000"
fv, err = strToFieldValue(dosa.Timestamp, tUnixMsecOverflow)
assert.Nil(t, fv)
assert.Contains(t, err.Error(), "value out of range")
}

0 comments on commit b598c59

Please sign in to comment.