Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在rails中使用Time.zone.now覆盖Time.now_Ruby On Rails_Ruby_Timezone - Fatal编程技术网

Ruby on rails 如何在rails中使用Time.zone.now覆盖Time.now

Ruby on rails 如何在rails中使用Time.zone.now覆盖Time.now,ruby-on-rails,ruby,timezone,Ruby On Rails,Ruby,Timezone,你好,我正在从事rails项目。我想要特定区域的时间,我知道我可以使用time.zone.now 但是我想先设置区域,然后根据区域得到时间。是否有任何方法可以在设置区域后使用Time.zone.now覆盖Time.now方法 我曾尝试在应用程序_controller.rb中创建一个before操作,然后定义区域,但每当我尝试访问时间时,它总是返回没有时区的时间。请帮帮我。提前准备好 application_controller.rb def set_current_time_zone Tim

你好,我正在从事rails项目。我想要特定区域的时间,我知道我可以使用
time.zone.now
但是我想先设置区域,然后根据区域得到时间。是否有任何方法可以在设置区域后使用
Time.zone.now
覆盖
Time.now
方法

我曾尝试在应用程序_controller.rb中创建一个before操作,然后定义区域,但每当我尝试访问时间时,它总是返回没有时区的时间。请帮帮我。提前准备好

application_controller.rb
def set_current_time_zone
  Time.zone = current_user.time_zone unless current_user.blank?
end

为什么不使用当前时间?如果您在
config.zone
time.zone
中设置了时区,它将为您提供时区时间

请参见示例:

2.5.0 :021 > Time.zone = "Tallinn"
  => "Tallinn" 
2.5.0 :022 > Time.current
  => Wed, 21 Feb 2018 20:37:29 EET +02:00 
2.5.0 :023 > Time.zone = "New Delhi"
  => "New Delhi" 
2.5.0 :024 > Time.current
  => Thu, 22 Feb 2018 00:07:38 IST +05:30 

请参阅相应的apidock about method和about Ruby timezones中的一篇好文章。

为什么不使用
Time.current
?如果您在
config.zone
time.zone
中设置了时区,它将为您提供时区时间

请参见示例:

2.5.0 :021 > Time.zone = "Tallinn"
  => "Tallinn" 
2.5.0 :022 > Time.current
  => Wed, 21 Feb 2018 20:37:29 EET +02:00 
2.5.0 :023 > Time.zone = "New Delhi"
  => "New Delhi" 
2.5.0 :024 > Time.current
  => Thu, 22 Feb 2018 00:07:38 IST +05:30 

请参阅相应的apidock about method和about Ruby timezones中的一篇好文章。

我就是这样做的,下面是Ryan Bates的railscast:

class ApplicationController
use_zone方法需要一个块,并为该块的持续时间设置时区。请求完成时,原始时区将被设置回原来的时区


然后,我可以使用Time.zone.now,它将始终使用为用户设置的正确时区(Time\u zone方法返回用户在其设置或UTC中配置的时区)。在控制器或模型中处理时,表单中的所有日期和时间字段也将在当前用户的时区中处理。

我就是这样做的,Ryan Bates的railscast位于:

class ApplicationController
use_zone方法需要一个块,并为该块的持续时间设置时区。请求完成时,原始时区将被设置回原来的时区

然后,我可以使用Time.zone.now,它将始终使用为用户设置的正确时区(Time\u zone方法返回用户在其设置或UTC中配置的时区)。在控制器或模型中处理时,表单中的所有日期和时间字段也将在当前用户的时区中处理。

希望这会有所帮助

module TimeOverride
  # overriding time to return time accoring to the application configured 
  #  timezone
  # instead of utc timezone
  def now
    super.getlocal(Time.zone.utc_offset)
  end
end

module DateTimeOverride
  # overriding time to return time accoring to the application configured 
  #timezone
  # instead of utc timezone
  def now
    Time.now.to_datetime
  end
end

Time.singleton_class.send(:prepend, TimeOverride)
DateTime.singleton_class.send(:prepend, DateTimeOverride)
希望这会有所帮助

module TimeOverride
  # overriding time to return time accoring to the application configured 
  #  timezone
  # instead of utc timezone
  def now
    super.getlocal(Time.zone.utc_offset)
  end
end

module DateTimeOverride
  # overriding time to return time accoring to the application configured 
  #timezone
  # instead of utc timezone
  def now
    Time.now.to_datetime
  end
end

Time.singleton_class.send(:prepend, TimeOverride)
DateTime.singleton_class.send(:prepend, DateTimeOverride)

Time
类中,重写
now
方法,或者只需使用返回
timezone.now
-)的\u timezone
方法添加
),在
Time
类中,重写
now
方法或,只需添加
和_timezone
方法,该方法返回
timezone.now
-))是的,我已经实现了相同的方法,但项目太大,我无法将Time.now替换为Time.zone.now。我想覆盖Time.now到Time.zone.now,这样我就不必更改everywhere@awsmsid如果你用猴子修补ruby核心库来产生不同的行为,那么你正在为任何其他加入该项目的人制造一场噩梦。不要那样做。只需使用IDE对整个项目进行grep,并将
Time.now
的所有实例替换为
Time.zone.now
。是的,我已经实现了相同的功能,但项目太大,无法将Time.now替换为Time.zone.now。我想覆盖Time.now到Time.zone.now,这样我就不必更改everywhere@awsmsid如果你用猴子修补ruby核心库来产生不同的行为,那么你正在为任何其他加入该项目的人制造一场噩梦。不要那样做。只要用IDE将整个项目grep,并将
Time.now
的所有实例替换为
Time.zone.now
。是的,兄弟,我同意,但我不想更改时间。now to Time.current我想更改时间。now to Time.current我想更改时间。now,这样我就不必在任何地方更改。是的,兄弟,我同意,但我不想更改时间。now to Time.current我想更改时间时间。现在,这样我就不必在任何地方改变。