Rails: get #beginning_of_day in time zone

2019-03-14 11:41发布

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"

8条回答
小情绪 Triste *
2楼-- · 2019-03-14 11:56

Going off Peder's answer, here's what I did for the PST time zone:

DateTime.now.in_time_zone("Pacific Time (US & Canada)").beginning_of_day
查看更多
甜甜的少女心
3楼-- · 2019-03-14 11:59

I know this post is old, but what about:

Time.zone.parse("12am")
查看更多
做个烂人
4楼-- · 2019-03-14 12:01
may2 = Date.new(2012,5,2)
midnight = Time.zone.local(may2.year,may2.month,may2.day).beginning_of_day

Wed, 02 May 2012 00:00:00 UTC +00:00

midnight = Time.zone.local(may2.year,may2.month,may2.day).in_time_zone("Moscow").beginning_of_day

=> Wed, 02 May 2012 00:00:00 MSK +04:00

midnight = Time.zone.local(may2.year,may2.month,may2.day).in_time_zone("Alaska").beginning_of_day

=> 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.

Time.zone="Alaska"
midnight = Time.zone.local(may2.year,may2.month,may2.day)

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:

foo = Time.zone; Time.zone="Alaska"; midnight = Time.zone.local(may2.year,may2.month,may2.day); Time.zone = foo

It seems like I ought be able to call TimeWithZone.new(), but I didn't figure out how.

查看更多
beautiful°
6楼-- · 2019-03-14 12:05

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:

bd = DateTime.now.in_time_zone('Paris').beginning_of_day
# or just
bd = DateTime.now.in_time_zone(1).beginning_of_day
#=> Thu, 24 Mar 2011 00:00:00 WET +01:00

Now lets found out what time is in Moscow:

moscow_time = bd.in_time_zone("Moscow") # or in_time_zone(3)
#=> Thu, 24 Mar 2011 02:00:00 AST +03:00
london_time = bd.in_time_zone("London")
#=> Wed, 23 Mar 2011 23:00:00 GMT +00:00
kyiv_time = bd.in_time_zone("Kyiv")
#=> Thu, 24 Mar 2011 01:00:00 EET +02:00 

For different form now day:

# You even shouldn't call now, because it by default will be 00:00
date = DateTime(2011, 1, 3).in_time_zone("-10")
# or
date = DateTime.new(2011,1,3,0,0,0,"-10")
# and same way as above
moscow_time = date.in_time_zone("Moscow") # or in_time_zone(3)

and converting Date to DateTime

date = Date.new(2011,1,3).to_datetime.change(:offset => "EST")
查看更多
贪生不怕死
7楼-- · 2019-03-14 12:14
DateTime.now.in_time_zone(Time.zone).beginning_of_day
查看更多
登录 后发表回答