The user inputs a date range let’s say from yesterday with any timezone.
from_datetime = "10/01/2012 00:00 +0545"
I get purchased time for the book like below:
purchased_at = Book.where("created_at > #{from_date}").purchased_at
=> Fri, 08 Jun 2012 09:44:26 UTC +00:00
The problem is this gives me UTC time but I want to show the purchased_at time in the requested time_zone which can vary.
I can’t use in_time_zone as the input from_date only has time offset, can I ?
purchased_at.in_time_zone("Kathmandu")
=> Fri, 08 Jun 2012 15:29:26 NPT +05:45
Is there any way around?
Give an offset, you can get a timezone name from
ActiveSupport::TimeZone:Then you can hand that to
in_time_zone:You can pull the offset out of the
from_datetimewith a bit of simple wrangling if you know the incoming format.There are issues with this approach:
ActiveSupport::TimeZone[]gives you the wrong name.Depending on your needs, you could just apply the offset manually and ignore the timezone when formatting the timestamp: