Timezones and Timestamps

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.

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

Here use Time.now.utc and see if that doesn't solve your problem.

-Rob

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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com

That worked perfectly! Thank you!