Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add use_of_customfuncs.md
Also fix a couple lint issues by unexporting constants (that don't need to be exported)
  • Loading branch information
jf-tech committed Nov 25, 2020
commit 0b426c99d63e0de1e8fe627138d56978395ad6d2
12 changes: 6 additions & 6 deletions customfuncs/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ func DateTimeLayoutToRFC3339(_ *transformctx.Ctx, datetime, layout, layoutTZ, fr
}

const (
EpochUnitMilliseconds = "MILLISECOND"
EpochUnitSeconds = "SECOND"
epochUnitMilliseconds = "MILLISECOND"
epochUnitSeconds = "SECOND"
)

// DateTimeToEpoch parses a 'datetime' string intelligently, and returns its epoch number. 'fromTZ'
Expand All @@ -131,9 +131,9 @@ func DateTimeToEpoch(_ *transformctx.Ctx, datetime, fromTZ, unit string) (string
return "", err
}
switch unit {
case EpochUnitMilliseconds:
case epochUnitMilliseconds:
return strconv.FormatInt(t.UnixNano()/int64(time.Millisecond), 10), nil
case EpochUnitSeconds:
case epochUnitSeconds:
return strconv.FormatInt(t.Unix(), 10), nil
default:
return "", fmt.Errorf("unknown epoch unit '%s'", unit)
Expand Down Expand Up @@ -163,9 +163,9 @@ func EpochToDateTimeRFC3339(_ *transformctx.Ctx, epoch, unit string, tz ...strin
}
var t time.Time
switch unit {
case EpochUnitSeconds:
case epochUnitSeconds:
t = time.Unix(n, 0)
case EpochUnitMilliseconds:
case epochUnitMilliseconds:
t = time.Unix(0, n*(int64(time.Millisecond)))
default:
return "", fmt.Errorf("unknown epoch unit '%s'", unit)
Expand Down
24 changes: 12 additions & 12 deletions customfuncs/datetime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,23 +216,23 @@ func TestDateTimeToEpoch(t *testing.T) {
name: "empty datetime -> no op",
datetime: "",
fromTZ: "UTC",
unit: EpochUnitMilliseconds,
unit: epochUnitMilliseconds,
err: "",
expected: "",
},
{
name: "invalid datetime",
datetime: "invalid",
fromTZ: "UTC",
unit: EpochUnitSeconds,
unit: epochUnitSeconds,
err: "unable to parse 'invalid' in any supported date/time format",
expected: "",
},
{
name: "invalid fromTZ",
datetime: "2020/09/22T12:34:56",
fromTZ: "invalid",
unit: EpochUnitMilliseconds,
unit: epochUnitMilliseconds,
err: "unknown time zone invalid",
expected: "",
},
Expand All @@ -248,23 +248,23 @@ func TestDateTimeToEpoch(t *testing.T) {
name: "datetime no tz; no fromTZ",
datetime: "2020/09/22T12:34:56",
fromTZ: "",
unit: EpochUnitMilliseconds,
unit: epochUnitMilliseconds,
err: "",
expected: "1600778096000",
},
{
name: "datetime no tz; with fromTZ",
datetime: "2020/09/22T12:34:56",
fromTZ: "America/Los_Angeles",
unit: EpochUnitSeconds,
unit: epochUnitSeconds,
err: "",
expected: "1600803296",
},
{
name: "datetime with tz; with fromTZ",
datetime: "2020/09/22T12:34:56-05",
fromTZ: "America/Los_Angeles",
unit: EpochUnitSeconds,
unit: epochUnitSeconds,
err: "",
expected: "1600796096",
},
Expand Down Expand Up @@ -295,31 +295,31 @@ func TestEpochToDateTimeRFC3339(t *testing.T) {
{
name: "empty epoch -> no op",
epoch: "",
unit: EpochUnitMilliseconds,
unit: epochUnitMilliseconds,
tz: nil,
err: "",
expected: "",
},
{
name: "more than one tz specified",
epoch: "1234567",
unit: EpochUnitMilliseconds,
unit: epochUnitMilliseconds,
tz: []string{"UTC", "UTC"},
err: "cannot specify tz argument more than once",
expected: "",
},
{
name: "invalid epoch",
epoch: "invalid",
unit: EpochUnitSeconds,
unit: epochUnitSeconds,
tz: nil,
err: `strconv.ParseInt: parsing "invalid": invalid syntax`,
expected: "",
},
{
name: "invalid tz",
epoch: "12345",
unit: EpochUnitSeconds,
unit: epochUnitSeconds,
tz: []string{"invalid"},
err: "unknown time zone invalid",
expected: "",
Expand All @@ -335,15 +335,15 @@ func TestEpochToDateTimeRFC3339(t *testing.T) {
{
name: "no tz",
epoch: "1234567890123",
unit: EpochUnitMilliseconds,
unit: epochUnitMilliseconds,
tz: nil,
err: "",
expected: "2009-02-13T23:31:30Z",
},
{
name: "with tz",
epoch: "1234567890",
unit: EpochUnitSeconds,
unit: epochUnitSeconds,
tz: []string{"America/Los_Angeles"},
err: "",
expected: "2009-02-13T15:31:30-08:00",
Expand Down
Loading