Hi everyone,

Is anyone familiar with ActiveSupport::TimeZone objects? I’m working with an API that is returning timestamps for a user in Los Angeles, CA in the PST timezone rather than PDT (we’re currently in DST). My goal here is to convert the timezone information from the API into an ActiveSupport::TimeZone object, but after reviewing the source code I’m noticing it can’t be initialized with a UTC offset.

I’d really like to pass this object from the gem in question off to a higher-level Rails application that I’m working on if possible for display to the user.

Here is a link to the source code I’m working on.

Does ENV['TZ'] = 'GMT' or ENV['TZ'] = 'UTC' help? Also make sure to run Time.now.zone to confirm the zone just once (in byebug/pry or something like that) to make sure nothing is overwriting it.

This can be done in the application.rb for example. Or to set the time dynamically inside the controller, place this code inside the controller.

In the Ruby on Rails application, ENV['TZ'] is set to a specific timezone. This was a legacy application upgraded from Rails 3.22 to Rails 6.1, and the database is still writing all timestamps in that timezone.

However my goal with linked gem is to convert the timezone returned by the API (in this example, America/Los_Angeles without DST.

Right now, ActiveSupport::TimeZone.new('America/Los_Angeles') returns a timezone with a UTC offset of -0700 because Los Angeles is currently in DST; but the goal is to create a new ActiveSupport::TimeZone with an offset of -0800 per the API.

I’ve done some more testing:

ActiveSupport::TimeZone.new('America/Los_Angeles').utc_offset / 3600
=> -8 # not DST

irb(main):023:0> Time.now.in_time_zone('America/Los_Angeles')
=> Sun, 05 Sep 2021 16:19:01.318034000 PDT -07:00 # DST

The goal is to have the second line return this instead:

irb(main):023:0> Time.now.in_time_zone('America/Los_Angeles')
Sun, 05 Sep 2021 15:19:01.318034000 PDT -08:00 # standard time

Since the Rails application is using a Asia/Manila in the database, Rails needs to use the correct API timezone to find the relevant records, so I can do something like:

# date here is a `Date` with no timezone information attached

from = date.to_datetime.in_time_zone(worksnaps_timezone.to_activesupport).beginning_of_day
to = date.to_datetime.in_time_zone(worksnaps_timezone.to_activesupport).end_of_day
OvertimeApprovalRequest.where('created_at >= ?', from).where('created_at <= ?', to)

Rails is correctly converting the from, to above into Asia/Manila, but it’s using the DST offset rather than the standard offset supplied by the API.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs