# Bug regarding ActiveRecord and TimeWithZone

**URL:** <https://www.ruby-forum.com/t/bug-regarding-activerecord-and-timewithzone/158449>\
**Category:** Rails\
**Created:** [January 23, 2009, 1:22pm UTC](https://www.ruby-forum.com/t/bug-regarding-activerecord-and-timewithzone/158449 "2009-01-23T13:22:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tuktuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/77aa72/32.png) [@tuktuk](https://www.ruby-forum.com/u/tuktuk)\
**Post date:** [January 23, 2009, 1:22pm UTC](https://www.ruby-forum.com/t/bug-regarding-activerecord-and-timewithzone/158449/1 "2009-01-23T13:22:26Z")

</div>

I’ve discovered a strange behavior regarding ActiveRecord and  
TimeWithZone

I’ve seen the error when using SQL Server, I have not yet tried with any  
other database.

Here’s output from the console - “User” is a simple model made with a  
migration.

* * *

> > d1 = User.find(:first).created\_at  
> > =\> Thu, 21 Jun 2001 19:11:57 CEST +02:00
> 
> > d2 = Time.zone.now  
> > =\> Fri, 23 Jan 2009 12:02:00 CET +01:00
> 
> > d1.class  
> > =\> ActiveSupport::TimeWithZone
> 
> > d2.class  
> > =\> ActiveSupport::TimeWithZone
> 
> > d2 - d1  
> > =\> Thu Aug 04 17:50:03 UTC 1977
> 
> > d1 - d2  
> > TypeError: expected numeric or date  
> > from /usr/lib/ruby/1.8/date.rb:1252:in `minus_without_duration' from /usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/core_ext/date/calculations.rb:88:in`-’  
> > from  
> > /usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active\_support/time\_with\_zone.rb:201:in  
> > `-’  
> > from (irb):11

* * *

The strange thing is, that “d1” (the ActiveRecord attribute) reports  
itself as a TimeWithZone class, but it isn’t. If I do this instead

> > d1 = Time.zone.now  
> > d2 = Time.zone.now  
> > d1 - d2

I get no errors.

So what exactly is happening in ActiveRecord? Is it a problem in the  
adapter?

- Carsten

---

<div class="post-metadata">

**Author:** ![tuktuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/77aa72/32.png) [@tuktuk](https://www.ruby-forum.com/u/tuktuk)\
**Post date:** [January 26, 2009, 8:53am UTC](https://www.ruby-forum.com/t/bug-regarding-activerecord-and-timewithzone/158449/2 "2009-01-26T08:53:26Z")

</div>

I will allow myself to bump this question just once, since I think it is  
important - it really seems like a bug, but I would like to know, if  
anyone else has experienced this before submitting to the bug list.

- Carsten

Carsten G. wrote:

> I’ve discovered a strange behavior regarding ActiveRecord and  
> TimeWithZone
> 
> * * *
> 
> > > d1 = User.find(:first).created\_at  
> > > =\> Thu, 21 Jun 2001 19:11:57 CEST +02:00
> > 
> > > d2 = Time.zone.now  
> > > =\> Fri, 23 Jan 2009 12:02:00 CET +01:00
> > 
> > > d1.class  
> > > =\> ActiveSupport::TimeWithZone
> > 
> > > d2.class  
> > > =\> ActiveSupport::TimeWithZone
> > 
> > > d2 - d1  
> > > =\> Thu Aug 04 17:50:03 UTC 1977
> > 
> > > d1 - d2  
> > > TypeError: expected numeric or date  
> > > from /usr/lib/ruby/1.8/date.rb:1252:in `minus_without_duration' from /usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/core_ext/date/calculations.rb:88:in`-’  
> > > from  
> > > /usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active\_support/time\_with\_zone.rb:201:in  
> > > `-’  
> > > from (irb):11
> 
> * * *

---

<div class="post-metadata">

**Author:** ![tuktuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/77aa72/32.png) [@tuktuk](https://www.ruby-forum.com/u/tuktuk)\
**Post date:** [January 26, 2009, 9:16am UTC](https://www.ruby-forum.com/t/bug-regarding-activerecord-and-timewithzone/158449/3 "2009-01-26T09:16:34Z")

</div>

On Mon, Jan 26, 2009 at 9:53 AM, Carsten G. \<  
removed\_email\_address@domain.invalid\> wrote:

> > ## =\> Thu Aug 04 17:50:03 UTC 1977 `-’ from (irb):11
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

Carsten

I don’t get that error using Edge Rails

> > d1 = u.created\_at  
> > =\> Sat, 03 Jan 2009 06:06:44 UTC +00:00  
> > d2 = Time.zone.now  
> > =\> Mon, 26 Jan 2009 08:14:33 UTC +00:00  
> > d1.class  
> > =\> ActiveSupport::TimeWithZone  
> > d2.class  
> > =\> ActiveSupport::TimeWithZone  
> > d2 - d1  
> > =\> 1994869.32909489  
> > d1 - d2  
> > =\> -1994869.32909489

–  
Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain

---

<div class="post-metadata">

**Author:** ![tuktuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/77aa72/32.png) [@tuktuk](https://www.ruby-forum.com/u/tuktuk)\
**Post date:** [January 26, 2009, 11:29am UTC](https://www.ruby-forum.com/t/bug-regarding-activerecord-and-timewithzone/158449/4 "2009-01-26T11:29:58Z")

</div>

Andrew T. wrote:

> I don’t get that error using Edge Rails

Whoops - should have tried that first… Sorry 🙂 And thanks.

- Carsten
