0

I'm setting the timezone for the users in my applicationcontroller

around_filter :user_time_zone, :if => :current_user

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

And the server stores the times as UTC, and created_at fields get updated accordingly to the user timezone. However, when I'm using a postgres timerange, the time in the range displays as UTC:

#<TimeEntry:0x007fb7c8701a20
  id: "5556cb4f-862f-4f7d-bb30-743578ba5741",
  time: 2016-05-03 21:02:28 UTC..2016-05-04 02:02:33 UTC,
  created_at: Tue, 03 May 2016 16:32:33 VET -04:30,
  updated_at: Tue, 03 May 2016 16:32:52 VET -04:30>

I'm building the timerange like this:

def self.build_timerange(start_time_ms, end_time_ms)
  start_time = Time.zone.at(start_time_ms.to_f / 1000)
  end_time = Time.zone.at(end_time_ms.to_f / 1000)

  start_time..end_time
end

I need to get them from the browser as ms.

I'd like the time range to update to the current request timezone (the user timezone), is it possible?

Carlos Martinez
  • 4,350
  • 5
  • 32
  • 62
  • I've been using local Time to deal with stuff like this. https://github.com/basecamp/local_time Everything is stored in UTC in the db and then I convert everything the user inputs to UTC using JavaScript when they select a range. I can post my code if you think it would be helpful. Tis not the exact Solution to your problem though – Doon May 03 '16 at 21:23
  • Interesting, although I'm using a server side approach because users are able to change their timezone – Carlos Martinez May 04 '16 at 00:17

1 Answers1

1

config/application.rb

config.time_zone = 'Moscow' # set default time zone to "Moscow" (UTC +4)

You can use your's timezone here... application will work on Defined time zone

Nischay Namdev
  • 553
  • 7
  • 23
  • I might have forgot to mention I already did this, my app stores the data with a default timezone of `UTC`. My problem is converting these dates **in the time range** to the user timezone, which is stored in the db. – Carlos Martinez May 04 '16 at 15:21