How can I filter data with an 'int64' type column?

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

How can I filter data with an 'int64' type column?

问题

我收到大量数据并将它们序列化为一个具有Int64类型字段'Create_time'的结构体,例如:

type Log struct {
	ID            int       `mapstructure:"id"`
	Create_time   int64     `mapstructure:"create_time,omitempty"`
}

现在,如果我想筛选出2021-10-24 00:00:00和2021-10-25 00:00:00之间的数据,我该如何操作?

英文:

I get lots of data and serialize them to a struct with a field 'Create_time' of type Int64, such as:

type Log struct {
	ID            int       `mapstructure:"id"`
	Create_time   int64     `mapstructure:"create_time,omitempty"`
}

Now if I want to filter data between 2021-10-24 00:00:00 and 2021-10-25 00:00:00, how can I do ?

答案1

得分: 4

假设Create_time是使用Time.Unix()获取的,你可以找到范围的Unix表示,然后进行过滤:

from := time.Date(2021, 10, 24, 0, 0, 0, 0, time.Local).Unix()
to := time.Date(2021, 10, 25, 0, 0, 0, 0, time.Local).Unix()

然后找到所有满足Create_time>=from && Create_time<=to条件的元素。

根据时区进行必要的更改。如果Create_time是毫秒/微秒/纳秒级别的时间戳,则使用UnixMilliUnixMicroUnixNano

英文:

Assuming Create_time is obtained using Time.Unix(), you can find the Unix representation for the range, and then filter:

from:=time.Date(2021,10,24,0,0,0,0,time.Local).Unix()
to:=time.Date(2021,10,25,0,0,0,0,time.Local).Unix()

Then find all elements with Create_time&gt;=from &amp;&amp; Create_time&lt;=to.

Make the necessary changes about the timezones. If the Create_time is milli/micro/nanoseconds, then use UnixMilli, UnixMicro, or UnixNano.

huangapple
  • 本文由 发表于 2021年10月25日 12:22:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/69702855.html
匿名

发表评论

匿名网友

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

确定