About Time_Zone on ROR

UTC: 協調世界時,以原子時秒長爲基礎....(度娘)

GMT: 世界時,以地球自轉爲基礎...(度娘)

mysql 中 timestamp 類型,內部存儲4個字節,存儲的時候轉換爲UTC時間存儲,檢索時再轉換回當前的時區( time zone ).


ROR 中獲取及設置time zone

~> Time.zone          # => (GMT+00:00) UTC

~> Time.zone = 'Beijing'  # => "Beijing"
~> Time.zone          # => (GMT+08:00) Beijing

~> Time.zone = 'Pacific Time (US & Canada)'
~> Time.zone          # => (GMT-08:00) Pacific Time (US & Canada)

~> Time.zone = 'International Date Line West'
~> Time.zone          # => (GMT-11:00) International Date Line West

~> Time.zone = 'Hawaii'
~> Time.zone          # => (GMT-10:00) Hawaii
~> Time.zone.now  # => Wed, 14 Jan 2015 20:41:09 HST -10:00
~> Time.zone.now.to_s   #=> "2015-01-14 20:41:44 -1000"

~> Time.now          # => 2015-01-15 14:43:13 +0800
~> t = Time.now.utc  # => 2015-01-15 06:38:32 UTC
# attention, irrelevant with Time.zone
~> t.localtime          # => 2015-01-15 14:38:32 +0800

Convert utc to date with time zone:

~> t = Time.now     # => 2015-01-15 14:51:52 +0800
~> t.in_time_zone("Hawaii")  # => Wed, 14 Jan 2015 20:51:52 HST -10:00
~> t.in_time_zone("Hawaii").to_s  # => "2015-01-14 20:51:52 -1000"

You can find the names of the ActiveSupport time zones:

~> ActiveSupport::TimeZone.all.map(&:name)
~> ActiveSupport::TimeZone.us_zones.map(&:name)


~> ts = "2015-01-14 20:51:52 -1000"
~> Time.parse(ts) 
~> Time.zone.parse(ts)



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章