github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/tests/integration/table_tz_timestamp_test.go (about)

     1  //go:build integration
     2  // +build integration
     3  
     4  package integration
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"os"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/stretchr/testify/require"
    14  
    15  	"github.com/ydb-platform/ydb-go-sdk/v3"
    16  	"github.com/ydb-platform/ydb-go-sdk/v3/table"
    17  )
    18  
    19  func TestTzTimestamp(t *testing.T) {
    20  	ctx := context.Background()
    21  	db, err := ydb.Open(ctx, os.Getenv("YDB_CONNECTION_STRING"))
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  	defer func() {
    26  		_ = db.Close(ctx)
    27  	}()
    28  	err = db.Table().DoTx(ctx, func(ctx context.Context, tx table.TransactionActor) (err error) {
    29  		microseconds := int64(1680021427000000)
    30  		res, err := tx.Execute(ctx, fmt.Sprintf(`SELECT CAST(CAST(%d AS Timestamp) AS TzTimestamp);`, microseconds), nil)
    31  		if err != nil {
    32  			return err
    33  		}
    34  		if err = res.NextResultSetErr(ctx); err != nil {
    35  			return err
    36  		}
    37  		if !res.NextRow() {
    38  			return fmt.Errorf("unexpected no rows in result set (err = %w)", res.Err())
    39  		}
    40  		var v *time.Time
    41  		if err = res.Scan(&v); err != nil {
    42  			return err
    43  		}
    44  		require.NotNil(t, v)
    45  		require.Equal(t, microseconds, v.UTC().UnixMicro())
    46  		return res.Err()
    47  	}, table.WithIdempotent())
    48  	require.NoError(t, err)
    49  }