获取时间类型数据

使用SQL语句查询到时间类型(包括Datetime、DateTime)数据后,您可以需要根据实际获取具体数据。

前提条件

接口说明

获取不同时间类型的接口说明请参见下表,请根据实际时间类型选择。

重要

getDateTime获取的时间时区默认为UTC时区,请根据需要进行时区转换。

时间类型

接口

参数

返回类型

Datetime

getDateTime

columnIndex:int

time.Time

Datetime

getDateTime

columnName:string

time.Time

Time

getTime

columnIndex:int

time.Duration

Time

getTime

columnName:string

time.Duration

Date

getDate

columnIndex:int

time.Time

Date

getDate

columnName:string

time.Time

参数

参数

说明

query

SQL语句,请根据所需功能进行设置。

示例

使用select from_unixtime(time_col) as datetime_value,time(from_unixtime(time_col)) as time_value, date(from_unixtime(time_col)) as date_value from test_table limit 1语句查询test_table表中time_col列数据并将其转换为Datetime、Time、Date三种类型,最多返回1行数据。系统会返回查询语句的请求类型、返回值Schema、返回结果等信息。

func queryData(client *tablestore.TableStoreClient) {
    // 创建SQL请求。
    request := &tablestore.SQLQueryRequest{Query: "select from_unixtime(time_col) as datetime_value,time(from_unixtime(time_col)) as time_value, date(from_unixtime(time_col)) as date_value from test_table limit 1"}

    // 获取SQL的响应结果。
    response, err := client.SQLQuery(request)
    if err != nil {
        panic(err)
    }

    // 获取SQL的返回结果。
    resultSet := response.ResultSet
    fmt.Println("response resultset:")
    for resultSet.HasNext() {
        row := resultSet.Next()
        for i := 0; i < len(columns); i++ {
            name := columns[i].Name
            isnull, err := row.IsNull(i)
            if err != nil {
                println("[INFO:] get column error, name: ", name, ", error: ", err.Error())
                continue
            }
            if isnull {
                println("[INFO]: column is SQL NULL, name: ", name)
                continue
            }
            switch columns[i].Type {
                case tablestore.ColumnType_DATETIME:
                time, err := row.GetDateTime(i)
                if err != nil {
                    println("[INFO:] get column error, name: ", name, ", error: ", err.Error())
                }
                println(time.Local().String())
                time, err = row.GetDateTimeByName("datetime_value")
                if err != nil {
                    println("[INFO:] get column error, name: ", name, ", error: ", err.Error())
                }
                println(time.String())
                case tablestore.ColumnType_TIME:
                duration, err := row.GetTime(i)
                if err != nil {
                    println("[INFO:] get column error, name: ", name, ", error: ", err.Error())
                }
                println(duration.String())
                duration, err = row.GetTimeByName("time_value")
                if err != nil {
                    println("[INFO:] get column error, name: ", name, ", error: ", err.Error())
                }
                println(duration.String())
                case tablestore.ColumnType_DATE:
                date, err := row.GetDate(i)
                if err != nil {
                    println("[INFO:] get column error, name: ", name, ", error: ", err.Error())
                }
                println(date.String())
                date, err = row.GetDateByName("date_value")
                if err != nil {
                    println("[INFO:] get column error, name: ", name, ", error: ", err.Error())
                }
                println(date.String())
            }
        }
    }
}

返回结果如下所示。

response resultset:
2023-11-09 10:14:00.01 +0800 CST
2023-11-09 10:14:00.01 +0800 CST
10h14m0.01s
10h14m0.01s
2023-11-09 00:00:00 +0000 UTC
2023-11-09 00:00:00 +0000 UTC