0

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.

Bloomberg
  • 2,317
  • 2
  • 25
  • 47

1 Answers1

2
%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]
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
  • NB: in the previous implementation there was a bug: it failed on '-0:30' timezone. AFAIK, such a timezone does not exist, but anyway I corrected a solution. – Aleksei Matiushkin Jul 06 '16 at 18:32