Rails:获取时区中的#beginning_of_day
我为 Rails 应用程序设置了默认时区。 以及 Date 对象的实例。
我怎样才能让 Date#beginning_of_day 返回指定时区的一天的开始,而不是我的本地时区。
是否有其他方法可以获取给定日期的指定时区的一天时间的开始时间?
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"
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"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
问题是,
Date.beginning_of_day
不支持 ActiveSupport 2.3 中的Time.zone
比较 https://github.com/rails/rails/blob/v2.3.11/activesupport/lib/active_support/core_ext /date/calculations.rb#L64 (AS 2.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)
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
将始终返回 00:00。但据我了解,您想知道其他时区的时间,而当前时区的时间是一天的开始。
所以。让我们看看您当前所在的地方一天的开始时间。想象一下这是法国巴黎:
现在让我们找出莫斯科的时间:
对于不同的形式
now
日:并将
Date
转换为DateTime
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
离开佩德的回答,这是我对 PST 时区所做的事情:
Going off Peder's answer, here's what I did for the PST time zone:
我知道这篇文章很旧,但是:
I know this post is old, but what about:
2012 年 5 月 2 日星期三 00:00:00 UTC +00:00
=> 2012 年 5 月 2 日星期三 00:00:00 MSK +04:00
=> 2012 年 5 月 1 日星期二 00:00:00 AKDT -08:00
注意,今天是错误的一天。
需要的是一种在正确时区构造 TimeWithZone 的方法。
我真的不喜欢这个,因为据我所知,我刚刚更改了我所在区域的系统概念。这个想法是更改我的数据库搜索以匹配该区域的区域客户...
所以,我必须保存区域并恢复它:
看起来我应该能够调用 TimeWithZone.new(),但我不知道如何调用。
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.
正如 Leonid Shevtsov 提到的,
Date.beginning_of_day
不支持 ActiveSupport 2.3 中的Time.zone
我使用的替代方案,如果您卡住使用 Rails 4.0 或 ActiveSupport 2.3,并且您需要使用自定义日期:
结果:
我原来使用 .beginning_of_day 到 .end_of_day 的失败模型范围无法工作:
而且,这就是修复它的原因,因为我无法升级到 Rails 4.0
As Leonid Shevtsov mentioned,
Date.beginning_of_day
does not honorTime.zone
in ActiveSupport 2.3An alternative I used, if your stuck using Rails 4.0 or ActiveSupport 2.3, and you need to use a custom date:
Results:
My original failed model scope using .beginning_of_day to .end_of_day failed to work:
And, this is what fixed it, since I could not upgrade to Rails 4.0
在特定时区生成新日期
如果您想获取特定日期的一天开始时间(并且您知道确切的日期),那么您必须使用:
它尊重季节时间变化:
尽管它已经由接受的引用我想补充一点,我个人更喜欢块符号。
转换现有日期并找出另一个时区的日期
如果您想知道已有的特定时间戳/纪元/日期是哪一天,您需要将现有日期转换为正确的时区, Peder 和 Tim 的答案已经提到了这一点:
请注意,在转换现有日期时,您可能会得到另一个日期天(第 1 日变为第 2 日):
如果您要转换时间,则
Time.now
或Time.current
无关紧要,但我更喜欢使用Time.current
作为规则:Generate new date in a specific time zone
If you would like to get beginning of the day for a specific date (and you know the exact date), then you have to use:
It respects seasonal time change:
Although it is already referred by accepted answer I'd like to add that personally I prefer block notation.
Convert existing and find out what the day it is in another time zone
If you would like to know what day that would be for a specific timestamp/epoch/date that you already have you need to convert your existing date to the proper time zone, which is already referred by Peder's and Tim's answers:
Be aware, when converting existing date you may end up in another day (1st became 2nd):
If you are going to convert time, then
Time.now
orTime.current
is indifferent, but I prefer usingTime.current
as a rule: