How do you convert next time from UTC to EST in Ruby (without Rails)?

I am trying to hide the UTC time "2018-04-02T14: 30: 00Z" to EST in pure Ruby. I noticed the following discrepancy. If I analyze time in UTC using Rails and then add EST zone_offset, I get a different time than using the in_time_zone helper. 9:30 a.m. versus 10:30 a.m.

2.2.4 :001 > t = Time.parse "2018-04-02T14:30:00Z"
 => 2018-04-02 14:30:00 UTC 
2.2.4 :002 > t + Time.zone_offset("EST")
 => 2018-04-02 09:30:00 UTC 
2.2.4 :003 > t.in_time_zone('Eastern Time (US & Canada)')
 => Mon, 02 Apr 2018 10:30:00 EDT -04:00 
+4
source share
3 answers

Rails does time zones very well, and you will have a hard time replicating the results ActiveSupport::TimeWithZone#in_time_zonewithout using it.

For example, as you indicated:

>> t = Time.parse "2018-04-02T14:30:00Z"
>> t.in_time_zone('Eastern Time (US & Canada)')
Mon, 02 Apr 2018 10:30:00 EDT -04:00

But keep in mind that you can also do:

>> t = Time.parse "2018-01-02T14:30:00Z"
>> t.in_time_zone('Eastern Time (US & Canada)')
Tue, 02 Jan 2018 09:30:00 EST -05:00

, ActiveSupport , / , . .js (, , , ).

require 'active_support', , Rails?

+1

localtime, - Time.parse("2018-04-02T14:30:00Z").localtime("-05:00").strftime("%m/%d/%Y %I:%M %p"), , , strftime

0

, " ( )" EDT, EST. .

irb(main):014:0> t = Time.parse "2018-04-02T14:30:00Z"
=> 2018-04-02 14:30:00 UTC
irb(main):015:0> t.in_time_zone('EST')
=> Mon, 02 Apr 2018 09:30:00 EST -05:00
irb(main):016:0> t.in_time_zone('Eastern Time (US & Canada)')
=> Mon, 02 Apr 2018 10:30:00 EDT -04:00
irb(main):017:0> 

: https://time.is/EST https://time.is/EDT

, - .

0

Source: https://habr.com/ru/post/1695540/


All Articles