Time#to_s with different UTC offset gives local hour with given offset #1517

Closed
jgaskins opened this Issue Feb 21, 2014 · 5 comments

Projects

None yet

5 participants

@jgaskins
$ jruby -v
jruby 1.7.10 (2.0.0p195) 2014-01-09 c4ecd6b on Java HotSpot(TM) 64-Bit Server VM 1.7.0_17-b02 [darwin-x86_64]

My UTC offset is -05:00. When I create a time with UTC offset +05:00, it adds 10 hours, but displays it with the given offset.

t = Time.new(2014, 1, 2, 3, 4, 5, '+05:00').to_s
# Expected: 2014-01-02 03:04:05 +0500
# Actual  : 2014-01-02 13:04:05 +0500

The actual hour is correct when you run time.hour, but the string rendering is off.

@jgaskins jgaskins referenced this issue in headius/jruby-pg Feb 21, 2014
Closed

Missing some exceptions #15

@k77ch7
Contributor
k77ch7 commented Apr 28, 2014

I have confirmed the same problem.
And 'eql?' method returns the wrong value.

jruby 1.7.12

$ ruby -v
jruby 1.7.12 (1.9.3p392) 2014-04-15 643e292 on Java HotSpot(TM) 64-Bit Server VM 1.8.0_05-b13 +indy [darwin-x86_64]
$ irb
jruby-1.7.12 :001 > t1 = Time.new(2014, 1, 2, 3, 4, 5, '+05:00')
 => 2014-01-02 13:04:05 +0500
jruby-1.7.12 :002 > t2 = Time.new(2014, 1, 2, 3, 4, 5, 18000)
 => 2014-01-02 03:04:05 +0500
jruby-1.7.12 :003 > t1.to_s == t2.to_s
 => false
jruby-1.7.12 :004 > t1.eql? t2
 => false

ruby 1.9.3

$ ruby -v
ruby 1.9.3p545 (2014-02-24 revision 45159) [x86_64-darwin13.1.0]
$ irb
1.9.3-p545 :001 > t2 = Time.new(2014, 1, 2, 3, 4, 5, 18000)
 => 2014-01-02 03:04:05 +0500
1.9.3-p545 :002 > t1 = Time.new(2014, 1, 2, 3, 4, 5, '+05:00')
 => 2014-01-02 03:04:05 +0500
1.9.3-p545 :003 > t1.to_s == t2.to_s
 => true
1.9.3-p545 :004 > t1.eql? t2
 => true 
@jeffwatts

I'm seeing the same issue, which to me seems to be limited to the string offset argument. The integer offset appears to work as expected. Here's a test I wrote that fails on all JRuby versions I've tried (up to 1.7.12) and passes on all CRuby implementations I've tried:

require 'rspec'

describe Time do
  describe '#new' do
    let(:year) { 2014 }
    let(:month) { 5 }
    let(:day) { 21 }
    let(:hour) { 22 }
    let(:minute) { 51 }
    let(:second) { 23 }

    # These two timezone offsets should be equivalent
    let(:offset_int) { -25200 }
    let(:offset_str) { '-07:00' }

    let(:instance_int) { Time.new(year, month, day, hour, minute, second, offset_int) }
    let(:instance_str) { Time.new(year, month, day, hour, minute, second, offset_str) }

    it 'creates equal instances with both UTC offset representations' do
      # Fails on JRuby 1.7.10 with
      #  expected: 2014-05-21 22:51:23 -0700
      #       got: 2014-05-21 08:51:23 -0700
      # 
      #  (compared using ==)
      expect(instance_str).to eq(instance_int)
    end
  end
end
@k77ch7
Contributor
k77ch7 commented May 26, 2014

@jeffwatts I added a test case you wrote. Pull request #1684 passes the test case.

@atambo atambo added a commit that referenced this issue Jun 20, 2014
@atambo atambo Fix incorrect regression spec
After the fix for issue #1517 this regression spec can now have
the correct local hour.
0c1c315
@atambo atambo added this to the JRuby 1.7.13 milestone Jun 20, 2014
@atambo
Member
atambo commented Jun 20, 2014

This should be fixed in 1.7.13.

@atambo atambo closed this Jun 20, 2014
@rjattrill

Time.new with UTC offset working much better in 1.7.13. Thank you.

Wrong in 1.7.12:

C:\>jruby -v -e 'p Time.new(2011, 7, 15, nil, nil, nil, '"'-08:00'"')'
jruby 1.7.12 (1.9.3p392) 2014-04-15 643e292 on Java HotSpot(TM) 64-Bit Server VM 1.7.0_07-b11 [Windows 7-amd64]
2011-07-14 08:00:00 -0800

Fixed in 1.7.13:

C:\>jruby -v -e 'p Time.new(2011, 7, 15, nil, nil, nil, '"'-08:00'"')'
jruby 1.7.13 (1.9.3p392) 2014-06-24 43f133c on Java HotSpot(TM) 64-Bit Server VM 1.7.0_07-b11 [Windows 7-amd64]
2011-07-15 00:00:00 -0800
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment