Can't load time with year greater than or equal to 10000
Steps to reproduce
require "psych"
require "time"
require "minitest/autorun"
class YearTenThousandTest < Minitest::Test
def test_year_ten_thousand
time = Time.parse("10000-01-01 00:00:00")
yaml_string = Psych.dump(time)
yaml_time = Psych.load(yaml_string)
assert_equal yaml_time, time
end
end
Expected behavior
Expected Psych.load to return a Time object, like it does for years <= 9999.
Actual behavior
It returns a string.
System configuration
Ruby version: 2.6.1
Originally reported as https://github.com/rails/rails/issues/35783
This has not been answered in a while now. I've been digging in for something else and found some information related to this issues.
https://github.com/ruby/psych/blob/df242e764492b602ced499e367ccf339cdb36699/lib/psych/scalar_scanner.rb#L8-L9
https://yaml.org/type/timestamp.html
It looks like Psych is respecting the spec by assuming a date year will only have a 4 digits year.
Now that has been said, the inability to round trip the operations of dump and load looks like a common issue in this repo. I am uncertain what is the stance of Psych regarding it and how it plays with your specific issue.
Should Psych have been able to round-trip the operation or should it have raised an error in the first place being explicit that the date format is not compatible with the spec?