* lib/time.rb (Time#xmlschema): new optional argument

fractional_seconds to specify a number of digits of
fractional part of the time.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1968 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2002-01-07 03:56:55 +00:00
parent 427237f889
commit 6a100f3ce4
2 changed files with 32 additions and 7 deletions

View File

@ -1,3 +1,9 @@
Mon Jan 7 12:38:47 2002 Tanaka Akira <akr@m17n.org>
* lib/time.rb (Time#xmlschema): new optional argument
fractional_seconds to specify a number of digits of
fractional part of the time.
Sat Jan 5 13:18:11 2002 Nobuyoshi Nakada <nobu.nakada@nifty.ne.jp> Sat Jan 5 13:18:11 2002 Nobuyoshi Nakada <nobu.nakada@nifty.ne.jp>
* range.c (range_member): beginning check was * range.c (range_member): beginning check was

View File

@ -346,25 +346,32 @@ class Time
end end
=begin =begin
--- Time#xmlschema --- Time#xmlschema([fractional_seconds])
--- Time#iso8601 --- Time#iso8601([fractional_seconds])
returns a string which represents the time as dateTime returns a string which represents the time as dateTime
defined by XML Schema: defined by XML Schema:
CCYY-MM-DDThh:mm:ssTZD CCYY-MM-DDThh:mm:ssTZD
CCYY-MM-DDThh:mm:ss.sssTZD
where TZD is Z or [+-]hh:mm. where TZD is Z or [+-]hh:mm.
If self is a UTC time, Z is used as TZD. If self is a UTC time, Z is used as TZD.
[+-]hh:mm is used otherwise. [+-]hh:mm is used otherwise.
((|fractional_seconds|)) specify as a number of digits of
fractional seconds.
The default value of ((|fractional_seconds|)) is 0.
=end =end
def xmlschema def xmlschema(fraction_digits=0)
sprintf('%d-%02d-%02dT%02d:%02d:%02d', sprintf('%d-%02d-%02dT%02d:%02d:%02d',
year, mon, day, hour, min, sec) + year, mon, day, hour, min, sec) +
if usec == 0 if fraction_digits == 0
'' ''
elsif fraction_digits <= 6
'.' + sprintf('%06d', usec)[0, fraction_digits]
else else
sprintf('.%06d', usec).sub(/0*\z/, '') '.' + sprintf('%06d', usec) + '0' * (fraction_digits - 6)
end + end +
if utc? if utc?
'Z' 'Z'
@ -479,8 +486,20 @@ if __FILE__ == $0
end end
def test_encode_xmlschema def test_encode_xmlschema
assert_equal("2001-04-17T19:23:17.3Z", t = Time.utc(2001, 4, 17, 19, 23, 17, 300000)
Time.utc(2001, 4, 17, 19, 23, 17, 300000).xmlschema) assert_equal("2001-04-17T19:23:17Z", t.xmlschema)
assert_equal("2001-04-17T19:23:17.3Z", t.xmlschema(1))
assert_equal("2001-04-17T19:23:17.300000Z", t.xmlschema(6))
assert_equal("2001-04-17T19:23:17.3000000Z", t.xmlschema(7))
t = Time.utc(2001, 4, 17, 19, 23, 17, 123456)
assert_equal("2001-04-17T19:23:17.1234560Z", t.xmlschema(7))
assert_equal("2001-04-17T19:23:17.123456Z", t.xmlschema(6))
assert_equal("2001-04-17T19:23:17.12345Z", t.xmlschema(5))
assert_equal("2001-04-17T19:23:17.1Z", t.xmlschema(1))
t = Time.utc(1960, 12, 31, 23, 0, 0, 123456)
assert_equal("1960-12-31T23:00:00.123456Z", t.xmlschema(6))
end end
def test_completion def test_completion