From dc54e380415cfdf442dafbc6ede8ab433af41fe2 Mon Sep 17 00:00:00 2001 From: nobu Date: Thu, 24 Mar 2016 15:49:43 +0000 Subject: strftime.c: Bignum conversion * strftime.c (format_value): convert from Bignum to String, instead of rb_str_format. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54253 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- strftime.c | 20 +++++++++++--------- test/ruby/test_time.rb | 9 +++++++++ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/strftime.c b/strftime.c index 81e56d1f7b..542091d874 100644 --- a/strftime.c +++ b/strftime.c @@ -196,14 +196,11 @@ case_conv(char *s, ptrdiff_t i, int flags) } static VALUE -format_value(const char *fmt, VALUE val, int precision) +format_value(VALUE val, int base) { - struct RString fmtv; - VALUE str = rb_setup_fake_str(&fmtv, fmt, strlen(fmt), 0); - VALUE args[2]; - args[0] = INT2FIX(precision); - args[1] = val; - return rb_str_format(2, args, str); + if (!RB_TYPE_P(val, T_BIGNUM)) + val = rb_Integer(val); + return rb_big2str(val, base); } /* @@ -323,9 +320,14 @@ rb_strftime_with_timespec(VALUE ftime, const char *format, size_t format_len, FMT((def_pad), (def_prec), "l"fmt, FIX2LONG(tmp)); \ } \ else { \ - const char *fmts = FMT_PADDING(fmt, def_pad); \ + const int base = ((fmt[0] == 'x') ? 16 : \ + (fmt[0] == 'o') ? 8 : \ + 10); \ precision = FMT_PRECISION(def_prec); \ - tmp = format_value(fmts, tmp, precision); \ + if (!padding) padding = (def_pad); \ + tmp = format_value(tmp, base); \ + i = RSTRING_LEN(tmp); \ + FILL_PADDING(i); \ rb_str_set_len(ftime, s-start); \ rb_str_append(ftime, tmp); \ RSTRING_GETMEM(ftime, s, len); \ diff --git a/test/ruby/test_time.rb b/test/ruby/test_time.rb index c75cffaacf..c7d4487553 100644 --- a/test/ruby/test_time.rb +++ b/test/ruby/test_time.rb @@ -680,6 +680,12 @@ class TestTime < Test::Unit::TestCase t = Time.at(946684800, 123456.789) assert_equal("946684800", t.strftime("%s")) assert_equal("946684800", t.utc.strftime("%s")) + + t = Time.at(10000000000000000000000) + assert_equal("<<10000000000000000000000>>", t.strftime("<<%s>>")) + assert_equal("<<010000000000000000000000>>", t.strftime("<<%24s>>")) + assert_equal("<<010000000000000000000000>>", t.strftime("<<%024s>>")) + assert_equal("<< 10000000000000000000000>>", t.strftime("<<%_24s>>")) end def test_strftime_zone @@ -759,6 +765,9 @@ class TestTime < Test::Unit::TestCase t = Time.utc(10000000000000000000000,1,1) assert_equal("<<10000000000000000000000>>", t.strftime("<<%Y>>")) + assert_equal("<<010000000000000000000000>>", t.strftime("<<%24Y>>")) + assert_equal("<<010000000000000000000000>>", t.strftime("<<%024Y>>")) + assert_equal("<< 10000000000000000000000>>", t.strftime("<<%_24Y>>")) end def test_strftime_weeknum -- cgit v1.2.3