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.

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-
[email protected]
To unsubscribe from this group, send email to [email protected]
.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
.

Rob B. http://agileconsultingllc.com
[email protected]

That worked perfectly! Thank you!

On Jan 19, 7:42Â pm, Rob B. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs