0

I included user time zones:

From here, adding this in application_controller

around_action :set_time_zone

private
  def set_time_zone
    if current_user
      Time.use_zone(current_user.time_zone) { yield }
    else
      yield
    end
  end

which produces

2020-09-21 00:00:00 +1000

This is great, but it could be more user friendly to display something along the lines of

2020-09-21 00:00 Melbourne time

Note: the word 'time' in the above is a string, nothing dynamic The word 'Melbourne' is from the options in the view created with time_zone_options_for_select(f.object.time_zone, nil, ActiveSupport::TimeZone) (from here)

Question

How do I adjust the set_time_zone method so that it displays 2020-09-21 00:00 Melbourne or 2020-09-21 00:00 Melbourne time rather than 2020-09-21 00:00:00 +1000?

I have tried (based on this):

  def set_time_zone
    if current_user
      Time.use_zone(current_user.time_zone).strftime('%Z') { yield }
    else
      yield
    end
  end

but it returns the error: no block given (yield)

stevec
  • 15,490
  • 6
  • 67
  • 110
  • Why would you want to do that? To make debugging easier? – Christian Bruckmayer Sep 19 '20 at 07:07
  • @ChristianBruckmayer purely for ux (I assume a lot of users would know a major city in their time zone but not everyone would know how many hours +/- GMT they are) – stevec Sep 19 '20 at 08:07
  • Presenting the time in the users TZ server side is a somewhat flawed idea as it will get in the way of caching. Instead use HTML5 time elements (with the time in UTC) and javascript to convert to the users local time on the client. – max Sep 19 '20 at 14:38

0 Answers0