搜索UNIX时间戳会得到不同的结果



下面的查询给出了不同的结果,两个查询的结果都应该是2。我在我的数据库(postgres)中使用时间戳列,并且正在搜索其end_at列小于或等于给定UNIX时间戳的对象。

puts object.time_records.where('time_records.end_at <= ?', object.time_records.second.end_at).count #=> 2 (Correct)
puts object.time_records.where('time_records.end_at <= ?', DateTime.strptime(object.time_records.second.end_at.to_i.to_s, '%s')).count # => 1 (Incorrect)
puts object.time_records.where('time_records.end_at <= ?', Time.at(object.time_records.second.end_at.to_i)).count # => 1 (Incorrect)

如果我输入一些数据,查询中使用的时间戳可能是,例如:

1473024092

那么如果我打印对象的时间戳:

puts object.time_records.pluck(:end_at).map(&:to_i)

我得到以下结果:

1472419292
1473024092
1473628892
1474233692

从这些可以看出,正确的结果应该是2。如果有人遇到类似的情况,我希望你能给我指点一下方向。

为了它的价值,这是发生在规范我写的宝石。我尝试了in_time_zone.utc的不同组合来解析和转换为时间戳,它们都提供了相同的结果。即使转换为时间戳并直接返回到时间,并且测试是否相等,当to_s对两者都相等时,结果为false。

我在irb中运行了一个例子:

2.3.0 :001 > now = Time.now
 => 2016-08-28 21:58:43 +0100 
2.3.0 :002 > timestamp = now.to_i
 => 1472417923 
2.3.0 :003 > parsed_timestamp = Time.at(timestamp)
 => 2016-08-28 21:58:43 +0100 
2.3.0 :004 > now.eql?(parsed_timestamp)
 => false 
2.3.0 :005 > now == parsed_timestamp
 => false 
2.3.0 :006 > now === parsed_timestamp
 => false 
2.3.0 :007 > now.class
 => Time 
2.3.0 :008 > parsed_timestamp.class
 => Time 

问题是小数倍。UNIX时间戳以秒为单位,因此在转换to_i时,将丢弃毫秒。

设置时间戳列的精度解决了这个问题:

class CreateTimeRecords < ActiveRecord::Migration
  def change
    create_table :time_records do |t|
      t.belongs_to :object, index: true, null: false
      t.datetime :start_at, null: false, index: true, precision: 0
      t.datetime :end_at, null: false, index: true, precision: 0
      t.timestamps null: false
    end
  end
end

最新更新