Skip to content

Commit

Permalink
fixup! Implement Date#deconstruct_keys and DateTime#deconstruct_keys
Browse files Browse the repository at this point in the history
  • Loading branch information
zverok committed Dec 12, 2022
1 parent c4708bb commit ef6f6dc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
6 changes: 1 addition & 5 deletions ext/date/date_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -7438,14 +7438,12 @@ d_lite_jisx0301(VALUE self)

static VALUE
deconstruct_keys(VALUE self, VALUE keys, int is_datetime) {
VALUE h;
VALUE h = rb_hash_new();
long i;

get_d1(self);

if (NIL_P(keys)) {
h = rb_hash_new();

rb_hash_aset(h, sym_year, m_real_year(dat));
rb_hash_aset(h, sym_month, INT2FIX(m_mon(dat)));
rb_hash_aset(h, sym_day, INT2FIX(m_mday(dat)));
Expand All @@ -7468,8 +7466,6 @@ deconstruct_keys(VALUE self, VALUE keys, int is_datetime) {

}

h = rb_hash_new();

for (i=0; i<RARRAY_LEN(keys); i++) {
VALUE key = RARRAY_AREF(keys, i);

Expand Down
12 changes: 11 additions & 1 deletion test/date/test_date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,27 @@ def test_deconstruct_keys
d = Date.new(1999,5,23)
assert_equal({year: 1999, month: 5, day: 23, wday: 0, yday: 143}, d.deconstruct_keys(nil))
assert_equal({year: 1999}, d.deconstruct_keys([:year, :century]))
assert_equal(
{year: 1999, month: 5, day: 23, wday: 0, yday: 143},
d.deconstruct_keys([:year, :month, :day, :wday, :yday])
)

dt = DateTime.new(1999, 5, 23, 4, 20, Rational(1, 10000))

assert_equal(
{year: 1999, month: 5, day: 23, wday: 0, yday: 143,
hour: 4, min: 20, sec: 13, sec: 0, sec_fraction: Rational(1, 10000), zone: "+00:00"},
hour: 4, min: 20, sec: 0, sec_fraction: Rational(1, 10000), zone: "+00:00"},
dt.deconstruct_keys(nil)
)

assert_equal({year: 1999}, dt.deconstruct_keys([:year, :century]))

assert_equal(
{year: 1999, month: 5, day: 23, wday: 0, yday: 143,
hour: 4, min: 20, sec: 0, sec_fraction: Rational(1, 10000), zone: "+00:00"},
dt.deconstruct_keys([:year, :month, :day, :wday, :yday, :hour, :min, :sec, :sec_fraction, :zone])
)

dtz = DateTime.parse('3rd Feb 2001 04:05:06+03:30')
assert_equal({zone: '+03:30'}, dtz.deconstruct_keys([:zone]))
end
Expand Down

0 comments on commit ef6f6dc

Please sign in to comment.