Rails: convertir la fecha y hora UTC a otra zona horaria
En Ruby/Rails, ¿cómo convertir una fecha y hora UTC a otra zona horaria?
6 answers
time.in_time_zone(time_zone)
Ejemplo:
zone = ActiveSupport::TimeZone.new("Central Time (US & Canada)")
Time.now.in_time_zone(zone)
O simplemente
Time.now.in_time_zone("Central Time (US & Canada)")
Puede encontrar los nombres de las zonas horarias de ActiveSupport haciendo:
ActiveSupport::TimeZone.all.map(&:name)
# or for just US
ActiveSupport::TimeZone.us_zones.map(&:name)
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
2010-04-23 14:42:06
Si Time.zone
es su zona horaria deseada, entonces puede usar @date.to_time.to_datetime
> @date
=> Tue, 02 Sep 2014 23:59:59 +0000
> @date.class
=> DateTime
> @date.to_time
=> 2014-09-02 12:59:59 -1100
> @date.to_time.to_datetime
=> Tue, 02 Sep 2014 12:59:59 -1100
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
2014-09-02 11:06:30
Pruebe los objetos TimeWithZone de ActiveSupport manipulados con Zona horaria. ActiveSupport también proporciona el método in_time_zone para convertir una hora UTC a una zona horaria especificada. la respuesta de Mckeed muestra el código.
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
2010-04-23 04:38:22
Por si acaso, si está tratando con el objeto ActiveRecord en Rails.
Podría ser una buena idea usar Time.use_zone
para una zona horaria por solicitud que anule la zona horaria predeterminada establecida en config.time_zone
Más detalles que explico en https://stackoverflow.com/a/25055692/542995
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 11:33:16
En ruby simple, con solo require 'date'
, use el método new_offset
:
require 'date'
d=DateTime.parse('2000-01-01 12:00 +0200')
l=d.new_offset('-0700')
u=l.new_offset('UTC')
puts "#{u.strftime('%a %F %T %Z')} ❖ #{l.strftime('%a %F %T %Z')}"
Probado con ruby 2.3.7 que viene de serie en Mac OS X 10.13.
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
2018-09-21 21:42:31
Estoy usando simple_form en Rails 4 y acabo de añadir el campo de entrada como
<%= f.input :time_zone, :as => :time_zone %>
Con la migración
class AddTimeZoneColumnToTextmessage < ActiveRecord::Migration
def change
add_column :textmessages, :time_zone, :string
end
end
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
2014-04-19 16:26:45