Glenn Fu » time http://www.glennfu.com code like water, compile like bamboo! Tue, 23 Aug 2011 21:00:34 +0000 en hourly 1 http://wordpress.org/?v=3.2.1 Overriding Time.now to Control the Server Time http://www.glennfu.com/2008/02/28/overriding-timenow-to-control-the-server-time/ http://www.glennfu.com/2008/02/28/overriding-timenow-to-control-the-server-time/#comments Thu, 28 Feb 2008 20:05:59 +0000 Glenn Sidney http://www.glennfu.com/2008/02/28/overriding-timenow-to-control-the-server-time/ I just recently worked on a project in which I needed to modify what time the Ruby on Rails server thought it was. I was able to change this very easily with the help of court3nay while on #rubyonrails.

def Time.now
  Time.parse("2010-02-30", nil)
end

With this I am able to change the server time whenever I want! The system was one in which user interface capabilities changed at different times of the month. Obviously I didn’t want to wait 20 days to see something different!

I placed this code snippet in my config/environments/development.rb file so that everything in my application could access it, and it wouldn’t affect the test or production environments. The only downside to this is that you have to restart the server in order to change the time. Of course, in RoR this takes like 5 seconds so it was no big deal for me.

In my specs I used stuff like

date = "2008-01-01"
Time.stub!(:now).and_return(Time.parse(date))

which of course made it very easy to write effective specs that covered all of my functionality. Thank you RSpec!

Voila, now you too can control time!!

]]>
http://www.glennfu.com/2008/02/28/overriding-timenow-to-control-the-server-time/feed/ 0