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!
source share