0

I have a Model with this scope:

scope :next_week, lambda { where("date > (?) and date <= (?)", Date.today, Date.today + 1.weeks ) }

However, it changes the date too early for me, as I want the app's time as (and the server is on UTC):

config.time_zone = 'Eastern Time (US & Canada)'

I've seen posts on using in_time_zone to convert Time.now to the config time.

Why doesn't `config.time_zone` seem to do anything?

Is there anything like that for dates? Or could someone recommend an approach to adjusting my date queries to calculate Date.today to the local date? Thank you!

Community
  • 1
  • 1
yellowreign
  • 3,528
  • 8
  • 43
  • 80

2 Answers2

2

Because Date does not take into account timezones at all you'll need to use the #to_time_in_current_zone method that Rails adds to the Ruby Date object.

From the Docs:

Converts Date to a TimeWithZone in the current zone if Time.zone or Time.zone_default is set, otherwise converts Date to a Time via #to_time

Usage looks like this:

Date.today
# => Mon, 06 May 2013 

Date.today.to_time_in_current_zone
# => Mon, 06 May 2013 00:00:00 CDT -05:00 

There is a fantastic blog post over at Elabs on Working with time zones in Ruby on Rails that gives more great information on the subject.

Dan Reedy
  • 1,872
  • 10
  • 13
  • Thank you Dan. I tried `Date.today.to_time_in_current_zone`, but it came back with `Tue, 07 May 2013 00:00:00 EDT -04:00` even though it is still on May 6th in EST. The info on the linked page was good, but there is also a discussion in the comments that alludes to `Date.today.to_time_in_current_zone` not changing the day as expected. – yellowreign May 07 '13 at 03:12
0

I solved this by using Time.current.to_date instead of Date.today

yellowreign
  • 3,528
  • 8
  • 43
  • 80