Rails之关于时间(时区)
来源:互联网 发布:网络黄金egd今日价格 编辑:程序博客网 时间:2024/05/18 12:33
It’s About Time (Zones)
这是时间系列的第一篇文章。第二篇的主题是 a case study in multiple time zones。
Ruby提供了两个类来管理时间:Time
和DateTime
。Ruby1.9.3之后两者之间的区别越来越小。鉴于Time
包含闰秒和夏令时的概念。本文中,我们将使用Time
来举例。
TZInfo是一个时区库,提供不同时区的夏令时转换。它被封装成了gem并且包含582个不同时区的数据。
Rails中的时区
Rails中的ActiveSupport::TimeZone封装了TZInfo,并提供其中146个时区的数据以及更好的时区格式(例如:用Eastern Time (US & Canada)
替换America/New_York
)。另外配合 ActiveSupport::TimeWithZone,Rails提供和Ruby中Time
类一样的API,所以Time
和ActiveSupport::TimeWithZone
是可以互换的,你永远都不需要直接创建一个TimeWithZone
实例(new)
执行以下命令查看Rails中所有可用的时区:
$ rake time:zones:all* UTC -11:00 *American SamoaInternational Date Line WestMidway IslandSamoa* UTC -10:00 *Hawaii* UTC -09:00 *Alaska...
在console中查看当前时区:
> Time.zone=> #<ActiveSupport::TimeZone:0x007fbf46947b38 @current_period=#<TZInfo::TimezonePeriod: nil,nil,#<TZInfo::TimezoneOffset: 0,0,UTC>>>, @name="UTC", @tzinfo=#<TZInfo::TimezoneProxy: Etc/UTC>, @utc_offset=nil>
或临时设置时区:
# in console> Time.zone = "Perth"
或在配置文件config/application.rb
中永久修改项目时区:
# config/application.rbconfig.time_zone = "Perth"
Rails的默认时区是UTC。最好就保留项目时区为UTC,让用户自己去设置他们各种的时区,下一篇文章a case study in multiple time zones 详述了这么做的原因。
用户的时区
假设每个用户都拥有自己的时区。这个可以通过向User表添加time_zone属性实现:
create_table :users do |t| t.string :time_zone, default: "UTC" ...end
将时区保存为字符串形式是因为Rails中大多数时区相关方法都接受字符串。因此,应该避免将时区保存为:enums
格式。
用户可以设置自己想要的时区。SimpleForm支持:time_zone
,并且提供了一个help方法让用户可以在下拉框中选择时区。
<%= f.input :time_zone %>
可以在ApplicationController
中使用around_action
来应用用户设置的时区。
# app/controllers/application_controller.rbaround_action :set_time_zone, if: :current_userprivatedef set_time_zone(&block) Time.use_zone(current_user.time_zone, &block)end
我们将用户的时区信息传递给Time.use_zone
(ActiveSupport提供的方法)。该方法需要传递一个块,在块的范围内设置时区。所以它只影响一次请求,请求结束之后使用的还是原始时区(应用默认时区)。
在Rails 3.2.13及更低版本中,需要使用around_filter
替代around_action
。
最后,在展示时区时,可以使用in_time_zone
方法:
<%= time.in_time_zone(current_user.time_zone) %>
相关API
操作API时最好使用ISO8601标准,该标准用字符串表示日期时间。ISO8601的优点是清晰,可读,广泛支持并且是按规则排序的。示例:
> timestamp = Time.now.utc.iso8601=> "2015-07-04T21:53:23Z"
结尾处的Z代表UTC时间,可以使用以下方法将字符串转为时间对象:
> Time.iso8601(timestamp)=> 2015-07-04 21:53:23 UTC
三个时区
在一个Rails应用中,存在三个不同的时区:
- 系统时间
- 应用时间
- 数据库时间
假设我们将时区设为Fiji,来看下结果:
# This is the time on my machine, also commonly described as "system time"> Time.now=> 2015-07-04 17:53:23 -0400# Let's set the time zone to be Fiji> Time.zone = "Fiji"=> "Fiji"# But we still get my system time> Time.now=> 2015-07-04 17:53:37 -0400# However, if we use `zone` first, we finally get the current time in Fiji> Time.zone.now=> Sun, 05 Jul 2015 09:53:42 FJT +12:00# We can also use `current` to get the same> Time.current=> Sun, 05 Jul 2015 09:54:17 FJT +12:00# Or even translate the system time to application time with `in_time_zone`> Time.now.in_time_zone=> Sun, 05 Jul 2015 09:56:57 FJT +12:00# Let's do the same with Date (we are still in Fiji time, remember?)# This again is the date on my machine, system date> Date.today=> Sat, 04 Jul 2015# But going through `zone` again, and we are back to application time> Time.zone.today=> Sun, 05 Jul 2015# And gives us the correct tomorrow according to our application's time zone> Time.zone.tomorrow=> Mon, 06 Jul 2015# Going through Rails' helpers, we get the correct tomorrow as well> 1.day.from_now=> Mon, 06 Jul 2015 10:00:56 FJT +12:00
时区相关查询
Rails使用UTC时区将时间戳存入数据库。在数据库查询中应该始终使用Time.current
,Rails会帮我们转换成正确的时间进行对比。
Post.where("published_at > ?", Time.current)# SELECT "posts".* FROM "posts" WHERE (published_at > '2015-07-04 17:45:01.452465')
总结
不要使用
* Time.now* Date.today* Date.today.to_time* Time.parse("2015-07-04 17:05:37")* Time.strptime(string, "%Y-%m-%dT%H:%M:%S%z")
推荐使用
* Time.current* 2.hours.ago* Time.zone.today* Date.current* 1.day.from_now* Time.zone.parse("2015-07-04 17:05:37")* Time.strptime(string, "%Y-%m-%dT%H:%M:%S%z").in_time_zone
- 始终使用UTC
- 用
Time.current
或者Time.zone.today
- Rails之关于时间(时区)
- 关于linux时间、时区
- magento 关于时间/时区
- 完美解决rails中国时区时间设置
- 完美解决rails中国时区时间设置
- 关于时间问题(时间的显示 和时区)
- Rails ActiveRecord 时区问题(Rails 2.3.8)
- PHP中关于时间(戳)、时区、本地时间、UTC时间等的梳理
- 关于php时区时间错误问题
- 关于Magento的时间/时区问题
- 关于php时区时间错误问题
- 关于时间不同时区的转换问题
- 时区与时间(二)
- rails中的时区问题
- Rails 时区Time Zone
- Java时间处理相对时间 之 时区相关
- 关于Magento的时间/时区问题--------转自精东
- 关于php的时间相关函数需要注意时区问题
- StructuredStreaming官方文档翻译
- TCP/IP协议、socket及socket简单实现网络通讯
- php mysql数据库操作类
- socket之UDP通信
- HBase的RowKey设计原则
- Rails之关于时间(时区)
- Node.js学习之Telnet
- ZooKeeper伸缩性
- MyBatis在insert插入操作时返回主键ID的配置
- 线程和进程的区别
- Tomcat开机自启动
- angularjs
- 大连血液中心见习报告
- 关于ArcGIS API for JavaScript中basemap的总结介绍(一)