LoginSignup
2
1

More than 5 years have passed since last update.

ActiveSupport::TimeWithZone.xmlschemaの戻り値がTimeやDateTimeと異なる

Posted at
$ ruby --version
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin16]
$ gem list activesupport

*** LOCAL GEMS ***

activesupport (5.1.1)
$ irb -r 'rubygems'

という環境において、

irb(main):001:0> require 'active_support'
=> true
irb(main):002:0> require 'active_support/time_with_zone'
=> true
irb(main):003:0> require 'active_support/core_ext/time/zones'
=> true
irb(main):004:0> Time.zone = "UTC"
=> "UTC"

UTCタイムゾーンで TimeWithZone, Time, DateTime それぞれの xmlschema を見ると、

irb(main):014:0> t = Time.zone.now
=> Thu, 01 Jun 2017 11:54:06 UTC +00:00
irb(main):015:0> t.xmlschema
=> "2017-06-01T11:54:06Z"
irb(main):016:0> t.to_time.xmlschema
=> "2017-06-01T11:54:06+00:00"
irb(main):017:0> t.to_datetime.xmlschema
=> "2017-06-01T11:54:06+00:00"

TimeWithZoneの場合だけタイムゾーンが Z となるんだけど、こういうもの?

文字列比較として使いたいため、xmlschema信用できないのは困るので、
strftimeを使うほうがよいのかな..

irb(main):018:0> t.strftime("%Y-%m-%dT%H:%M:%S%:z")
=> "2017-06-01T11:54:06+00:00"
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1