ActiveSupport::TimeWithZone#strftime responds incorrectly to %:z and %::z format strings #6962

Closed
pastafari opened this Issue Jul 4, 2012 · 2 comments

Comments

Projects
None yet
3 participants
@pastafari

Environment : Ruby(MRI) 1.9.3-p194 and Rails 3.2.6

Issue

ActiveRecord::TimeWithZone is supposed to be a drop in replacement for Ruby Time.
Howerver TimeWithZone#strftime does not respond correctly to the %:z and %::z format strings as per the Time#strftime documentation. It loses the time offset information as shown below.

Test Case

irb> Time.zone = "Eastern Time (US & Canada)"
=> "Eastern Time (US & Canada)"
irb> twz = Time.zone.now
=> Wed, 04 Jul 2012 11:55:11 EDT -04:00
irb> twz.strftime('%z')
=> "-0400"
irb> twz.strftime('%:z')
=> "+00:00"
irb> twz.strftime('%::z')
=> "+00:00:00"

Pairing with @dhakadamit

@rroblak

This comment has been minimized.

Show comment Hide comment
@rroblak

rroblak Aug 9, 2012

Contributor

I came across this issue yesterday. Thanks for reporting it @pastafari.

The workaround I used for my use case was to convert it to a Time first:

1.9.3p125 :043 > twz = ActiveSupport::TimeWithZone.new(Time.now.utc, Time.zone)
 => Thu, 09 Aug 2012 13:22:37 PDT -07:00 
1.9.3p125 :044 > twz.strftime('%Y-%m-%dT%H:%M:%S%:z')
 => "2012-08-09T13:22:37+00:00" 
1.9.3p125 :045 > twz.to_time.strftime('%Y-%m-%dT%H:%M:%S%:z')
 => "2012-08-09T20:22:37+00:00"
Contributor

rroblak commented Aug 9, 2012

I came across this issue yesterday. Thanks for reporting it @pastafari.

The workaround I used for my use case was to convert it to a Time first:

1.9.3p125 :043 > twz = ActiveSupport::TimeWithZone.new(Time.now.utc, Time.zone)
 => Thu, 09 Aug 2012 13:22:37 PDT -07:00 
1.9.3p125 :044 > twz.strftime('%Y-%m-%dT%H:%M:%S%:z')
 => "2012-08-09T13:22:37+00:00" 
1.9.3p125 :045 > twz.to_time.strftime('%Y-%m-%dT%H:%M:%S%:z')
 => "2012-08-09T20:22:37+00:00"
@kennyj

This comment has been minimized.

Show comment Hide comment
@kennyj

kennyj Sep 19, 2012

Contributor

Hi guys ! Thanks for reporting.
I think I fix this issue. I already send PR #7703 :)

Contributor

kennyj commented Sep 19, 2012

Hi guys ! Thanks for reporting.
I think I fix this issue. I already send PR #7703 :)

@kennyj kennyj closed this in 4e715e3 Sep 20, 2012

rafaelfranca added a commit that referenced this issue Sep 20, 2012

Merge pull request #7718 from kennyj/fix_6962-32
[3-2-stable] Fix #6962. AS::TimeWithZone#strftime responds incorrectly to %:z and %::z format strings.

rafaelfranca added a commit that referenced this issue Sep 20, 2012

Merge pull request #7703 from kennyj/fix_6962
Fix #6962. AS::TimeWithZone#strftime responds incorrectly to %:z and %::z format strings.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment