请注意,精美的 1.8.7 手册没有提到%z
:
...
%w - Day of the week (Sunday is 0, 0..6)
%x - Preferred representation for the date alone, no time
%X - Preferred representation for the time alone, no date
%y - Year without a century (00..99)
%Y - Year with century
%Z - Time zone name
%% - Literal ``%'' character
但1.9.3 版本确实有文档支持%z
:
Time zone:
%z - Time zone as hour and minute offset from UTC (e.g. +0900)
%:z - hour and minute offset from UTC with a colon (e.g. +09:00)
%::z - hour, minute and second offset from UTC (e.g. +09:00:00)
%Z - Time zone abbreviation name
产生任何东西的事实%z
似乎是一个未记录的并且可能是意外的实现细节。
您可以%Z
在 1.8.7 和 1.9.3 中使用;例如,您在 1.8.7 中得到这些结果:
>> t = Time.now
=> Mon Dec 19 16:46:06 -0800 2011
>> t.zone
=> "PST"
>> t.strftime('%z %Z')
=> "-0800 PST"
>> t = Time.now.utc
=> Tue Dec 20 00:46:27 UTC 2011
>> t.zone
=> "UTC"
>> t.strftime('%z %Z')
=> "-0800 UTC"
这将为您提供 UTC、PST、EDT 和类似常见缩写的时区。如果你想要偏移量,你应该gmt_offset
在1.9.3和1.8.7中使用:
>> Time.now.gmt_offset
=> -28800
>> Time.now.utc.gmt_offset
=> 0
请注意,这gmt_offset
会给您以秒为单位的偏移量。