Comparing the time or date of switching in ruby ​​on rails

I have a specific time from my database, and here is what it looks like:

ruby-1.9.2-p290 :017 > djel.smjena.pocetak1.to_time => 2000-01-01 08:00:00 +0100 

and that’s fine, I was assigned 2000-1-1 also, I got something that happened at some datetime

 ruby-1.9.2-p290 :019 > dog.pocetak => Thu, 25 Aug 2011 08:18:00 UTC +00:00 

So I was hoping that .to_time would break my date, but that would not happen

 ruby-1.9.2-p290 :020 > dog.pocetak.to_time => Thu, 25 Aug 2011 08:18:00 UTC +00:00 

So, now, comparing if something happened before 8:00 is useless. So how can I compare this? Is there a way to install dog.pocetak for 2000-01-01 without a touch clock?

Thank you

ps In addition, I was thinking of creating a new time variable, only to get hours and minutes from the old variables, but these methods do not work?

 ruby-1.9.2-p290 :059 > dog.pocetak.hour => 8 

but

 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>' 

really upset :)

+6
source share
2 answers

With ActiveSupport and Time.change you can reset the year, month, and day if you want:

 > 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 

Thus, you can compare the "times" with each other if they were all reset to the same date. Not sure if this is a good solution, though, it depends on what you are really looking for.

EDIT

According to the mu suggestion, you can also see the time data type.

+3
source

To get minutes from a Time object, you want min not minutes . You cannot have an instance of Time that is simply a β€œtime of day” (ie, No year, month, ...), but you can use strftime to get a string version that will correctly compare:

 tod = Time.now.strftime('%H:%M:%S') # "17:07:23" if(t1.strftime('%H:%M:%S') == t2.strftime('%H:%M:%S')) # Same time of day (to one second resolution) end 

Or you can compare individual hour , min and sec components:

 if(t1.hour == t2.hour && t1.min == t2.min && t1.sec == t2.sec) # Same time of day (to one second resolution) end 

What approach you use, as usual, depends on your specific situation and what else is going on in this area.

+3
source

Source: https://habr.com/ru/post/895510/


All Articles