How to convert the timezone format automatically according to the country

109 views Asked by At
@time = Time.now
 if @time.zone == "IST"
  @time = Time.now.utc  
 end

i tried the above code, and it works for me. but how to change the time according to the country when the application is opened in some other country.

i.e., If the application is opened in PDT it should show time in PDT format when the same application is opened in UTC it should show time in UTC format by checking the zone automatically. How to apply this for all the timezone in ruby?

i have tried checking for one timezone, how to do it for all the timezone!!

Is there any gem?

Thanks..

1

There are 1 answers

2
M Zippka On

This will do the same thing as config.time_zone but on a per request basis. I still recommend to change the default config.time_zone to a time zone that is a good default for your users.

around_action :user_time_zone, if: :current_user

def user_time_zone(&block)
  Time.use_zone(current_user.time_zone, &block)
end