From a0a292cd2be7852f4d1bcc8e57d99ed88e24a6b2 Mon Sep 17 00:00:00 2001 From: Charlie Somerville Date: Sun, 6 Oct 2013 20:13:42 -0500 Subject: [PATCH] datetime: fix strptime '%s %z' Before: DateTime.strptime('0 +0100', '%s %z').strftime('%s %z') => "0 +0000" After: DateTime.strptime('0 +0100', '%s %z').strftime('%s %z') => "0 +0100" Signed-off-by: Felipe Contreras --- ext/date/date_core.c | 8 ++++++-- test/date/test_date_strptime.rb | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/ext/date/date_core.c b/ext/date/date_core.c index 1685d15..ab60322 100644 --- a/ext/date/date_core.c +++ b/ext/date/date_core.c @@ -3663,12 +3663,17 @@ date_s_today(int argc, VALUE *argv, VALUE klass) static VALUE rt_rewrite_frags(VALUE hash) { - VALUE seconds; + VALUE seconds, offset; seconds = ref_hash("seconds"); if (!NIL_P(seconds)) { VALUE d, h, min, s, fr; + offset = ref_hash("offset"); + if(!NIL_P(offset)) { + seconds = f_add(seconds, offset); + } + d = f_idiv(seconds, INT2FIX(DAY_IN_SECONDS)); fr = f_mod(seconds, INT2FIX(DAY_IN_SECONDS)); @@ -3687,7 +3692,6 @@ rt_rewrite_frags(VALUE hash) set_hash("sec", s); set_hash("sec_fraction", fr); del_hash("seconds"); - del_hash("offset"); } return hash; } diff --git a/test/date/test_date_strptime.rb b/test/date/test_date_strptime.rb index 40d4e22..60db664 100644 --- a/test/date/test_date_strptime.rb +++ b/test/date/test_date_strptime.rb @@ -310,6 +310,7 @@ class TestDateStrptime < Test::Unit::TestCase DateTime.strptime('2002-03-14T11:22:33-09:00', '%FT%T%Z')) assert_equal(DateTime.new(2002,3,14,11,22,33, -9.to_r/24) + 123456789.to_r/1000000000/86400, DateTime.strptime('2002-03-14T11:22:33.123456789-09:00', '%FT%T.%N%Z')) + assert_equal(Rational(1, 24), DateTime.strptime('0 +0100', '%s %z').offset) end def test_strptime__2 -- 1.9.2+fc1.20.g204a630