Change the time zone in a pure ruby ​​(not rails)

I am creating a Sinatra site that has mixed UTC / PST data sources, but will be displayed in PST. Therefore, I need to easily convert Time objects from UTC to PST. Without Rails, I do not have access to Time.zone , in_time_zone , etc.

I only need to change the time zone of the Time object, for example. 2014-08-14 21:44:17 +0000 => 2014-08-14 14:44:17 -0700.

First I tried this:

 class Time def to_pst self.utc + Time.zone_offset('PDT') end end 

But this changes the actual timestamps, not the zone. I need both time.to_i and time.strftime to work; therefore, I cannot change the absolute value of timestamps.

 > t = Time.now => 2014-08-14 21:46:20 +0000 > t.to_pst => 2014-08-14 14:46:20 UTC > t.to_i => 1408052780 > t.to_pst.to_i => 1408027580 

gem 'timezone' is a similar problem.

This solution works, but modifies global variables and is not thread safe.

OS time zone must remain in UTC.

I just need a way to change the time zone on one Time object. This is a simple problem and it looks like it should have a simple solution! Has anyone found it? Thanks in advance!

+5
source share
5 answers

You can use the Time extensions from Active Support outside of Rails:

 require 'active_support/core_ext/time' t = Time.now #=> 2014-08-15 15:38:56 +0200 t.in_time_zone('Pacific Time (US & Canada)') #=> Fri, 15 Aug 2014 06:38:56 PDT -07:00 

Now you can do

 class Time def to_pst in_time_zone('Pacific Time (US & Canada)') end end t = Time.now #=> 2014-08-15 15:42:39 +0200 t.to_i #=> 1408110159 t.to_pst #=> Fri, 15 Aug 2014 06:42:39 PDT -07:00 t.to_pst.to_i #=> 1408110159 # timestamp does not change! 

In addition, you may also need to extend the time on Numeric and Date :

 require 'active_support/core_ext/date' require 'active_support/core_ext/numeric/time' 2.days.from_now #=> 2014-08-17 15:42:39 +0200 
+6
source

In Pure Ruby

 Time.now.utc.localtime("+05:30") 

where +05: 30 (IST) is the offset of a specific zone

+7
source

If you do not want to load a ton of libraries, and if you do not mind doing this, the poor person and you know that your offset is -8, for example, you can subtract 8 * 3600 seconds to get a new time. For instance,

 offset=-8 t = Time.now + offset * 3600 

You will need to adjust the time of day on your own, but this will not change the time zone, so% z will not be right.

+3
source

I recently did the same on an iron.io worker, the default time is UTC

 # For generality/testing, only require what you need. require 'active_support/all' # Set the Timezone Time.zone = "Asia/Hong_Kong" t = Time.zone.now #=> 2015-01-20 16:44:58 +0800 

For information on supported time zones, see ActiveSupport http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html

0
source

Get the current time in utc, then add the offset:

 Time.now.utc + Time.zone_offset('PDT') #=> 2015-12-01 18:10:07 UTC 

The problem is that the PDT tied to -8 , the PST tied to -7 and the Pacific Zone (the one that is configured between them depending on the time of year and daylight saving time) does not exist, for this you most likely will need ActiveSupport.

0
source

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


All Articles