Reputation: 53319
I have a Date object like this:
>> the_date
=> Tue, 12 Jun 2012
>> the_date.class
=> Date
And a timezone stored as a string:
>> tz = "Pacific Time (US & Canada)"
=> "Pacific Time (US & Canada)"
And I'm looking to generate an ActiveSupport::TimeWithZone at midnight of the given date in the given timezone (not midnight of the given date in utc, then converted to the given timezone). The best way I've found to do this so far is exceedingly ugly:
>> the_time = ActiveSupport::TimeZone.new(tz).parse(the_date.to_s)
=> Tue, 12 Jun 2012 00:00:00 PDT -07:00
>> the_time.class
=> ActiveSupport::TimeWithZone
There's got to be a better way to generate this! Anyone know how to do this?
Upvotes: 2
Views: 390
Reputation: 3965
Not really much better, but different than your solution:
the_date.to_time.in_time_zone
#=> Mon, 11 Jun 2012 22:00:00 UTC +00:00
the_date.to_time.in_time_zone(tz)
#=> Mon, 11 Jun 2012 15:00:00 PDT -07:00
Time.zone = tz
#=> "Pacific Time (US & Canada)"
the_date.to_time.in_time_zone
#=> Mon, 11 Jun 2012 15:00:00 PDT -07:00
the_date.to_time.in_time_zone.end_of_day
#=> Mon, 11 Jun 2012 23:59:59 PDT -07:00
(the_date.to_time.in_time_zone + 1.day).beginning_of_day
#=> Tue, 12 Jun 2012 00:00:00 PDT -07:00
Upvotes: 1