Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions ext/date/date_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -4098,6 +4098,16 @@ rt_rewrite_frags(VALUE hash)
if (!NIL_P(seconds)) {
VALUE offset, d, h, min, s, fr;

fr = ref_hash("sec_fraction");
if (!NIL_P(fr) && RB_INTEGER_TYPE_P(seconds)) {
/* "%s.%N": the fraction has the sign of the seconds,
as in Time.strptime. */
if (f_negative_p(seconds))
seconds = f_sub(seconds, fr);
else
seconds = f_add(seconds, fr);
}

offset = ref_hash("offset");
if (!NIL_P(offset))
seconds = f_add(seconds, offset);
Expand Down
26 changes: 26 additions & 0 deletions test/date/test_date_strptime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,32 @@ def test_strptime__minus
[d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.sec_fraction])
end

def test_strptime__seconds_with_fraction
d = DateTime.strptime('1.5', '%s.%N')
assert_equal([1970, 1, 1, 0, 0, 1, 1.to_r/2],
[d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.sec_fraction])
d = DateTime.strptime('981158400.123456789', '%s.%N')
assert_equal([2001, 2, 3, 0, 0, 0, 123456789.to_r/10**9],
[d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.sec_fraction])
d = DateTime.strptime('981158400 123', '%s %L')
assert_equal([2001, 2, 3, 0, 0, 0, 123.to_r/10**3],
[d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.sec_fraction])

# The fraction takes the sign of the seconds, as in Time.strptime.
d = DateTime.strptime('-1.25', '%s.%N')
assert_equal([1969, 12, 31, 23, 59, 58, 3.to_r/4],
[d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.sec_fraction])

d = DateTime.strptime('1.5 +0900', '%s.%N %z')
assert_equal([1970, 1, 1, 9, 0, 1, 1.to_r/2, 9.to_r/24],
[d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.sec_fraction, d.offset])

# %Q already holds the fraction.
d = DateTime.strptime('1500 500000000', '%Q %N')
assert_equal([1970, 1, 1, 0, 0, 1, 1.to_r/2],
[d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.sec_fraction])
end

def test_strptime__comp
n = DateTime.now

Expand Down