Ignoring time zones altogether in Rails and PostgreSQL

后端 未结 2 2087
梦谈多话
梦谈多话 2020-11-21 04:53

I\'m dealing with dates and times in Rails and Postgres and running into this issue:

The database is in UTC.

The user sets a time-zone of choice in the Rails

2条回答
  •  独厮守ぢ
    2020-11-21 05:23

    If you want to deal in UTC by default:

    In config/application.rb, add:

    config.time_zone = 'UTC'
    

    Then, if you store the current user timezone name is current_user.timezone you can say.

    post.created_at.in_time_zone(current_user.timezone)
    

    current_user.timezone should be a valid timezone name, otherwise you will get ArgumentError: Invalid Timezone, see full list.

提交回复
热议问题