diff --git a/ChangeLog b/ChangeLog index ad5d2e4a73..3d0661eb0a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +Sun Jan 30 16:09:22 2011 Tanaka Akira + + * strftime.c (rb_strftime_with_timespec): %G produces 4 digits. + Sun Jan 30 15:13:19 2011 Nobuyoshi Nakada * enc/emacs_mule.c (emacsmule_islead): 7bit range is also leading diff --git a/strftime.c b/strftime.c index 289f6ed48a..dc6df615d7 100644 --- a/strftime.c +++ b/strftime.c @@ -613,7 +613,13 @@ rb_strftime_with_timespec(char *s, size_t maxsize, const char *format, const str yv = sub(yv, INT2FIX(1)); if (*format == 'G') { - FMTV('0', 1, "d", yv); + if (FIXNUM_P(yv)) { + long y = FIX2LONG(yv); + FMT('0', 0 <= y ? 4 : 5, "ld", y); + } + else { + FMTV('0', 4, "d", yv); + } } else { yv = mod(yv, INT2FIX(100)); diff --git a/test/ruby/test_time.rb b/test/ruby/test_time.rb index ec5395092f..838a7692f7 100644 --- a/test/ruby/test_time.rb +++ b/test/ruby/test_time.rb @@ -590,6 +590,18 @@ class TestTime < Test::Unit::TestCase assert_equal("02", t.strftime("%0l")) assert_equal(" 2", t.strftime("%_l")) + t = Time.utc(1,1,4) + assert_equal("0001", t.strftime("%Y")) + assert_equal("0001", t.strftime("%G")) + + t = Time.utc(0,1,4) + assert_equal("0000", t.strftime("%Y")) + assert_equal("0000", t.strftime("%G")) + + t = Time.utc(-1,1,4) + assert_equal("-0001", t.strftime("%Y")) + assert_equal("-0001", t.strftime("%G")) + # [ruby-dev:37155] t = Time.mktime(1970, 1, 18) assert_equal("0", t.strftime("%w"))