Lannister Lannister - 4 months ago 12
Ruby Question

how can i convert utc time to local time timezone with +05:30

I am trying to convert UTC time to user local time. For local time zone if have values like:

user_id time_zone
23 +05:30
25 +10:00


is this sufficient info? How can i convert using this. In documentation all i could find is this method.

Time.now.in_time_zone('Eastern Time (US & Canada)')


but i dont have that. Or should I fetch this
Eastern Time (US & Canada)
using +10:00

I am trying to trigger push notifications at 10 am of user's time.

Answer
%w|+05:30 +10:00|.map do |time_zone|
  hours, mins = time_zone.split(':').map(&:to_i).map(&:abs)
  sign = time_zone[0] == '-' ? -1 : 1
  Time.now.in_time_zone(ActiveSupport::TimeZone[(hours + 1.0 * mins / 60]) * sign)
end
#⇒ [Wed, 06 Jul 2016 12:54:10 IST +05:30, Wed, 06 Jul 2016 17:24:10 AEST +10:00]
Comments