指定したオフセット値(JSTなら9時間)で ActiveSupport::TimeZone
を作り、それをもとに ActiveSupport::TimeWithZone
インスタンスを作りたい。
jst = ActiveSupport::TimeZone[9]
# => #<ActiveSupport::TimeZone:0x00007fbbe3b034b0 @name="Osaka", @utc_offset=nil, @tzinfo=#<TZInfo::TimezoneProxy: Asia/Tokyo>>
jst.now
# => Fri, 21 Jun 2019 21:24:26 JST +09:00
+9は、問題なし。
utc = ActiveSupport::TimeZone[0]
# => #<ActiveSupport::TimeZone:0x00007fbbe3afb030 @name="Casablanca", @utc_offset=nil, @tzinfo=#<TZInfo::TimezoneProxy: Africa/Casablanca>>
utc.now
# => Fri, 21 Jun 2019 13:25:48 WEST +01:00
utc.tzinfo.current_period.utc_offset
# => 0
ファッ!? +00:00
が期待されるところ +01:00
となっている!?
TZInfoのutc_offsetは0を指しているようだが...
そもそもoffsetだけではtimezoneと言えないのでやろうとしていることが強引な気もするが、それはさておき、どうやるのが正解なのだろうか。
関係ありそうだが、詳しくは読んでない。
https://github.com/tzinfo/tzinfo/issues/98
動作確認環境
ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-darwin16]
activesupport (5.2.1)
tzinfo (1.2.5)