當我與調度事件的應用程序,我不得不時區添加到用戶模型,以抵消它們的調度。
當我錄的偏移,我用下面的代碼:
<%= time_zone_select "user",
"timezone",
ActiveSupport::TimeZone.us_zones,
{default: "Mountain Time (US & Canada)"},
{class: "form-control"} %>
因爲Rails有time zones built into their ActiveSupport model.你可以使用ActiveSupport::TimeZone.all
,如果你想用戶是全球性的。
這裏有time_zone_select
一些信息然後取決於你如何使用它,你可以只設置
Time.zone = current_user.timezone unless current_user.nil?
或在您的application.rb
文件類似的東西。
UPDATE
我個人用它來設置控制器的時區上的只有2個動作,其中有必要。
def index
@user = current_user
@pending = @user.share_events.where("time > ?", DateTime.now.in_time_zone(@user.timezone))
@complete = @user.share_events.where("time <= ?", DateTime.now.in_time_zone(@user.timezone))
end
def new
@user = current_user
Time.zone = @user.timezone
@post = Post.find(params[:post_id])
@share_event = ShareEvent.new
end
您可以使用Time.current ...檢查了這一點 - http://apidock.com/rails/Time/current/class – Milind