for resultsIterator.HasNext() { response, err := resultsIterator.Next() if err != nil { return shim.Error(err.Error()) } if bArrayMemberAlreadyWritten == true { buffer.WriteString(",") } buffer.WriteString("{"TID":") buffer.WriteString(""") buffer.WriteString(response.TxId) buffer.WriteString(""") buffer.WriteString(", "THistory":") if response.IsDelete { buffer.WriteString("null") } else { buffer.WriteString(string(response.Value)) } buffer.WriteString(", "TTime":") buffer.WriteString(""") buffer.WriteString(time.Unix(response.Timestamp.Seconds, int64(response.Timestamp.Nanos)).String()) buffer.WriteString(""") buffer.WriteString(", "IsDelete":") buffer.WriteString(""") buffer.WriteString(strconv.FormatBool(response.IsDelete)) buffer.WriteString(""") buffer.WriteString("}") bArrayMemberAlreadyWritten = true } buffer.WriteString("]") return shim.Success(buffer.Bytes()) }这个tttime改成北京时间
时间: 2023-07-05 07:09:40 浏览: 105
可以使用`time.LoadLocation()`和`time.In()`函数将UTC时间转换为北京时间。修改后的代码如下:
```
for resultsIterator.HasNext() {
response, err := resultsIterator.Next()
if err != nil {
return shim.Error(err.Error())
}
if bArrayMemberAlreadyWritten == true {
buffer.WriteString(",")
}
buffer.WriteString("{\"TID\":")
buffer.WriteString("\"")
buffer.WriteString(response.TxId)
buffer.WriteString("\"")
buffer.WriteString(", \"THistory\":")
if response.IsDelete {
buffer.WriteString("null")
} else {
buffer.WriteString(string(response.Value))
}
buffer.WriteString(", \"TTime\":")
timestamp := time.Unix(response.Timestamp.Seconds, int64(response.Timestamp.Nanos))
location, _ := time.LoadLocation("Asia/Shanghai") // 加载北京时区
beijingTime := timestamp.In(location) // 转换为北京时间
buffer.WriteString("\"")
buffer.WriteString(beijingTime.String())
buffer.WriteString("\"")
buffer.WriteString(", \"IsDelete\":")
buffer.WriteString("\"")
buffer.WriteString(strconv.FormatBool(response.IsDelete))
buffer.WriteString("\"")
buffer.WriteString("}")
bArrayMemberAlreadyWritten = true
}
```
需要注意的是,你应该根据实际情况选择正确的时区。上面代码中使用的是"Asia/Shanghai"时区,即北京时间。
阅读全文