Ruby Rails 日历 DST 问题

问题描述 投票:0回答:1

我遇到了 Rails 代码在查看时显示日历条目关闭一小时的问题。数据库中的数据是正确的,所以我确信我遗漏了代码中的某些内容。我已经尝试了很多事情,非常感谢任何帮助。我对 Rails 相当陌生并继承了该应用程序 这是我的代码,正如您可以通过注释掉的部分看到的,我已经尝试过。我也无法让它填充时区。我应该提一下,这只需要是太平洋时间

  #Time.zone = "Pacific Time (US & Canada)"
  #Time.zone_default = Time.find_zone! 'Pacific Time (US & Canada)'

  def convert_virtual_attributes_to_schedule_object

    zone = 'Pacific Time (US & Canada)'
    @start_time = Time.parse("#{start_date} #{start_hours}:#{start_minutes}") rescue nil

    #@start_time = Time.parse("#{start_date} #{start_hours}:#{start_minutes} #{Time.zone}") rescue nil
    #@start_time = start_time - 1.hour if start_time.dst? && !Time.now.dst?
    #@start_time = start_time + 1.hour if Time.now.dst? && start_time.dst?

    @end_time = Time.parse("#{start_date} #{end_hours}:#{end_minutes}") rescue nil

    #ActiveSupport::TimeZone[zone].parse("start_time")
    #tz = ActiveSupport::TimeZone.new 'Pacific Time (US & Canada)'
    #tz.parse("start_time").to_datetime

    unless self.schedule # New event
      self.schedule = Schedule.new(start_time, :duration => end_time - start_time)
    else # Changing old event
      self.schedule.start_time = start_time
      self.schedule.duration = end_time - start_time
    end
  end

我尝试使用 activesupport 来设置时区以及开始时间的计算,例如:

#@start_time = Time.parse("#{start_date} #{start_hours}:#{start_minutes} #{Time.zone}") rescue nil
#@start_time = start_time - 1.hour if start_time.dst? && !Time.now.dst?
#@start_time = start_time + 1.hour if Time.now.dst? && start_time.dst?
ruby-on-rails calendar dst
1个回答
0
投票

我不确定我是否理解正确,但这也许可以帮助你一点:

Time.now
# => 2024-04-10 22:58:32.72156 +0200
Time.now.zone
# => "CEST"

Time.zone = "Pacific Time (US & Canada)"
# => "Pacific Time (US & Canada)"
Time.now
# => 2024-04-10 22:58:46.051782 +0200
Time.now.zone
# => "CEST"
Time.now.in_time_zone
# => Wed, 10 Apr 2024 13:58:57.539545000 PDT -07:00

t = Time.parse("2024-04-10 22:45:31")
# => 2024-04-10 22:45:31 +0200
t.zone
# => "CEST"
t.in_time_zone
# => Wed, 10 Apr 2024 13:45:31.000000000 PDT -07:00
© www.soinside.com 2019 - 2024. All rights reserved.