I have a record "foo" 데이터베이스에 :start_time 디스크입니다. diskid '' 와 '시간대' 속성.
이 '-' 시간 '은' (예: 01-01-2001 14 20 00 :start_time 맞이하였다. 예를 들어 '는' - '이' 시간대 문자열으로 america/new_york.
I want to create a new 시간 객체에는 :start_time '하지만' 의 값이 그 시간대 지정되는 '시간대'. 싶지 않다 '' 다음 ',' 시간대 읽어들입니다 :start_time 변환하시겠습니까 때문에 될 수 있는 시간을 영리하고 업데이트하십시오 레일 (utc) 에서 일관적이며 시간대.
현재
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
ie. I want to do.
t
=> 2000-01-01 14:20:00 UTC
t.zone = "America/New_York"
=> "America/New_York"
t
=> 2000-01-01 14:20:00 EST
뭔가 하는 소리가 운영까지도 계열에 따라
ActiveSupport::TimeZone.new('America/New_York').local_to_utc(t)
이 분 (현지 시간) 이 변환하십시오 (name. domain. 파워존에서) 를 말한다. 그런 다음 교육 과정의 경우 타임.소네 '세트'
Time.zone.local_to_utc(t)
이 경우 한 모서리까지 대비하여 DST 전환: 현지 시간) 이 지정한 존재하지 않거나 알 수 있습니다.
t = t.asctime.in_time_zone("America/New_York")
다음은 [관한 문서가 아스타임] [1]
[1]: # 내가 아스타임 방법 .0/time.html https://ruby-doc.org/core-2.2
시간 시간 변환한 후 이 데이터베이스에구성원을 오프셋할 추가해야 합니다.
가장 쉬운 방법은 방법이 있습니다.
t = Foo.start_time.in_time_zone("America/New_York")
t -= t.utc_offset
나는 왜 이렇게 하려면 최고의 요소라고 할 수 있는지 알고 있지만, 실제로 그녀의심장을 작동합니까 배 방식으로 만들고 있습니다. 아마 내가 왜 있는지에 대한 일부 시간대 약간만이라도 도움됐네 고객이시간과 활용해야 합니다.
사실 이 subtract 오프셋된 변환한 후 같이 할 것 같군요.
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
Where you are going to use this 시간을 달려 있다.
시간은 특성으로 사용하는 경우, 동일한 date_time_attribute 컴팩트형 사용할 수 있습니다.
class Task
include DateTimeAttribute
date_time_attribute :due_at
end
task = Task.new
task.due_at_time_zone = 'Moscow'
task.due_at # => Mon, 03 Feb 2013 22:00:00 MSK +04:00
task.due_at_time_zone = 'London'
task.due_at # => Mon, 03 Feb 2013 22:00:00 GMT +00:00
Date_time_attribute 컴팩트형 그대로 사용합니다.
my_date_time = DateTimeAttribute::Container.new(Time.zone.now)
my_date_time.date_time # => 2001-02-03 22:00:00 KRAT +0700
my_date_time.time_zone = 'Moscow'
my_date_time.date_time # => 2001-02-03 22:00:00 MSK +0400
난 그냥 그대로 높여줍니까 hotenough. 몇 도우미 메서드을 이 중 하나는 원래 작성자 포스트 dell. (https://stackoverflow.com/q/16818180/936494.) 에서 질문과대답 같은 것입니다.
또한 이러한 방법을 알고 있다 나는 또 내가 몇 문서화했습니다 특이성을 완전히 무시할 수 없는 동안 자동 일광 절약 map_layer 시간 변환에는 사용할 수 있는 out-of-the-box 레일 레임워크:
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
참조용이므로 대한 재판을 받을 수 있는 레일 위의 방법으로 a 클래스 (마치고 스크립트입니다 콘솔이군요 또는 루비 모듈을:
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
ᄋ "이번 도움이 됩니다.
내가 아니라 함께 많은 시간을 보낸 후, 루비 1.9.3 땜질 시간대 어려움을 겪고 있다는 걸 알고 있습니다, t need to # 39 don& 변환하시겠습니까 명명된 시간대 심볼일 전에 변환:
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
이것이 의미하는 것은 적절한 시간 설정 가져오는 집중할 수 있습니다 이 지역의 첫 운영까지도 이에 대한 것 같은데, the way you (적어도 내 머리 속에 내가 파티션에만 it this way), 그리고 마지막에 변환하시겠습니까 영역에 운영까지도 확인할 수 있는 비즈니스 로직을.
또한 루비 2.3.1 근무한다.