Skip to content

Time/Datetime conversion inconsistency with MRI #4810

@rdubya

Description

@rdubya

Environment

Tested with JRuby 9.1.12.0 and JRuby 9.1.13.0 and compared to MRI 2.3.1

Expected Behavior

  • A Datetime value should be able to be converted to a Time and back to a Datetime and still retain the original value to match MRI's functionality

Actual Behavior

  • The value at the end does not match the value at the beginning

Example Cases

JRuby 9.1.13.0

jruby-9.1.13.0 :001 > datetime = DateTime.parse('-0001-12-31T23:58:59+00:00')
 => #<DateTime: -0001-12-31T23:58:59+00:00 ((1721057j,86339s,0n),+0s,2299161j)>
jruby-9.1.13.0 :002 > datetime.to_time
 => -0001-12-31 19:02:57 -0456
jruby-9.1.13.0 :003 > datetime.to_time.to_datetime
 => #<DateTime: 0000-01-02T19:02:57-04:56 ((1721059j,86339s,0n),-17762s,2299161j)>
jruby-9.1.13.0 :004 > time = Time.new(-1, 12, 31, 23, 59, 59, '+00:00')
 => -0001-12-31 23:59:59 UTC
jruby-9.1.13.0 :005 > time.to_datetime
 => #<DateTime: 0000-01-02T23:59:59+00:00 ((1721059j,86399s,0n),+0s,2299161j)>
jruby-9.1.13.0 :006 > time.to_datetime.to_time
 => 0000-01-02 19:03:57 -0456

MRI 2.3.1

2.3.1 :001 > require 'time'
 => true
2.3.1 :002 > datetime = DateTime.parse('-0001-12-31T23:58:59+00:00')
 => #<DateTime: -0001-12-31T23:58:59+00:00 ((1721057j,86339s,0n),+0s,2299161j)>
2.3.1 :003 > datetime.to_time
 => -0001-12-31 18:58:59 -0500
2.3.1 :004 > datetime.to_time.to_datetime
 => #<DateTime: -0001-12-31T18:58:59-05:00 ((1721057j,86339s,0n),-18000s,2299161j)>
2.3.1 :005 > time = Time.new(-1, 12, 31, 23, 59, 59, '+00:00')
 => -0001-12-31 23:59:59 +0000
2.3.1 :006 > time.to_datetime
 => #<DateTime: -0001-12-31T23:59:59+00:00 ((1721057j,86399s,0n),+0s,2299161j)>
2.3.1 :007 > time.to_datetime.to_time
 => -0001-12-31 18:59:59 -0500

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions