# Date helper bug?

**URL:** <https://www.ruby-forum.com/t/date-helper-bug/86394>\
**Category:** Rails\
**Created:** [February 21, 2007, 11:41pm UTC](https://www.ruby-forum.com/t/date-helper-bug/86394 "2007-02-21T23:41:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kreiggers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ac91a4/32.png) [@kreiggers](https://www.ruby-forum.com/u/kreiggers)\
**Post date:** [February 21, 2007, 11:41pm UTC](https://www.ruby-forum.com/t/date-helper-bug/86394/1 "2007-02-21T23:41:01Z")

</div>

I’ve found an issue when trying to use the time\_ago\_in\_words and  
distance\_of\_time\_in\_words from action view’s date\_helper.rb

Using SQL Server, I have a datetime column that gets returned as:  
“2007/02/21 09:54:00”. When I pass this to time\_ago\_in\_words (which  
just passes on to distance\_of\_time\_in\_words) it miscalculates the  
difference as “9 hours” (…um yeah, it was 10:54 when I tested this),  
when the difference should have been “1 hour”. So I fired up script/  
console to see what was going on…

# setting up to use the date\_helpers…

> > helper.extend ApplicationHelper  
> > =\> #Object:0x391bfd8

# my record from SQL server returns time like this…

> > t.updated\_on  
> > =\> "“2007/02/20 15:09:04"”
> 
> > t.updated\_on.class  
> > =\> String

# so make a string in same format that represents ~1 hour ago…

> > nine\_54 = “2007/02/21 09:54:00”  
> > =\> “2007/02/21 09:54:00”

# see what the helper gives me… (about 1 hour difference from

Time.now)

> > helper.time\_ago\_in\_words(nine\_54)  
> > =\> “about 9 hours”

# looking at date\_helper.rb I see that parameters passed in are called

with to\_time method…

> > nine\_54.to\_time  
> > =\> Wed Feb 21 09:54:00 UTC 2007
> 
> > Time.now.to\_time  
> > =\> Wed Feb 21 11:04:34 Pacific Standard Time 2007

# A ha! So we’re off by the difference in time zones between PST and

UTC (GMT?). So poking around a bit more…

> > Time.parse(nine\_54)  
> > =\> Wed Feb 21 09:54:00 Pacific Standard Time 2007

Success!

So in my view, all I have to do to get the correct time is use  
time\_ago\_in\_words(Time.parse(updated\_on)) for it to work.

So is this a bug? It is correctly parsing the difference in the times  
when considering the time zone differences, but what about the  
difference between Time.parse and to\_time? Shouldn’t these return the  
same answer?

What about the datetime from SQL server? Is the value in the db stored  
with tzone info, or is to\_time just assuming a defualt tzone of UTC?

---

<div class="post-metadata">

**Author:** ![kreiggers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ac91a4/32.png) [@kreiggers](https://www.ruby-forum.com/u/kreiggers)\
**Post date:** [February 22, 2007, 3:05am UTC](https://www.ruby-forum.com/t/date-helper-bug/86394/2 "2007-02-22T03:05:21Z")

</div>

On Feb 21, 2007, at 2:53 PM, kreiggers wrote:

> I’ve found an issue when trying to use the time\_ago\_in\_words and  
> distance\_of\_time\_in\_words from action view’s date\_helper.rb
> 
> Using SQL Server, I have a datetime column that gets returned as:  
> “2007/02/21 09:54:00”. When I pass this to time\_ago\_in\_words (which  
> just passes on to distance\_of\_time\_in\_words) it miscalculates the  
> difference as “9 hours” (…um yeah, it was 10:54 when I tested this),  
> when the difference should have been “1 hour”. So I fired up script/  
> console to see what was going on…  
>   
> So is this a bug? It is correctly parsing the difference in the times  
> when considering the time zone differences, but what about the  
> difference between Time.parse and to\_time? Shouldn’t these return the  
> same answer?
> 
> What about the datetime from SQL server? Is the value in the db stored  
> with tzone info, or is to\_time just assuming a defualt tzone of UTC?

In your config/environment.rb, find and uncomment:

# Make Active Record use UTC-base instead of local time

config.active\_record.default\_timezone = :utc

So your database times will be UTC.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![kreiggers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ac91a4/32.png) [@kreiggers](https://www.ruby-forum.com/u/kreiggers)\
**Post date:** [February 23, 2007, 7:28pm UTC](https://www.ruby-forum.com/t/date-helper-bug/86394/3 "2007-02-23T19:28:31Z")

</div>

On Feb 21, 6:04 pm, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> In your config/environment.rb, find and uncomment:
> 
> # Make Active Record use UTC-base instead of local time
> 
> config.active\_record.default\_timezone = :utc
> 
> So your database times will be UTC.

The times coming out of the database are already coming out as UTC  
when they’re really PST times… i.e. PST time input → stored in db -

> rails retrieves a UTC time, all along the only thing that changes in  
> this is the tzone (0900 Feb 21 2007 PST → database → 0900 Feb 21  
> 2007 UTC).

I don’t have control of the data in the database. All I can do is read  
the data. Can I force the dates to come out as PST?
