Time.now and 1.day.ago style comparison

Hi,

I'm trying to use the ActiveSupport Time extension but running into a bit of trouble with the time zone.

I'd like to test if a user record has expired using the Time .ago method, such as:

def expired   return true if self.activated < 1.day.ago end

Unfortunately I can't seem to use the .ago method in the comparison because it's using UTC time, whereas Time.now has my current time zone (-0700).

will$ script/console Loading development environment (Rails 2.3.2)

Time.now

=> Thu Jul 30 12:16:30 -0700 2009

1.hour.ago

=> Thu, 30 Jul 2009 18:16:34 UTC +00:00

I can get around this by using something like "Time.now + 1.day" but I'm interested to know what I'm doing wrong with the .ago method.

Maybe there's some sort of time zone setting that I've missed?

Many thanks, Will

Hi,

I'm trying to use the ActiveSupport Time extension but running into a bit of trouble with the time zone.

I'd like to test if a user record has expired using the Time .ago method, such as:

def expired return true if self.activated < 1.day.ago end

Unfortunately I can't seem to use the .ago method in the comparison because it's using UTC time, whereas Time.now has my current time zone (-0700).

will$ script/console Loading development environment (Rails 2.3.2)

Time.now

=> Thu Jul 30 12:16:30 -0700 2009

1.hour.ago

=> Thu, 30 Jul 2009 18:16:34 UTC +00:00

I can get around this by using something like "Time.now + 1.day" but I'm interested to know what I'm doing wrong with the .ago method.

Maybe there's some sort of time zone setting that I've missed?

Maybe. I've never looked, but you can always tack localtime() onto it.

>> Time.now => Thu Jul 30 12:40:31 -0700 2009 >> 1.day.ago => Wed, 29 Jul 2009 19:40:31 UTC 00:00 >> 1.day.ago.localtime => Wed Jul 29 12:40:38 -0700 2009

Great thanks! That's working nicely.

Will

Hi Will,

>> Time.now => Thu Jul 30 12:16:30 -0700 2009

>> 1.hour.ago => Thu, 30 Jul 2009 18:16:34 UTC +00:0

Time.now

=> Thu Jul 30 14:47:09 -500 2009

Time.now.utc

=> Thu Jul 30 19:47:13 UTC 2009

HTH, Bill