aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog6
-rw-r--r--lib/time.rb12
-rw-r--r--strftime.c8
-rw-r--r--test/test_time.rb9
4 files changed, 28 insertions, 7 deletions
diff --git a/ChangeLog b/ChangeLog
index caf113894f..c889009b2f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Tue Nov 24 20:11:37 2009 Tanaka Akira <akr@fsij.org>
+
+ * strftime.c: %Y format a year with 4 digits at least.
+
+ * lib/time.rb: format a year with 4 digits at least.
+
Tue Nov 24 20:05:27 2009 Tanaka Akira <akr@fsij.org>
* defs/known_errors.def: more errors.
diff --git a/lib/time.rb b/lib/time.rb
index 2e6ed324e3..f2ba18e51c 100644
--- a/lib/time.rb
+++ b/lib/time.rb
@@ -432,9 +432,9 @@ class Time
# If +self+ is a UTC time, -0000 is used as zone.
#
def rfc2822
- sprintf('%s, %02d %s %04d %02d:%02d:%02d ',
+ sprintf('%s, %02d %s %0*d %02d:%02d:%02d ',
RFC2822_DAY_NAME[wday],
- day, RFC2822_MONTH_NAME[mon-1], year,
+ day, RFC2822_MONTH_NAME[mon-1], year < 0 ? 5 : 4, year,
hour, min, sec) +
if utc?
'-0000'
@@ -464,9 +464,9 @@ class Time
#
def httpdate
t = dup.utc
- sprintf('%s, %02d %s %04d %02d:%02d:%02d GMT',
+ sprintf('%s, %02d %s %0*d %02d:%02d:%02d GMT',
RFC2822_DAY_NAME[t.wday],
- t.day, RFC2822_MONTH_NAME[t.mon-1], t.year,
+ t.day, RFC2822_MONTH_NAME[t.mon-1], t.year < 0 ? 5 : 4, t.year,
t.hour, t.min, t.sec)
end
@@ -485,8 +485,8 @@ class Time
# Its default value is 0.
#
def xmlschema(fraction_digits=0)
- sprintf('%04d-%02d-%02dT%02d:%02d:%02d',
- year, mon, day, hour, min, sec) +
+ sprintf('%0*d-%02d-%02dT%02d:%02d:%02d',
+ year < 0 ? 5 : 4, year, mon, day, hour, min, sec) +
if fraction_digits == 0
''
else
diff --git a/strftime.c b/strftime.c
index addfe6b862..6ad74de085 100644
--- a/strftime.c
+++ b/strftime.c
@@ -496,7 +496,13 @@ rb_strftime(char *s, size_t maxsize, const char *format, const struct vtm *vtm,
continue;
case 'Y': /* year with century */
- FMTV('0', 1, "d", vtm->year);
+ if (FIXNUM_P(vtm->year)) {
+ long y = FIX2LONG(vtm->year);
+ FMT('0', 0 <= y ? 4 : 5, "ld", y);
+ }
+ else {
+ FMTV('0', 4, "d", vtm->year);
+ }
continue;
#ifdef MAILHEADER_EXT
diff --git a/test/test_time.rb b/test/test_time.rb
index 2a22c6069f..7cb75ab6c8 100644
--- a/test/test_time.rb
+++ b/test/test_time.rb
@@ -188,6 +188,15 @@ class TestTimeExtension < Test::Unit::TestCase # :nodoc:
end
assert_equal(249, Time.xmlschema("2008-06-05T23:49:23.000249+09:00").usec)
+
+ assert_equal("10000-01-01T00:00:00Z", Time.utc(10000).xmlschema)
+ assert_equal("9999-01-01T00:00:00Z", Time.utc(9999).xmlschema)
+ assert_equal("0001-01-01T00:00:00Z", Time.utc(1).xmlschema) # 1 AD
+ assert_equal("0000-01-01T00:00:00Z", Time.utc(0).xmlschema) # 1 BC
+ assert_equal("-0001-01-01T00:00:00Z", Time.utc(-1).xmlschema) # 2 BC
+ assert_equal("-0004-01-01T00:00:00Z", Time.utc(-4).xmlschema) # 5 BC
+ assert_equal("-9999-01-01T00:00:00Z", Time.utc(-9999).xmlschema)
+ assert_equal("-10000-01-01T00:00:00Z", Time.utc(-10000).xmlschema)
end
def test_completion