【问题标题】:Rails time zone not overrides as wellRails 时区也不会覆盖
【发布时间】:2011-12-11 23:33:54
【问题描述】:

我在我的 Rails 项目中遇到了 UTC 的嘈杂问题。

class ApplicationController < ActionController::Base
   before_filter :set_timezone

   def set_timezone
     Time.zone = current_user.time_zone if current_user
   end

酷。我覆盖了时区。 现在,服务器的时区是+3。用户的时区是+5。我希望对 Time 的任何请求都应该获取用户的时区,但此代码返回的不是预期值:

render :text => Time.zone.to_s + "<br/>" +
                Time.now.to_s + "<br/>" +
                Time.now.in_time_zone.to_s

结果:

(GMT+05:00) Tashkent
Thu Oct 20 19:41:11 +0300 2011
2011-10-20 21:41:11 +0500

+0300 偏移从哪里来??

【问题讨论】:

    标签: ruby-on-rails timezone utc


    【解决方案1】:

    要在当前设置的时区中获取当前时间,您可以使用

    Time.zone.now
    

    您的服务器时区是 +3 并且

    Time.now.to_s   is returning this 
    

    【讨论】:

      【解决方案2】:

      萨哈!抱歉,我没有 15 点声望让你升级))。 无论如何感谢您的帮助。 我写了一个 TimeUtil 助手,用于时间校正。这是我目前的伪代码:

      class RacesController < ApplicationController
        def create
          @race = Race.new(params[:race])
          @race.correct_time_before_save   #can be before_save
          @race.save
        end
      
      class Race < ActiveRecord::Base
        def correct_time_before_save
          date = self.attributes["race_date"]
          time = self.attributes["race_time"]
          datetime = Time.local(date.year, date.month, date.day, time.hour, time.min, time.sec)
          datetime_corrected = TimeUtil::override_offset(datetime)
          self.race_date = datetime_corrected.to_date
          self.race_time = datetime_corrected.to_time
        end
      
      # TimeUtil is uses for time correction. It should be very clear, please read description before using.
      # It's for time correction, when server's and user's time zones are different.
      # Example: User lives in Madrid where UTC +1 hour, Server had deployed in New York, UTC -5 hours.
      # When user say: I want this race to be started in 10:00.
      # Server received this request, and say: Okay man, I can do it!
      # User expects to race that should be started in 10:00 (UTC +1hour) = 09:00 UTC+0
      # Server will start the race in 10:00 (UTC -5 hour) = 15:00 UTC+0
      #
      # This module brings the workaround. All that you need is to make a preprocess for all incoming Time data from users.
      # Check the methods descriptions for specific info.
      #
      # The Time formula is:
      #                       UTC + offset = local_time
      #                    or
      #                       UTC = local_time - offset
      #
      
      module TimeUtil
      
      # It returns the UTC+0 DateTime object, that computed from incoming parameter  "datetime_arg".
      # The offset data in "datetime_arg" is ignored - it replaces with Time.zone offset.
      # Time.zone offset initialized in ApplicationController::set_timezone before-filter method.
      #
      def self.override_offset datetime_arg      
        Time.zone.parse(datetime_arg.strftime("%D %T")).utc
      end
      

      ActiveRecord getter 也适应用户的时区。时间以“utc+0”格式存储在数据库(mysql)中,我们想以当前用户的时区格式获取这个时间:

      class Race < ActiveRecord::Base
        def race_date
          date = self.attributes["race_date"]
          time = self.attributes["race_time"]
          datetime = Time.utc(date.year, date.month, date.day, time.hour, time.min, time.sec).in_time_zone
          datetime.to_date
        end
      
        def race_time
          date = self.attributes["race_date"]
          time = self.attributes["race_time"]
          datetime = Time.utc(date.year, date.month, date.day, time.hour, time.min, time.sec).in_time_zone
          datetime.to_time
        end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-26
        • 1970-01-01
        • 2019-04-20
        • 1970-01-01
        • 2021-09-01
        • 1970-01-01
        • 2010-12-22
        • 1970-01-01
        相关资源
        最近更新 更多