如何将当前时间解析为 InfluxDB Go 客户端的格式?

huangapple go评论148阅读模式
英文:

How to parse current time to InfluxDB Go client?

问题

我正在使用以下Go客户端("github.com/influxdata/influxdb/client/v2")查询InfluxDB,并且它工作正常。

q = fmt.Sprintf("SELECT * FROM %s WHERE time > now() - 3600s", Measurement)

但是我想使用Go的时间变量代替InfluxDB的now()

t := time.Now().Format(time.RFC3339)
q = fmt.Sprintf("SELECT * FROM %s WHERE time > %s - 3600s", Measurement, t)

但是出现了错误解析查询:在第1行,第101个字符处找到-01,预期为;

英文:

I am using below Go client ("github.com/influxdata/influxdb/client/v2") to query InfluDB and it is working fine

q = fmt.Sprintf("SELECT * FROM %s WHERE time > now() - 3600s", Measurement)

but I want to use Go time variable instead of InfluxDB now()

t := time.Now().Format(time.RFC3339)
q = fmt.Sprintf("SELECT * FROM %s WHERE time > %s - 3600s", Measurement, t)

but getting error parsing query: found -01, expected ; at line 1, char 101

答案1

得分: 2

日期字符串必须使用单引号括起来。使用以下代码可以解决你的问题:

t := time.Now().Format(time.RFC3339)
q = fmt.Sprintf("SELECT * FROM %s WHERE time > '%s' - 3600s", Measurement, t)
英文:

Date strings must be single quoted. Using

t := time.Now().Format(time.RFC3339)
q = fmt.Sprintf("SELECT * FROM %s WHERE time > '%s' - 3600s", Measurement, t)

Should solve your issue.

huangapple
  • 本文由 发表于 2017年1月26日 02:37:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/41859100.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定