Luigi
Luigi

Reputation: 5603

How does rails know the current date? Need to spoof the date to yesterday

I have the following code:

puts 12.months.ago.month

Currently, this displays 5, for May since it's May 1st.

I need to spoof the date to make Rails think that it is yesterday, April 30th, so that the above will display 4. I am running this on my local machine - I tried changing my computers date/time, but it didn't work as intended.

Any thoughts?

Upvotes: 0

Views: 86

Answers (2)

Uri Agassi
Uri Agassi

Reputation: 37409

class Time
  class << self
    alias :orig_now :now
    def now
      orig_now - 1.day
    end
  end
end

Time.now
# => 2014-04-30 16:35:01 +0300 

puts 12.months.ago.month
# => 4

Upvotes: 3

Saurabh
Saurabh

Reputation: 73639

Why don't you do something like following:

puts (12.months.ago - 1.day).month         # returns 4

Upvotes: 0

Related Questions