summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2002-01-07 03:56:55 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2002-01-07 03:56:55 +0000
commit6a100f3ce470fe659a88e46138068b7a8611f650 (patch)
tree51bc8ac50cd495ddfe4a49d12db6f1d7c0c6b11d /lib
parent427237f8891e9a22cf9ce6e64aed9a249f18879a (diff)
* 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
Diffstat (limited to 'lib')
-rw-r--r--lib/time.rb33
1 files changed, 26 insertions, 7 deletions
diff --git a/lib/time.rb b/lib/time.rb
index abf90630be..5559241dc9 100644
--- a/lib/time.rb
+++ b/lib/time.rb
@@ -346,25 +346,32 @@ class Time
end
=begin
---- Time#xmlschema
---- Time#iso8601
+--- Time#xmlschema([fractional_seconds])
+--- Time#iso8601([fractional_seconds])
returns a string which represents the time as dateTime
defined by XML Schema:
CCYY-MM-DDThh:mm:ssTZD
+ CCYY-MM-DDThh:mm:ss.sssTZD
where TZD is Z or [+-]hh:mm.
If self is a UTC time, Z is used as TZD.
[+-]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
- def xmlschema
+ def xmlschema(fraction_digits=0)
sprintf('%d-%02d-%02dT%02d:%02d:%02d',
year, mon, day, hour, min, sec) +
- if usec == 0
+ if fraction_digits == 0
''
+ elsif fraction_digits <= 6
+ '.' + sprintf('%06d', usec)[0, fraction_digits]
else
- sprintf('.%06d', usec).sub(/0*\z/, '')
+ '.' + sprintf('%06d', usec) + '0' * (fraction_digits - 6)
end +
if utc?
'Z'
@@ -479,8 +486,20 @@ if __FILE__ == $0
end
def test_encode_xmlschema
- assert_equal("2001-04-17T19:23:17.3Z",
- Time.utc(2001, 4, 17, 19, 23, 17, 300000).xmlschema)
+ t = Time.utc(2001, 4, 17, 19, 23, 17, 300000)
+ 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
def test_completion