This should be simple, but I am getting edge cases that seem to be failing, I am doing something wrong and it kinda confuses me. I have a method like this:
def self.needs_updating?(last_updated_time, time_since_update)
return false if last_updated_time.nil?
time_since_update < last_updated_time
end
It's called using this syntax:
Tools::Utils.needs_updating?(@entry.updated_at, 15.minutes.ago)
What I am trying to do is discover whether @entry has been updated at least 15.minutes.ago. If it has been updated 20.minutes.ago this should return true, if it has been updated 1.minute.ago, it should return false.
I think i may need to insert DateTime.now.utc in there maybe. But how can i do it so that I can keep using the Rails idiom (14.minutes.ago, 1.hour.ago etc..) ?
Thanks!
CodePudding user response:
It should be
def self.needs_updating?(last_updated_time, time_since_update)
return false if last_updated_time.nil?
last_updated_time < time_since_update
end
If you want time in UTC use last_updated_time.utc < time_since_update.utc
If you want time in your local time use last_updated_time.localtime < time_since_update.localtime
example
last_updated_time = @entry.updated_at
=> 2022-05-11 22:30:11 0200
time_since_update = 15.minutes.ago
=> 2022-05-12 22:35:15 0200
last_updated_time < time_since_update
=> true