Rails:在时区获取#beginning_of_day [英] Rails: get #beginning_of_day in time zone

查看:286
本文介绍了Rails:在时区获取#beginning_of_day的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于rails应用程序的默认时区设置。
和Date对象的一个​​实例。

I have a default time zone setup for the rails application. And an instance of the Date object.

如何让Date Date_of_day返回指定时区的一天的开头,但不能我的当地时区。

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"


推荐答案

time_zone = Time.zone # any time zone really
time_zone.local(date.year, date.month, date.day)

问题是, 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 / calculate.rb#L64 (AS 2.3)

to https://github.com/rails/rails/blob/master/ activesupport / lib / active_support / core_ext / date / calculate.rb#L74

https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/date/zones.rb #L7
(AS 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)

这篇关于Rails:在时区获取#beginning_of_day的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆