Convierte la hora de una zona horaria a otra en Rails


Mis created_at marcas de tiempo se almacenan en UTC:

>> Annotation.last.created_at
=> Sat, 29 Aug 2009 23:30:09 UTC +00:00

¿Cómo convierto uno de ellos a 'Hora del Este (EE.UU. y Canadá)' (teniendo en cuenta el horario de verano)? Algo como:

Annotation.last.created_at.in_eastern_time
Author: Tom Lehman, 2009-09-07

5 answers

Utilice el método in_time_zone de la clase DateTime

Loading development environment (Rails 2.3.2)
>> now = DateTime.now.utc
=> Sun, 06 Sep 2009 22:27:45 +0000
>> now.in_time_zone('Eastern Time (US & Canada)')
=> Sun, 06 Sep 2009 18:27:45 EDT -04:00
>> quit

Así que para su ejemplo particular

Annotation.last.created_at.in_time_zone('Eastern Time (US & Canada)')
 167
Author: Steve Weet,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-09-06 22:33:28

Aunque esta es una vieja pregunta, vale la pena mencionar algo. En una respuesta anterior se sugiere usar un before_filter para establecer la zona horaria temporalmente.

Debes nunca, nunca hacer eso porque el Tiempo.zone almacena la información en el subproceso, y probablemente se filtrará a la siguiente solicitud manejada por ese subproceso.

En su lugar, debe usar un around_filter para asegurarse de que la Hora.la zona se restablece después de completar la solicitud. Algo como:

around_filter :set_time_zone

private

def set_time_zone
  old_time_zone = Time.zone
  Time.zone = current_user.time_zone if logged_in?
  yield
ensure
  Time.zone = old_time_zone
end

Lea más sobre esto aquí

 15
Author: opsidao,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:09:44

Si añades esto a tu /config/application.rb

config.time_zone = 'Eastern Time (US & Canada)'

Entonces usted puede celular

Annotation.last.created_at.in_time_zone

Para obtener la hora en la zona horaria especificada.

 8
Author: Eifion,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-03-22 02:15:45

Establezca su zona horaria en Hora del Este.

Puede establecer su zona horaria predeterminada en config/environment.rb

config.time_zone = "Eastern Time (US & Canada)"

Ahora todos los registros que extraigas estarán en esa zona horaria. Si necesita diferentes zonas horarias, por ejemplo, en función de una zona horaria de usuario, puede cambiarla con un before_filter en su controlador.

class ApplicationController < ActionController::Base

  before_filter :set_timezone

  def set_timezone
    Time.zone = current_user.time_zone
  end
end

Solo asegúrese de que está almacenando todas sus horas en la base de datos como UTC y todo será dulce.

 2
Author: nitecoder,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-06-16 07:07:44

Si configura su /config/application.rb

config.time_zone = 'Eastern Time (US & Canada)'

Time.now.in_time_zone

DateTime.now.in_time_zone
 2
Author: Dorian,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-12-17 15:41:25