Skip to content

Commit

Permalink
fix: printing timestamps that are higher than int64 max / 1e3 (#245)
Browse files Browse the repository at this point in the history
* fix: printing timestamps that are higher than int64 max / 1e3

* fix test that was depending on local time

---------

Co-authored-by: Baha Aiman <bahaaiman@google.com>
  • Loading branch information
bmfurtado and bhshkh authored Nov 15, 2024
1 parent 3d525f6 commit 6441222
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
8 changes: 6 additions & 2 deletions cbt.go
Original file line number Diff line number Diff line change
Expand Up @@ -1403,6 +1403,10 @@ func doLookup(ctx context.Context, args ...string) {
}

func printRow(r bigtable.Row, w io.Writer) {
printRowAtTimezone(r, w, time.Local)
}

func printRowAtTimezone(r bigtable.Row, w io.Writer, loc *time.Location) {
fmt.Fprintln(w, strings.Repeat("-", 40))
fmt.Fprintln(w, r.Key())

Expand All @@ -1415,10 +1419,10 @@ func printRow(r bigtable.Row, w io.Writer) {
ris := r[fam]
sort.Sort(byColumn(ris))
for _, ri := range ris {
ts := time.Unix(0, int64(ri.Timestamp)*1e3)
ts := time.UnixMicro(int64(ri.Timestamp))
fmt.Fprintf(w, " %-40s @ %s\n",
ri.Column,
ts.Format("2006/01/02-15:04:05.000000"))
ts.In(loc).Format("2006/01/02-15:04:05.000000"))
formatted, err :=
globalValueFormatting.format(
" ", fam, ri.Column, ri.Value)
Expand Down
26 changes: 26 additions & 0 deletions cbt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,32 @@ func TestCsvParseAndWrite(t *testing.T) {
}
}

func TestPrintRowWithHighTimestamp(t *testing.T) {
ctx, client := setupEmulator(t, []string{"my-table"}, []string{"my-family"})
tbl := client.Open("my-table")
mut := bigtable.NewMutation()

loc, err := time.LoadLocation("US/Pacific")
if (err != nil) {
t.Fatalf("Failed to load timezone: %v", err)
}

// a timestamp that is just over int64 max in nanoseconds
mut.Set("my-family", "foo", 9223372036855000, []byte("bar"))
err = tbl.Apply(ctx, "my-key", mut)
if (err != nil) {
t.Fatalf("Could not write some rows to prepare the test.")
}
row, err := tbl.ReadRow(ctx, "my-key")
var sb strings.Builder
printRowAtTimezone(row, &sb, loc)

expected := "@ 2262/04/11-16:47:16.855000"
if !strings.Contains(sb.String(), expected) {
t.Fatalf("Could not find correct timestamp string in printRow result.\nExpected a string containing: %s\nGot: %s", expected, sb.String())
}
}

func TestCsvParseAndWriteBadFamily(t *testing.T) {
ctx, client := setupEmulator(t, []string{"my-table"}, []string{"my-family"})

Expand Down

0 comments on commit 6441222

Please sign in to comment.