From: spiketeika@... Date: 2016-05-21T04:45:46+00:00 Subject: [ruby-core:75653] [Ruby trunk Misc#10473] Date.to_datetime.to_time != Date.to_time Issue #10473 has been updated by Kaneko Yuichiro. `Date#to_time` interprets date as the local time zone. But `Date#to_datetime` interprets date as UTC. I think this incompatibility is a king of bug. We have two ways to solve this incompatibility, to make both methods to interpret date as the local time zone, or to interpret date UTC. It is difficult to judge which is more natural or useful. But I always interpret date as the local time zone in my daily life... ```diff diff --git a/ext/date/date_core.c b/ext/date/date_core.c index 3a10fcb..4cc3dc9 100644 --- a/ext/date/date_core.c +++ b/ext/date/date_core.c @@ -8520,35 +8520,7 @@ date_to_date(VALUE self) static VALUE date_to_datetime(VALUE self) { - get_d1a(self); - - if (simple_dat_p(adat)) { - VALUE new = d_lite_s_alloc_simple(cDateTime); - { - get_d1b(new); - bdat->s = adat->s; - return new; - } - } - else { - VALUE new = d_lite_s_alloc_complex(cDateTime); - { - get_d1b(new); - bdat->c = adat->c; - bdat->c.df = 0; - RB_OBJ_WRITE(new, &bdat->c.sf, INT2FIX(0)); -#ifndef USE_PACK - bdat->c.hour = 0; - bdat->c.min = 0; - bdat->c.sec = 0; -#else - bdat->c.pc = PACK5(EX_MON(adat->c.pc), EX_MDAY(adat->c.pc), - 0, 0, 0); - bdat->c.flags |= HAVE_DF | HAVE_TIME; -#endif - return new; - } - } + return time_to_datetime(date_to_time(self)); } /* diff --git a/test/date/test_date_conv.rb b/test/date/test_date_conv.rb index 3729476..0feaf2a 100644 --- a/test/date/test_date_conv.rb +++ b/test/date/test_date_conv.rb @@ -126,10 +126,13 @@ def test_to_datetime__from_time def test_to_datetime__from_date d = Date.new(2004, 9, 19) + 1.to_r/2 - d2 = d.to_datetime - assert_equal([2004, 9, 19, 0, 0, 0, 0, 0], - [d2.year, d2.mon, d2.mday, d2.hour, d2.min, d2.sec, - d2.sec_fraction, d2.offset]) + + with_tz('Asia/Tokyo') do + d2 = d.to_datetime + assert_equal([2004, 9, 19, 0, 0, 0, 0, (3.to_r/8)], + [d2.year, d2.mon, d2.mday, d2.hour, d2.min, d2.sec, + d2.sec_fraction, d2.offset]) + end end def test_to_datetime__from_datetime ``` ---------------------------------------- Misc #10473: Date.to_datetime.to_time != Date.to_time https://bugs.ruby-lang.org/issues/10473#change-58786 * Author: Jack Lowe * Status: Open * Priority: Normal * Assignee: ---------------------------------------- Date.new(2014,1,1).to_datetime.to_time.utc.to_s => "2014-01-01 00:00:00 UTC" Date.new(2014,1,1).to_time.utc.to_s => "2014-01-01 05:00:00 UTC" -- https://bugs.ruby-lang.org/ Unsubscribe: