Ruby-on-rails Rails:在时区获取#beginning_of_day
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5419148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails: get #beginning_of_day in time zone
提问by Bogdan Gusiev
I have a default time zone setup for the rails application. And an instance of the Date object.
我为 rails 应用程序设置了默认时区。以及 Date 对象的一个实例。
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.
我怎样才能让 Date#beginning_of_day 返回指定时区的一天的开始,但不是我的本地时区。
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"
采纳答案by Leonid Shevtsov
time_zone = Time.zone # any time zone really
time_zone.local(date.year, date.month, date.day)
Problem is, Date.beginning_of_daydoes not honor Time.zonein ActiveSupport 2.3
问题是,Date.beginning_of_day不支持Time.zoneActiveSupport 2.3
Compare 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#L74and https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/date/zones.rb#L7(AS 3)
到https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/date/calculations.rb#L74和 https://github.com/rails/rails/blob/master/activesupport /lib/active_support/core_ext/date/zones.rb#L7(AS 3)
回答by Peder
DateTime.now.in_time_zone(Time.zone).beginning_of_day
回答by fl00r
Date#beginning_of_daywill always return 00:00.
Date#beginning_of_day将始终返回 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 nowday:
对于不同的形式now日:
# 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 Dateto DateTime
并转换Date为DateTime
date = Date.new(2011,1,3).to_datetime.change(:offset => "EST")
回答by Tim S
Going off Peder's answer, here's what I did for the PST time zone:
离开 Peder 的回答,这是我为 PST 时区所做的:
DateTime.now.in_time_zone("Pacific Time (US & Canada)").beginning_of_day
回答by Jason Galuten
I know this post is old, but what about:
我知道这篇文章很旧,但是呢:
Time.zone.parse("12am")
回答by mcr
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
2012 年 5 月 2 日,星期三 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
=> 2012 年 5 月 2 日,星期三 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
=> 2012 年 5 月 1 日,星期二 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.
需要的是一种在正确的时区中构造 TimeWithZone 的方法。
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 systemnotion 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.
似乎我应该能够调用 TimeWithZone.new(),但我不知道如何调用。
回答by Dmitry Lihachev
ActiveSupport::TimeZone['Europe/London'].parse('30.07.2013') # 2013-07-29 23:00:00 UTC
ActiveSupport::TimeZone['Asia/Magadan'].parse('30.07.2013') # 2013-07-29 12:00:00 UTC
回答by Eric Wanchic
As Leonid Shevtsov mentioned, Date.beginning_of_daydoes not honor Time.zonein ActiveSupport 2.3
正如 Leonid Shevtsov 提到的,在 ActiveSupport 2.3Date.beginning_of_day中不兑现Time.zone
An alternative I used, if your stuck using Rails 4.0 or ActiveSupport 2.3, and you need to use a custom date:
如果您坚持使用 Rails 4.0 或 ActiveSupport 2.3,并且您需要使用自定义日期,则我使用了另一种方法:
date = Date.new(2014,10,29)
date.to_time.change(hour: 0, min: 0, sec: 0).in_time_zone #.beginning_of_day
date.to_time.change(hour: 23, min: 59, sec: 59).in_time_zone #.end_of_day
Results:
结果:
2.0.0-p247 :001 > date = Date.new(2014,10,29)
=> Wed, 29 Oct 2014
2.0.0-p247 :002 > date.to_time.change(hour: 0, min: 0, sec: 0)
=> 2014-10-29 00:00:00 -0500
2.0.0-p247 :003 > date.to_time.change(hour: 0, min: 0, sec: 0).in_time_zone
=> Wed, 29 Oct 2014 05:00:00 UTC +00:00
2.0.0-p247 :004 > date.to_time.change(hour: 23, min: 59, sec: 59)
=> 2014-10-29 23:59:59 -0500
2.0.0-p247 :005 > date.to_time.change(hour: 23, min: 59, sec: 59).in_time_zone
=> Thu, 30 Oct 2014 04:59:59 UTC +00:00
My original failed model scope using .beginning_of_day to .end_of_day failed to work:
我最初使用 .beginning_of_day 到 .end_of_day 失败的模型范围无法工作:
scope :on_day, ->(date) { where( created_at: date.beginning_of_day..date.end_of_day ) }
And, this is what fixed it, since I could not upgrade to Rails 4.0
而且,这就是修复它的原因,因为我无法升级到 Rails 4.0
scope :on_day, ->(date) { where( created_at: date.to_time.change(hour: 0, min: 0, sec: 0).in_time_zone..date.to_time.change(hour: 23, min: 59, sec: 59).in_time_zone ) }

