mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
[ruby/date] Fix Time#to_datetime before calendar reform
Time is always in the proleptic Gregorian calendar.
Also DateTime#to_time should convert to the Gregorian calendar first,
before extracting its components.
https://bugs.ruby-lang.org/issues/18946#change-98527
b2aee75248
This commit is contained in:
parent
43239b23b4
commit
e07d450dea
2 changed files with 26 additions and 4 deletions
|
@ -8811,7 +8811,7 @@ time_to_datetime(VALUE self)
|
||||||
ret = d_complex_new_internal(cDateTime,
|
ret = d_complex_new_internal(cDateTime,
|
||||||
nth, 0,
|
nth, 0,
|
||||||
0, sf,
|
0, sf,
|
||||||
of, DEFAULT_SG,
|
of, GREGORIAN,
|
||||||
ry, m, d,
|
ry, m, d,
|
||||||
h, min, s,
|
h, min, s,
|
||||||
HAVE_CIVIL | HAVE_TIME);
|
HAVE_CIVIL | HAVE_TIME);
|
||||||
|
@ -8915,12 +8915,17 @@ date_to_datetime(VALUE self)
|
||||||
static VALUE
|
static VALUE
|
||||||
datetime_to_time(VALUE self)
|
datetime_to_time(VALUE self)
|
||||||
{
|
{
|
||||||
volatile VALUE dup = dup_obj(self);
|
get_d1(self);
|
||||||
|
|
||||||
|
if (m_julian_p(dat)) {
|
||||||
|
self = d_lite_gregorian(self);
|
||||||
|
get_d1a(self);
|
||||||
|
dat = adat;
|
||||||
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
VALUE t;
|
VALUE t;
|
||||||
|
|
||||||
get_d1(dup);
|
|
||||||
|
|
||||||
t = rb_funcall(rb_cTime,
|
t = rb_funcall(rb_cTime,
|
||||||
rb_intern("new"),
|
rb_intern("new"),
|
||||||
7,
|
7,
|
||||||
|
|
|
@ -77,6 +77,11 @@ class TestDateConv < Test::Unit::TestCase
|
||||||
assert_equal([2004, 9, 19, 1, 2, 3, 456789],
|
assert_equal([2004, 9, 19, 1, 2, 3, 456789],
|
||||||
[t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.usec])
|
[t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.usec])
|
||||||
|
|
||||||
|
d = DateTime.new(1582, 10, 3, 1, 2, 3, 0) + 456789.to_r/86400000000
|
||||||
|
t = d.to_time.utc
|
||||||
|
assert_equal([1582, 10, 13, 1, 2, 3, 456789],
|
||||||
|
[t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.usec])
|
||||||
|
|
||||||
if Time.allocate.respond_to?(:nsec)
|
if Time.allocate.respond_to?(:nsec)
|
||||||
d = DateTime.new(2004, 9, 19, 1, 2, 3, 0) + 456789123.to_r/86400000000000
|
d = DateTime.new(2004, 9, 19, 1, 2, 3, 0) + 456789123.to_r/86400000000000
|
||||||
t = d.to_time.utc
|
t = d.to_time.utc
|
||||||
|
@ -100,6 +105,10 @@ class TestDateConv < Test::Unit::TestCase
|
||||||
t = Time.utc(2004, 9, 19, 1, 2, 3, 456789)
|
t = Time.utc(2004, 9, 19, 1, 2, 3, 456789)
|
||||||
d = t.to_date
|
d = t.to_date
|
||||||
assert_equal([2004, 9, 19, 0], [d.year, d.mon, d.mday, d.day_fraction])
|
assert_equal([2004, 9, 19, 0], [d.year, d.mon, d.mday, d.day_fraction])
|
||||||
|
|
||||||
|
t = Time.utc(1582, 10, 13, 1, 2, 3, 456789)
|
||||||
|
d = t.to_date # using ITALY
|
||||||
|
assert_equal([1582, 10, 3, 0], [d.year, d.mon, d.mday, d.day_fraction])
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_to_date__from_date
|
def test_to_date__from_date
|
||||||
|
@ -136,6 +145,14 @@ class TestDateConv < Test::Unit::TestCase
|
||||||
[d.year, d.mon, d.mday, d.hour, d.min, d.sec,
|
[d.year, d.mon, d.mday, d.hour, d.min, d.sec,
|
||||||
d.sec_fraction, d.offset])
|
d.sec_fraction, d.offset])
|
||||||
|
|
||||||
|
t = Time.utc(1582, 10, 13, 1, 2, 3, 456789)
|
||||||
|
d = t.to_datetime # using ITALY
|
||||||
|
assert_equal([1582, 10, 3, 1, 2, 3,
|
||||||
|
456789.to_r/1000000,
|
||||||
|
0],
|
||||||
|
[d.year, d.mon, d.mday, d.hour, d.min, d.sec,
|
||||||
|
d.sec_fraction, d.offset])
|
||||||
|
|
||||||
t = Time.now
|
t = Time.now
|
||||||
d = t.to_datetime
|
d = t.to_datetime
|
||||||
require 'time'
|
require 'time'
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue