2

I have records in database which has created_at field in UTC. In need to show it in active_admin table as a time in user timezone. For example:
in DB created at 2017-12-25 15:52:58
on page: 2017-12-25 15 17:52:58

I tried to use this code in activeadmin

paginated_collection(user_actions.page(params[:page]).per(15), download_links: false) do
            table_for(collection, sortable: false) do |action|
              column ("Timestamp"){|action| action.created_at.in_time_zone('EET').strftime("%Y-%m-%d %H:%M:%S")}
            end
          end

and it works fine. But I need to get user timezone constant programmatically to pass it into

in_time_zone(timezone_variable)

or

in_time_zone(get_current_timezone_method)

I tried to get it like this

Time.zone.now.name

but it doesn't work and I think it because this code try to get server timezone but not a client. Is there any way to get client timezone on client side and display created_at in active_admin which include timezone correction? Also (if previous way is impossible) I thought about another way - to store users timezone in DB when user signed in. And get it from DB to pass into

in_time_zone(action.user.timezone)

. Is there any way to get users timezone constant in controller action on server side to store it in DB?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
ruby_dev
  • 136
  • 12

1 Answers1

1

This is a common Rails problem with no good answer. You can try setting a header with JavaScript but the simplest way is to have the user manually set their preferred timezone in their user profile.

Piers C
  • 2,880
  • 1
  • 23
  • 29
  • Yes, JS is the only way. A better approach though is documented [here](https://stackoverflow.com/questions/22618056/javascript-timezone-information-how-to-get-america-los-angeles-or-equivalent). – Matt Johnson-Pint Dec 27 '17 at 19:59