# Timezones and Timestamps

**URL:** <https://www.ruby-forum.com/t/timezones-and-timestamps/181903>\
**Category:** Rails\
**Created:** [January 19, 2010, 11:24pm UTC](https://www.ruby-forum.com/t/timezones-and-timestamps/181903 "2010-01-19T23:24:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmjmjm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/898d66/32.png) [@jmjmjm](https://www.ruby-forum.com/u/jmjmjm)\
**Post date:** [January 19, 2010, 11:24pm UTC](https://www.ruby-forum.com/t/timezones-and-timestamps/181903/1 "2010-01-19T23:24:54Z")

</div>

I have a Post model that has a column ‘published\_on’. What I would  
like to do is be able to set the published\_on date in the future so a  
post will automatically become visible to the pubic if the  
published\_on timestamp is before the current timestamp.

I have created a named scope for this so I can do something like:  
@posts = Post.published

Here is the named scope in my Post model:  
named\_scope :published, lambda { { :conditions =\> [‘published\_on \< ?’,  
Time.now] } }

The problem is timezones. I am in the Central timezone (-6). So in my  
environment.rb I have set config.time\_zone = ‘Central Time (US &  
Canada)’.

When I use script/console:

> > Time.now  
> > =\> Tue Jan 19 16:17:37 -0600 2010

> > p = Post.find(1)  
> > =\> #\<Post id: 2, category\_id: nil, title: “This Is The Post Of  
> > Tomorrow”, content: “This post will automatically be published on  
> > 2010-0…”, published\_on: “2010-01-19 18:20:59”, created\_at:  
> > “2010-01-18 22:54:24”, updated\_at: “2010-01-19 18:21:01”\>
> 
> > p.published\_on = Time.now  
> > =\> Tue Jan 19 16:18:32 -0600 2010
> 
> > p.save  
> > =\> true
> 
> > p.published\_on  
> > =\> Tue, 19 Jan 2010 16:18:32 CST -06:00
> 
> > p  
> > =\> #\<Post id: 2, category\_id: nil, title: “This Is The Post Of  
> > Tomorrow”, content: “This post will automatically be published on  
> > 2010-0…”, published\_on: “2010-01-19 22:18:32”, created\_at:  
> > “2010-01-18 22:54:24”, updated\_at: “2010-01-19 22:18:35”\>

So p.published on returns a time of 16:18:32 but in the database we  
see 22:18:32, a difference of six hours (central time).

The named scope generates SQL like:  
SELECT \* FROM `posts` WHERE (published\_on \< ‘2010-01-19 16:21:18’)

16:18:32 is less than 16:21:18, but the database is 22:18:32.

Is there a setting that I need to modify so that the timestamps are  
inserted correctly or selected correctly? What am I doing wrong?  
Thanks!

Which is correct.

---

<div class="post-metadata">

**Author:** ![jmjmjm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/898d66/32.png) [@jmjmjm](https://www.ruby-forum.com/u/jmjmjm)\
**Post date:** [January 20, 2010, 2:44am UTC](https://www.ruby-forum.com/t/timezones-and-timestamps/181903/2 "2010-01-20T02:44:28Z")

</div>

On Jan 19, 2010, at 5:22 PM, jm wrote:

> Time.now] } }  
> Here use Time.now.utc and see if that doesn’t solve your problem.

-Rob

> > > p = Post.find(1)  
> > > p
> 
> To post to this group, send email to rubyonrails-  
> removed\_email\_address@domain.invalid.  
> To unsubscribe from this group, send email to removed\_email\_address@domain.invalid  
> .  
> For more options, visit this group at [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en)  
> .

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

---

<div class="post-metadata">

**Author:** ![jmjmjm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/898d66/32.png) [@jmjmjm](https://www.ruby-forum.com/u/jmjmjm)\
**Post date:** [January 20, 2010, 4:15pm UTC](https://www.ruby-forum.com/t/timezones-and-timestamps/181903/3 "2010-01-20T16:15:04Z")

</div>

That worked perfectly! Thank you!

On Jan 19, 7:42Â pm, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
