# Time.now and 1.day.ago style comparison

**URL:** <https://www.ruby-forum.com/t/time-now-and-1-day-ago-style-comparison/172990>\
**Category:** Rails\
**Created:** [July 30, 2009, 9:39pm UTC](https://www.ruby-forum.com/t/time-now-and-1-day-ago-style-comparison/172990 "2009-07-30T21:39:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![will](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/90ced4/32.png) [@will](https://www.ruby-forum.com/u/will)\
**Post date:** [July 30, 2009, 9:39pm UTC](https://www.ruby-forum.com/t/time-now-and-1-day-ago-style-comparison/172990/1 "2009-07-30T21:39:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![will](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/90ced4/32.png) [@will](https://www.ruby-forum.com/u/will)\
**Post date:** [July 30, 2009, 9:43pm UTC](https://www.ruby-forum.com/t/time-now-and-1-day-ago-style-comparison/172990/2 "2009-07-30T21:43:54Z")

</div>

On Jul 30, 2009, at 12:38 PM, Will wrote:

> return true if self.activated \< 1.day.ago  
> =\> 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

---

<div class="post-metadata">

**Author:** ![will](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/90ced4/32.png) [@will](https://www.ruby-forum.com/u/will)\
**Post date:** [July 30, 2009, 9:49pm UTC](https://www.ruby-forum.com/t/time-now-and-1-day-ago-style-comparison/172990/3 "2009-07-30T21:49:10Z")

</div>

Great thanks! That’s working nicely.

Will

---

<div class="post-metadata">

**Author:** ![will](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/90ced4/32.png) [@will](https://www.ruby-forum.com/u/will)\
**Post date:** [July 30, 2009, 9:50pm UTC](https://www.ruby-forum.com/t/time-now-and-1-day-ago-style-comparison/172990/4 "2009-07-30T21:50:56Z")

</div>

Hi Will,

On Thu, 2009-07-30 at 12:38 -0700, Will wrote:

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