diff options
-rw-r--r-- | lib/psych/visitors/to_ruby.rb | 2 | ||||
-rw-r--r-- | lib/psych/visitors/yast_builder.rb | 11 | ||||
-rw-r--r-- | test/visitors/test_yast_builder.rb | 4 |
3 files changed, 16 insertions, 1 deletions
diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb index b4e33ed..40236ce 100644 --- a/lib/psych/visitors/to_ruby.rb +++ b/lib/psych/visitors/to_ruby.rb @@ -40,7 +40,7 @@ module Psych date, time = *(lexeme.split(/[ tT]/, 2)) (yy, m, dd) = date.split('-').map { |x| x.to_i } - md = time.match /(\d+:\d+:\d+)(\.\d*)?\s*(Z|[-+]\d+(:\d\d)?)?/ + md = time.match(/(\d+:\d+:\d+)(\.\d*)?\s*(Z|[-+]\d+(:\d\d)?)?/) (hh, mm, ss) = md[1].split(':').map { |x| x.to_i } diff --git a/lib/psych/visitors/yast_builder.rb b/lib/psych/visitors/yast_builder.rb index 9111f8b..1f1eae4 100644 --- a/lib/psych/visitors/yast_builder.rb +++ b/lib/psych/visitors/yast_builder.rb @@ -21,6 +21,17 @@ module Psych raise TypeError, "Can't dump #{target.class}" end + def visit_Time o + formatted = o.strftime("%Y-%m-%d %H:%M:%S") + if o.utc? + formatted += ".%06dZ" % [o.usec] + else + formatted += ".%06d %d:00" % [o.usec, o.gmt_offset / 3600] + end + + append Nodes::Scalar.new formatted + end + def visit_Date o append Nodes::Scalar.new o.to_s end diff --git a/test/visitors/test_yast_builder.rb b/test/visitors/test_yast_builder.rb index 2f6e443..8205430 100644 --- a/test/visitors/test_yast_builder.rb +++ b/test/visitors/test_yast_builder.rb @@ -8,6 +8,10 @@ module Psych @v = Visitors::YASTBuilder.new end + def test_time + assert_round_trip Time.now + end + def test_date date = Date.strptime('2002-12-14', '%Y-%m-%d') assert_round_trip date |