Skip to content

Commit 9230426

Browse files
committed
Dump Date/DateTime as proleptic Gregorian date as well as Time
Fix #572
1 parent 2366075 commit 9230426

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

lib/psych/scalar_scanner.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def tokenize string
6363
elsif string.match?(/^\d{4}-(?:1[012]|0\d|\d)-(?:[12]\d|3[01]|0\d|\d)$/)
6464
require 'date'
6565
begin
66-
class_loader.date.strptime(string, '%Y-%m-%d')
66+
class_loader.date.strptime(string, '%F', Date::GREGORIAN)
6767
rescue ArgumentError
6868
string
6969
end

lib/psych/visitors/to_ruby.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ def deserialize o
8080
when "!ruby/object:DateTime"
8181
class_loader.date_time
8282
require 'date' unless defined? DateTime
83-
@ss.parse_time(o.value).to_datetime
83+
t = @ss.parse_time(o.value)
84+
DateTime.civil(*t.to_a[0, 6].reverse, Rational(t.utc_offset, 86400)) +
85+
(t.subsec/86400)
8486
when '!ruby/encoding'
8587
::Encoding.find o.value
8688
when "!ruby/object:Complex"

lib/psych/visitors/yaml_tree.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,13 @@ def visit_Regexp o
192192
register o, @emitter.scalar(o.inspect, nil, '!ruby/regexp', false, false, Nodes::Scalar::ANY)
193193
end
194194

195+
def visit_Date o
196+
register o, visit_Integer(o.gregorian)
197+
end
198+
195199
def visit_DateTime o
196-
formatted = if o.offset.zero?
197-
o.strftime("%Y-%m-%d %H:%M:%S.%9N Z".freeze)
198-
else
199-
o.strftime("%Y-%m-%d %H:%M:%S.%9N %:z".freeze)
200-
end
200+
t = o.italy
201+
formatted = format_time t, t.offset.zero?
201202
tag = '!ruby/object:DateTime'
202203
register o, @emitter.scalar(formatted, nil, tag, false, false, Nodes::Scalar::ANY)
203204
end
@@ -235,7 +236,6 @@ def visit_Integer o
235236
end
236237
alias :visit_TrueClass :visit_Integer
237238
alias :visit_FalseClass :visit_Integer
238-
alias :visit_Date :visit_Integer
239239

240240
def visit_Float o
241241
if o.nan?
@@ -482,8 +482,8 @@ def dump_exception o, msg
482482
@emitter.end_mapping
483483
end
484484

485-
def format_time time
486-
if time.utc?
485+
def format_time time, utc = time.utc?
486+
if utc
487487
time.strftime("%Y-%m-%d %H:%M:%S.%9N Z")
488488
else
489489
time.strftime("%Y-%m-%d %H:%M:%S.%9N %:z")

test/psych/test_date_time.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,26 @@ def test_datetime_timezone_offset
4444
assert_match(/12:00:00-05:00/, cycled.last.to_s)
4545
end
4646

47+
def test_julian_date
48+
d = Date.new(1582, 10, 4, Date::GREGORIAN)
49+
assert_cycle d
50+
end
51+
52+
def test_proleptic_gregorian_date
53+
d = Date.new(1582, 10, 14, Date::GREGORIAN)
54+
assert_cycle d
55+
end
56+
57+
def test_julian_datetime
58+
dt = DateTime.new(1582, 10, 4, 23, 58, 59, 0, Date::GREGORIAN)
59+
assert_cycle dt
60+
end
61+
62+
def test_proleptic_gregorian_datetime
63+
dt = DateTime.new(1582, 10, 14, 23, 58, 59, 0, Date::GREGORIAN)
64+
assert_cycle dt
65+
end
66+
4767
def test_invalid_date
4868
assert_cycle "2013-10-31T10:40:07-000000000000033"
4969
end

0 commit comments

Comments
 (0)