I have a default time zone setup for the rails application. And an instance of the Date object.
How can I get make Date#beginning_of_day to return the beginning of the day in the specified time zone, but not my local timezone.
Is there any other method to get beginning of the day time in the specified timezone for the given date?
date = Date.new(2014,10,29)
zone = ActiveSupport::TimeZone.new('CET')
date.foo(zone) # should return "Wed, 29 Oct 2014 00:00:00 CET +01:00"
zone = ActiveSupport::TimeZone.new('UTC')
date.foo(zone) # should return "Wed, 29 Oct 2014 00:00:00 UTC +00:00"
Going off Peder's answer, here's what I did for the PST time zone:
I know this post is old, but what about:
Wed, 02 May 2012 00:00:00 UTC +00:00
=> Wed, 02 May 2012 00:00:00 MSK +04:00
=> Tue, 01 May 2012 00:00:00 AKDT -08:00
NOTICE, it's the WRONG DAY.
What is needed is a way to construct a TimeWithZone in the correct timezone.
I really dislike this, because as far as I can see, I've just changed the system notion of what zone I'm in. The idea is to change my database searches to match the zone of the client... So, I have to save zone and restore it:
It seems like I ought be able to call TimeWithZone.new(), but I didn't figure out how.
Problem is,
Date.beginning_of_day
does not honorTime.zone
in ActiveSupport 2.3Compare https://github.com/rails/rails/blob/v2.3.11/activesupport/lib/active_support/core_ext/date/calculations.rb#L64 (AS 2.3)
to https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/date/calculations.rb#L74 and https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/date/zones.rb#L7 (AS 3)
Date#beginning_of_day
will always return 00:00.But as I understand you want to know time in other time zone while in current time zone is beginning of the day.
So. Let's find out beginning of the day in your current place. Imagine it is Paris, France:
Now lets found out what time is in Moscow:
For different form
now
day:and converting
Date
toDateTime