Weird Behaviors of datetime(timezone) in Rails application

50 views
Skip to first unread message

Shanmugavel A

unread,
Aug 16, 2015, 5:45:17 PM8/16/15
to Ruby on Rails: Talk


Hi,

In our rails 3.2 application, we have configured,

    config.time_zone = 'London'
    config.active_record.default_timezone = :local

And in postgresql also configured timezone as "Europe/London".

For the past one week, our application datetime field is not working properly with timezone.

For an example, If we create an reminder start_date at 2015-08-18 10AM. it creates 2015-08-18 10:00:00 in postgresql database.

when displaying in template,

reminder start date: 2015-08-18 10AM (2015-08-18 10:00:00 +0100 )

BUT,(not always) now frequently its showing UTC time.

reminder start date:  2015-08-18 9AM (2015-08-18 09:00:00 UTC )

it could not be reproduced in development.

anyone faced  this kind of  problem?





Frederick Cheung

unread,
Aug 16, 2015, 5:47:43 PM8/16/15
to Ruby on Rails: Talk
On Sunday, August 16, 2015 at 10:45:17 PM UTC+1, Shanmugavel A wrote:
> Hi,
>
> In our rails 3.2 application, we have configured,
>
>     config.time_zone = 'London'
>     config.active_record.default_timezone = :local
>

I wouldn't recommend setting default_timezone to local - it means that in addition to these settings, the timezone of the application server will also influence results.

Fred

Colin Law

unread,
Aug 17, 2015, 4:02:11 AM8/17/15
to Ruby on Rails: Talk
On 16 August 2015 at 22:47, Frederick Cheung <frederic...@gmail.com> wrote:
> On Sunday, August 16, 2015 at 10:45:17 PM UTC+1, Shanmugavel A wrote:
>> Hi,
>>
>> In our rails 3.2 application, we have configured,
>>
>> config.time_zone = 'London'
>> config.active_record.default_timezone = :local
>>
>
> I wouldn't recommend setting default_timezone to local - it means that in addition to these settings, the timezone of the application server will also influence results.

Just to reinforce Fred's comment, in case you think :local means the
local time of the user in the browser that is not correct. It means
the local time of the server which may not even be under your control.

Colin

Shanmugavel A

unread,
Aug 17, 2015, 1:08:04 PM8/17/15
to Ruby on Rails: Talk



I fixed this issue, by adding around filter in ApplicationController.

around_filter :use_time_zone

private

def use_time_zone(&block)
  Time.use_zone('London', &block)
end

So, whenever the default timezone changed to UTC, it will override and set as BST.

Reply all
Reply to author
Forward
0 new messages