如果我們的系統(tǒng)需要支持全球用戶,怎樣保證不同時(shí)區(qū)的用戶顯示正確的本地時(shí)間呢
我們可以讓用戶選擇自己的時(shí)區(qū),記錄在數(shù)據(jù)庫(kù),然后轉(zhuǎn)換時(shí)間后顯示
如users表有一個(gè)string的timezone字段記錄時(shí)區(qū)
而恰好Rails里有一個(gè)TimeZone類,TimeZone.all可以得到所有時(shí)區(qū):
- class User < ActiveRecord::Base
- composed_of :tz,
- :class_name => 'TimeZone',
- :mapping => %w(time_zone name)
- class ApplicationController < ActionController::Base
- def user2utc(time)
- current_user.tz.unadjust(time)
- end
- def utc2user(time)
- current_user.tz.adjust(time)
- end
- end
安徽新華電腦學(xué)校專業(yè)職業(yè)規(guī)劃師為你提供更多幫助【在線咨詢】