Skip to content

Commit

Permalink
* strftime.c: %Y format a year with 4 digits at least.
Browse files Browse the repository at this point in the history
* lib/time.rb: format a year with 4 digits at least.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25906 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
akr committed Nov 24, 2009
1 parent db1564e commit 54370de
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Tue Nov 24 20:11:37 2009 Tanaka Akira <[email protected]>

* 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 <[email protected]>

* defs/known_errors.def: more errors.
Expand Down
12 changes: 6 additions & 6 deletions lib/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,9 @@ def xmlschema(date)
# 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'
Expand Down Expand Up @@ -464,9 +464,9 @@ def rfc2822
#
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

Expand All @@ -485,8 +485,8 @@ def httpdate
# 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
Expand Down
8 changes: 7 additions & 1 deletion strftime.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions test/test_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@ def test_encode_xmlschema
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
Expand Down

0 comments on commit 54370de

Please sign in to comment.