比较铁轨上红宝石的时间或开沟日期

use*_*119 6 ruby ruby-on-rails ruby-on-rails-3

我有一些时间从我的数据库定义,这是它的样子:

ruby-1.9.2-p290 :017 > djel.smjena.pocetak1.to_time 
 => 2000-01-01 08:00:00 +0100 
Run Code Online (Sandbox Code Playgroud)

这没关系,它也给了我2000-1-1,我得到的东西发生在某个日期时间

ruby-1.9.2-p290 :019 > dog.pocetak 
 => Thu, 25 Aug 2011 08:18:00 UTC +00:00 
Run Code Online (Sandbox Code Playgroud)

所以我希望,.to_time会放弃我的约会,但这不会发生

ruby-1.9.2-p290 :020 > dog.pocetak.to_time 
 => Thu, 25 Aug 2011 08:18:00 UTC +00:00 
Run Code Online (Sandbox Code Playgroud)

那么,现在,比较8:00之前发生的事情是否无用.那么,我该如何比较呢?有没有办法将dog.pocetak设置为2000-01-01没有触摸时钟?

谢谢

ps也,我想创建新的时间变量,只是从旧的变量小时和分钟,但这种方法不起作用?

ruby-1.9.2-p290 :059 > dog.pocetak.hour
 => 8
Run Code Online (Sandbox Code Playgroud)

ruby-1.9.2-p290 :060 > dog.pocetak.minute
NoMethodError: undefined method `minute' for 2011-08-25 08:18:00 UTC:Time
        from /home/dorijan/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.10/lib/active_support/time_with_zone.rb:322:in `method_missing'
        from (irb):60
        from /home/dorijan/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.10/lib/rails/commands/console.rb:44:in `start'
        from /home/dorijan/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.10/lib/rails/commands/console.rb:8:in `start'
        from /home/dorijan/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.10/lib/rails/commands.rb:23:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'
ruby-1.9.2-p290 :061 > dog.pocetak.minutes
NoMethodError: undefined method `minutes' for 2011-08-25 08:18:00 UTC:Time
        from /home/dorijan/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.10/lib/active_support/time_with_zone.rb:322:in `method_missing'
        from (irb):61
        from /home/dorijan/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.10/lib/rails/commands/console.rb:44:in `start'
        from /home/dorijan/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.10/lib/rails/commands/console.rb:8:in `start'
        from /home/dorijan/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.10/lib/rails/commands.rb:23:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'
Run Code Online (Sandbox Code Playgroud)

真的很沮丧:)

Cas*_*per 3

如果您愿意,可以使用ActiveSupportTime.change重置年、月和日:

> t = Time.now
=> Sun Aug 21 00:46:29 +0000 2011
> t.change(:month => 1, :day => 1, :year => 2000)
=> Sat Jan 01 00:46:29 +0000 2000
Run Code Online (Sandbox Code Playgroud)

这样,您就可以比较彼此之间的“时间”(如果它们都重置为同一日期)。但不确定这是否是一个好的解决方案,取决于您真正想要的是什么。

编辑

根据 mu 的建议,您还可以查看时间数据类型。