26

Railsアプリケーションのデフォルトのタイムゾーン設定があります。そして、Dateオブジェクトのインスタンス。

Date#beginning_of_dayを取得して、指定したタイムゾーンで1日の始まりを返すようにするにはどうすればよいですか。ただし、ローカルタイムゾーンでは返しません。

指定された日付の指定されたタイムゾーンで1日の始まりを取得する他の方法はありますか?

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"
4

8 に答える 8

49
DateTime.now.in_time_zone(Time.zone).beginning_of_day
于 2011-09-21T06:58:19.987 に答える
20
time_zone = Time.zone # any time zone really
time_zone.local(date.year, date.month, date.day)

問題は、ActiveSupport2.3Date.beginning_of_dayでは尊重されないことです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)

于 2011-03-24T13:23:53.187 に答える
9

Date#beginning_of_day常に00:00を返します。

しかし、私が理解しているように、現在のタイムゾーンが1日の始まりである間に、他のタイムゾーンの時間を知りたいと思います。

それで。あなたの現在の場所で一日の始まりを見つけましょう。フランスのパリだと想像してみてください。

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

さて、モスクワの何時かを調べましょう。

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 

別のフォーム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)

に変換DateしますDateTime

date = Date.new(2011,1,3).to_datetime.change(:offset => "EST")
于 2011-03-24T12:54:55.027 に答える
4

Pederの答えから離れて、PSTタイムゾーンで行ったことは次のとおりです。

DateTime.now.in_time_zone("Pacific Time (US & Canada)").beginning_of_day
于 2016-04-07T04:12:53.077 に答える
1
may2 = Date.new(2012,5,2)
midnight = Time.zone.local(may2.year,may2.month,may2.day).beginning_of_day

2012年5月2日水曜日00:00:00UTC+00:00

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

=>2012年5月2日水曜日00:00:00MSK+04:00

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

=>2012年5月1日火曜日00:00:00AKDT-08:00

注意、それは間違った日です。

必要なのは、正しいタイムゾーンでTimeWithZoneを作成する方法です。

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

私はこれが本当に嫌いです。なぜなら、私が見る限り、自分がいるゾーンのシステム概念を変更したばかりだからです。データベース検索をクライアントのゾーンに一致するように変更するという考えです...だから、私はゾーンを保存して復元する必要があります。

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

TimeWithZone.new()を呼び出すことができるはずですが、その方法がわかりませんでした。

于 2013-01-30T22:34:11.883 に答える
1

この投稿が古いことは知っていますが、どうでしょうか。

Time.zone.parse("12am")
于 2017-02-20T18:59:12.313 に答える
0
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
于 2013-07-29T09:16:21.733 に答える
0

Leonid Shevtsovが述べたように、ActiveSupport2.3Date.beginning_of_dayでは尊重しませんTime.zone

Rails4.0またはActiveSupport2.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

結果:

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 

.beginning_of_dayから.end_of_dayを使用した元の失敗したモデルスコープが機能しませんでした:

scope :on_day, ->(date) { where( created_at: date.beginning_of_day..date.end_of_day ) }

そして、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 ) }
于 2016-02-17T23:58:24.863 に答える