121

と属性fooを持つデータベースにレコードがあります。:start_time:timezone

:start_timeUTC の時刻です -2001-01-01 14:20:00たとえば。は:timezone文字列です -America/New_Yorkたとえば。

の値を持つ新しい Time オブジェクトを作成したいのです:start_timeが、そのタイムゾーンは で指定されてい:timezoneます。Rails は賢く、UTC からの時刻をそのタイムゾーンと一致するように更新するため、をロードして:start_timeから に変換したくありません。:timezone

現在、

t = foo.start_time
=> 2000-01-01 14:20:00 UTC
t.zone
=> "UTC"
t.in_time_zone("America/New_York")
=> Sat, 01 Jan 2000 09:20:00 EST -05:00

むしろ見たい

=> Sat, 01 Jan 2000 14:20:00 EST -05:00

すなわち。私はやってみたいです:

t
=> 2000-01-01 14:20:00 UTC
t.zone = "America/New_York"
=> "America/New_York"
t
=> 2000-01-01 14:20:00 EST
4

12 に答える 12

84

次のようなものが欲しいようです

ActiveSupport::TimeZone.new('America/New_York').local_to_utc(t)

これは、この現地時間を (ゾーンを使用して) utc に変換することを示しています。設定した場合Time.zoneは、もちろん次のことができます

Time.zone.local_to_utc(t)

これは、t に関連付けられたタイムゾーンを使用しません。変換元のタイムゾーンに対してローカルであると想定します。

ここで警戒すべきエッジ ケースの 1 つは DST 移行です。指定した現地時間が存在しないか、あいまいである可能性があります。

于 2013-05-29T16:14:37.757 に答える
6

実際には、次のように、変換後にオフセットを差し引く必要があると思います。

1.9.3p194 :042 > utc_time = Time.now.utc
=> 2013-05-29 16:37:36 UTC
1.9.3p194 :043 > local_time = utc_time.in_time_zone('America/New_York')
 => Wed, 29 May 2013 12:37:36 EDT -04:00
1.9.3p194 :044 > desired_time = local_time-local_time.utc_offset
 => Wed, 29 May 2013 16:37:36 EDT -04:00 
于 2013-05-29T16:39:57.800 に答える
1
def relative_time_in_time_zone(time, zone)
   DateTime.parse(time.strftime("%d %b %Y %H:%M:%S #{time.in_time_zone(zone).formatted_offset}"))
end

仕事を解決するために思いついた簡単な小さな機能。誰かがこれを行うより効率的な方法を持っている場合は、投稿してください!

于 2015-07-03T00:50:23.640 に答える
0

Ruby / Rails - Change the timezone of a Time, without changing the value .

また、私が観察したいくつかの特異点を文書化しました。また、これらのヘルパーには、Rails フレームワークですぐに使用できない時間変換中に適用される自動夏時間調整を完全に無視するメソッドが含まれています。

  def utc_offset_of_given_time(time, ignore_dst: false)
    # Correcting the utc_offset below
    utc_offset = time.utc_offset

    if !!ignore_dst && time.dst?
      utc_offset_ignoring_dst = utc_offset - 3600 # 3600 seconds = 1 hour
      utc_offset = utc_offset_ignoring_dst
    end

    utc_offset
  end

  def utc_offset_of_given_time_ignoring_dst(time)
    utc_offset_of_given_time(time, ignore_dst: true)
  end

  def change_offset_in_given_time_to_given_utc_offset(time, utc_offset)
    formatted_utc_offset = ActiveSupport::TimeZone.seconds_to_utc_offset(utc_offset, false)

    # change method accepts :offset option only on DateTime instances.
    # and also offset option works only when given formatted utc_offset
    # like -0500. If giving it number of seconds like -18000 it is not
    # taken into account. This is not mentioned clearly in the documentation
    # , though.
    # Hence the conversion to DateTime instance first using to_datetime.
    datetime_with_changed_offset = time.to_datetime.change(offset: formatted_utc_offset)

    Time.parse(datetime_with_changed_offset.to_s)
  end

  def ignore_dst_in_given_time(time)
    return time unless time.dst?

    utc_offset = time.utc_offset

    if utc_offset < 0
      dst_ignored_time = time - 1.hour
    elsif utc_offset > 0
      dst_ignored_time = time + 1.hour
    end

    utc_offset_ignoring_dst = utc_offset_of_given_time_ignoring_dst(time)

    dst_ignored_time_with_corrected_offset =
      change_offset_in_given_time_to_given_utc_offset(dst_ignored_time, utc_offset_ignoring_dst)

    # A special case for time in timezones observing DST and which are
    # ahead of UTC. For e.g. Tehran city whose timezone is Iran Standard Time
    # and which observes DST and which is UTC +03:30. But when DST is active
    # it becomes UTC +04:30. Thus when a IRDT (Iran Daylight Saving Time)
    # is given to this method say '05-04-2016 4:00pm' then this will convert
    # it to '05-04-2016 5:00pm' and update its offset to +0330 which is incorrect.
    # The updated UTC offset is correct but the hour should retain as 4.
    if utc_offset > 0
      dst_ignored_time_with_corrected_offset -= 1.hour
    end

    dst_ignored_time_with_corrected_offset
  end

上記のメソッドをクラスまたはモジュールにラップした後、Rails コンソールまたは Ruby スクリプトで試すことができる例:

dd1 = '05-04-2016 4:00pm'
dd2 = '07-11-2016 4:00pm'

utc_zone = ActiveSupport::TimeZone['UTC']
est_zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
tehran_zone = ActiveSupport::TimeZone['Tehran']

utc_dd1 = utc_zone.parse(dd1)
est_dd1 = est_zone.parse(dd1)
tehran_dd1 = tehran_zone.parse(dd1)

utc_dd1.dst?
est_dd1.dst?
tehran_dd1.dst?

ignore_dst = true
utc_to_est_time = utc_dd1.in_time_zone(est_zone.name)
if utc_to_est_time.dst? && !!ignore_dst
  utc_to_est_time = ignore_dst_in_given_time(utc_to_est_time)
end

puts utc_to_est_time

お役に立てれば。

于 2016-12-13T11:25:12.360 に答える
0

私は TimeZones にもかなりの時間を費やしました.Ruby 1.9.3をいじった後、変換する前に名前付きのタイムゾーンシンボルに変換する必要がないことに気付きました:

my_time = Time.now
west_coast_time = my_time.in_time_zone(-8) # Pacific Standard Time
east_coast_time = my_time.in_time_zone(-5) # Eastern Standard Time

これが意味することは、最初に必要な地域で適切な時間設定を取得することに集中でき、それについて考える方法で (少なくとも私の頭ではこのように分割します)、最後にゾーンに変換することができるということです。ビジネスロジックを検証したい。

これは Ruby 2.3.1 でも機能します。

于 2017-01-10T23:07:09.943 に答える