From 1cc6a87d2077c52b951ea18ed55edd9ddccf7a14 Mon Sep 17 00:00:00 2001 From: reesporte Date: Tue, 16 Nov 2021 14:53:30 -0600 Subject: [PATCH] refactor and add test --- server/pg.go | 2 +- util.go | 11 +++++++++-- util_test.go | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/server/pg.go b/server/pg.go index da6dfd633..169f2e747 100644 --- a/server/pg.go +++ b/server/pg.go @@ -332,7 +332,7 @@ func pgWriteGroupCount(w pg.QueryResultWriter, counts *pilosa.GroupCounts) error switch { case g.Value != nil: if g.FieldOptions.Type == pilosa.FieldTypeTimestamp { - v = time.Unix(0, (int64(*g.Value)+int64(g.FieldOptions.Base))*pilosa.TimeUnitNanos(g.FieldOptions.TimeUnit)).UTC().Format(time.RFC3339Nano) + v = pilosa.FormatTimestampNano(int64(*g.Value), g.FieldOptions.Base, g.FieldOptions.TimeUnit) } else { v = strconv.FormatInt(*g.Value, 10) } diff --git a/util.go b/util.go index 6d4b8e09e..1db0c74fc 100644 --- a/util.go +++ b/util.go @@ -115,8 +115,9 @@ func roaringFragmentHasData(path string, index, field, view string, shard uint64 return } -// GetLoopProgress returns the estimated remaining time to iterate through some items -// as well as the loop completion percentage with the following parameters: +// GetLoopProgress returns the estimated remaining time to iterate through some +// items as well as the loop completion percentage with the following +// parameters: // the start time, the current time, the iteration, and the number of items func GetLoopProgress(start time.Time, now time.Time, iteration uint, total uint) (remaining time.Duration, pctDone float64) { itemsLeft := total - (iteration + 1) @@ -124,3 +125,9 @@ func GetLoopProgress(start time.Time, now time.Time, iteration uint, total uint) pctDone = (float64(iteration+1) / float64(total)) * 100 return time.Duration(avgItemTime * float64(itemsLeft)), pctDone } + +// FormatTimestampNano returns the string representation of a timestamp given: +// an epoch value, base, and time unit +func FormatTimestampNano(value, base int64, timeUnit string) string { + return time.Unix(0, (value+base)*TimeUnitNanos(timeUnit)).UTC().Format(time.RFC3339Nano) +} diff --git a/util_test.go b/util_test.go index c659053c5..14ed78c36 100644 --- a/util_test.go +++ b/util_test.go @@ -85,3 +85,21 @@ func TestGetLoopProgress(t *testing.T) { }) } } + +func TestFormatTimestampNano(t *testing.T) { + if FormatTimestampNano(0, 69, "s") != "1970-01-01T00:01:09Z" { + t.Fatal("Timestamp not formatted properly") + } + if FormatTimestampNano(0, 420, "ms") != "1970-01-01T00:00:00.42Z" { + t.Fatal("Timestamp not formatted properly") + } + if FormatTimestampNano(420, 0, "μs") != "1970-01-01T00:00:00.00000042Z" { + t.Fatal("Timestamp not formatted properly") + } + if FormatTimestampNano(420, 69, "us") != "1970-01-01T00:00:00.000489Z" { + t.Fatal("Timestamp not formatted properly") + } + if FormatTimestampNano(69, 420, "ns") != "1970-01-01T00:00:00.000000489Z" { + t.Fatal("Timestamp not formatted properly") + } +}