1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[ruby/psych] Dump Date/DateTime as proleptic Gregorian date as well as Time

Fix ruby/psych#572

https://github.com/ruby/psych/commit/92304269bc
This commit is contained in:
Nobuyoshi Nakada 2022-08-09 09:22:53 +09:00 committed by git
parent bfa6d0f6d7
commit cbdde8e0c5
4 changed files with 32 additions and 10 deletions

View file

@ -63,7 +63,7 @@ module Psych
elsif string.match?(/^\d{4}-(?:1[012]|0\d|\d)-(?:[12]\d|3[01]|0\d|\d)$/) elsif string.match?(/^\d{4}-(?:1[012]|0\d|\d)-(?:[12]\d|3[01]|0\d|\d)$/)
require 'date' require 'date'
begin begin
class_loader.date.strptime(string, '%Y-%m-%d') class_loader.date.strptime(string, '%F', Date::GREGORIAN)
rescue ArgumentError rescue ArgumentError
string string
end end

View file

@ -80,7 +80,9 @@ module Psych
when "!ruby/object:DateTime" when "!ruby/object:DateTime"
class_loader.date_time class_loader.date_time
require 'date' unless defined? DateTime require 'date' unless defined? DateTime
@ss.parse_time(o.value).to_datetime t = @ss.parse_time(o.value)
DateTime.civil(*t.to_a[0, 6].reverse, Rational(t.utc_offset, 86400)) +
(t.subsec/86400)
when '!ruby/encoding' when '!ruby/encoding'
::Encoding.find o.value ::Encoding.find o.value
when "!ruby/object:Complex" when "!ruby/object:Complex"

View file

@ -192,12 +192,13 @@ module Psych
register o, @emitter.scalar(o.inspect, nil, '!ruby/regexp', false, false, Nodes::Scalar::ANY) register o, @emitter.scalar(o.inspect, nil, '!ruby/regexp', false, false, Nodes::Scalar::ANY)
end end
def visit_DateTime o def visit_Date o
formatted = if o.offset.zero? register o, visit_Integer(o.gregorian)
o.strftime("%Y-%m-%d %H:%M:%S.%9N Z".freeze)
else
o.strftime("%Y-%m-%d %H:%M:%S.%9N %:z".freeze)
end end
def visit_DateTime o
t = o.italy
formatted = format_time t, t.offset.zero?
tag = '!ruby/object:DateTime' tag = '!ruby/object:DateTime'
register o, @emitter.scalar(formatted, nil, tag, false, false, Nodes::Scalar::ANY) register o, @emitter.scalar(formatted, nil, tag, false, false, Nodes::Scalar::ANY)
end end
@ -235,7 +236,6 @@ module Psych
end end
alias :visit_TrueClass :visit_Integer alias :visit_TrueClass :visit_Integer
alias :visit_FalseClass :visit_Integer alias :visit_FalseClass :visit_Integer
alias :visit_Date :visit_Integer
def visit_Float o def visit_Float o
if o.nan? if o.nan?
@ -482,8 +482,8 @@ module Psych
@emitter.end_mapping @emitter.end_mapping
end end
def format_time time def format_time time, utc = time.utc?
if time.utc? if utc
time.strftime("%Y-%m-%d %H:%M:%S.%9N Z") time.strftime("%Y-%m-%d %H:%M:%S.%9N Z")
else else
time.strftime("%Y-%m-%d %H:%M:%S.%9N %:z") time.strftime("%Y-%m-%d %H:%M:%S.%9N %:z")

View file

@ -44,6 +44,26 @@ module Psych
assert_match(/12:00:00-05:00/, cycled.last.to_s) assert_match(/12:00:00-05:00/, cycled.last.to_s)
end end
def test_julian_date
d = Date.new(1582, 10, 4, Date::GREGORIAN)
assert_cycle d
end
def test_proleptic_gregorian_date
d = Date.new(1582, 10, 14, Date::GREGORIAN)
assert_cycle d
end
def test_julian_datetime
dt = DateTime.new(1582, 10, 4, 23, 58, 59, 0, Date::GREGORIAN)
assert_cycle dt
end
def test_proleptic_gregorian_datetime
dt = DateTime.new(1582, 10, 14, 23, 58, 59, 0, Date::GREGORIAN)
assert_cycle dt
end
def test_invalid_date def test_invalid_date
assert_cycle "2013-10-31T10:40:07-000000000000033" assert_cycle "2013-10-31T10:40:07-000000000000033"
end end