听说Rails 2.1有个Time Zone

Rails 2.1快要release了,下面是对Time Zone一篇文章的翻译,觉得有点意思,便拿来翻译翻译,正好可以学习一番。翻译质量很差,希望不要嘲笑。

(链接在此: http://mad.ly/2008/04/09/rails-21-time-zone-support-an-overview/)
图片就不自己搞rails来原创的,直接取之来用,另外我已将图片打包,免得链接失效了就不好办了

具体的API可以查看 http://api.rubyonrails.org/

可惜可爱的ruby 1.9x 稳定版还是遥遥无期

看看rails 2.1的这个新功能吧,Time Zone

什么是Time Zone,就是时区,对,论坛里经常有设置的,UTC或者是GMT

UTC = Coordinated Universal Time. 中文名称为协调世界时
GMT = Greenwich Mean Time. 中文名称为格林尼治时

那UTC和GMT又有何区别?据说UTC=GMT +/- 0.9s

废话多了

在rails 2.1中,编辑
# config/environment.rb
config.time_zone = 'UTC'

这就把UTC做成了默认设置
同时,也可以运行 time:zones:all, time:zones:us,还有 time:zones:local来获取一些相关的时区信息

$ rake time:zones:local
 
* UTC -06:00 *
Central America
Central Time (US & Canada)
Guadalajara
Mexico City
Monterrey
Saskatchewan

没错,可以把Central Time设置成默认的
# config/environment.rb
config.time_zone = 'Central Time (US & Canada)'


下面用scaffold来试试看这个新功能
$ script/generate scaffold Task name:string alert_at:datetime
$ rake db:migrate
$ script/server


create后就可以看到时区了

图片是-5区,记得我们国家是+8区
用script/console验证一下
>> t = Task.find_by_name('foo')
=> #< Task … >
>> t.alert_at
=> Sun, 06 Apr 2008 10:30:00 CDT -05:00
>> t.alert_at_before_type_cast
=> "2008-04-06 15:30:00"

可以得知,2008-04-06 15:30:00时间是UTC时间,而Sun, 06 Apr 2008 10:30:00 CDT -05:00是CDT时间,为上面设置的时区

现在把日期改一下,改成1月份

时区变成-6的了,因为已经不是夏时制了(什么是夏时制?请看 http://baike.baidu.com/view/131456.htm)

再次验证一下
>> t = Task.find_by_name('foo')
=> #< Task … >
>> t.alert_at
=> Sun, 06 Jan 2008 10:30:00 CST -06:00
>> t.alert_at_before_type_cast
=> "2008-01-06 16:30:00"


用户自定义时区

创建一个scaffold
$ script/generate scaffold User name:string time_zone:string
$ rake db:migrate

用时间选择框代替文本框
# views/users/new.html.erb
<%= f.time_zone_select :time_zone, TimeZone.us_zones %>

看看效果

为了demo效果,添加一个login_from_querystring before_filter到controller
# controllers/application.rb
before_filter :login_from_querystring
 
def login_from_querystring
  @current_user = User.find_by_name(params[:user])
end

再添加一个set_time_zone before_filter,用来设置当前用户的时区信息
# controllers/application.rb
before_filter :set_time_zone
 
def set_time_zone
  Time.zone = @current_user.time_zone if @current_user
end

加个view,用来显示谁登录了,登录用户的时区,用户的当前时间
# views/layouts/tasks.html.erb

Current user: <%= @current_user.name if @current_user %>
Current time zone: <%= Time.zone.name %>
Current time: <%= Time.zone.now.inspect %>
<hr />

最后,需要显示一下时间(只是按照我的理解,原文可能并非此意)
<%=h task.alert_at.inspect %>

用户登录后,会看到之前已经设置好的时区

用户的页面显示的都是他们所在的时区的时间
如果用户没有登录,显示的就是config.time_zone中设置的时区


在当前时区用来修改设置的一些方法

Time.zone.local(), Time.zone.parse() and Time.zone.at() Time.zone.now
>> Time.zone = 'Hawaii'
=> "Hawaii"
>> Time.zone.now
=> Wed, 09 Apr 2008 15:48:18 HST -10:00
>> Time.zone.local(2008, 4, 9, 15, 48, 18)
=> Wed, 09 Apr 2008 15:48:18 HST -10:00
>> Time.zone.parse('2008-04-09 15:48:18')
=> Wed, 09 Apr 2008 15:48:18 HST -10:00
>> Time.zone.at(1207792098)
=> Wed, 09 Apr 2008 15:48:18 HST -10:00

in_time_zone函数可以把任何实例转化成存在Time.zone中的任何时区
>> Time.zone = 'Alaska'
=> "Alaska"
>> t = Time.utc(2000)
=> Sat Jan 01 00:00:00 UTC 2000
>> t.in_time_zone
=> Fri, 31 Dec 1999 15:00:00 AKST -09:00

也可以这样,用名称代表时区
>> t.in_time_zone('Hawaii')
=> Fri, 31 Dec 1999 14:00:00 HST -10:00
>> t.in_time_zone(-6.hours)
=> Fri, 31 Dec 1999 18:00:00 CST -06:00


升级
看原文吧,别鄙视我
   1. the new time zone features assume that the database is storing times in UTC, so if you've currently storing times in the database in a zone other than UTC, you'll need to migrate existing data to UTC
   2. if the tzinfo_timezone plugin is installed, you'll need to remove it, given that it overrides the TimeZone class in ActiveSupport
   3. the TZInfo gem is no longer required, given that it's now bundled in ActiveSupport. However, if you do have a recent version of this gem installed, Rails will favor the gem over the bundled version.
   4. The bundled TZInfo is a slimmed-down version of the gem, so if you're interacting with the TZInfo API directly, you should have the gem installed
   5. If you *don't* wish to use the new time zone features — the new features shouldn't interfere with your existing code, as long as you don't declare config.time_zone in environment.rb

你可能感兴趣的:(F#,Ruby,sun,Rails)