4 ms·
We have a rails 6 app, and there's a test that essentially expects time_ago_in_words(1.year.from_now) to return "about 1 year" (as part of a user facing message
by EnigmaticLion 3y ago
We have a rails 6 app, and there's a test that essentially expects time_ago_in_words(1.year.from_now) to return "about 1 year" (as part of a user facing message). The test failed. I thought it's a flaky test but i was able to reproduce it locally. Turns out executing that code on a leap day returns "almost 1 year" instead.
Can test it in rails console if you are interested:
irb(main):001:0> include ActionView::Helpers::DateHelper
=> Object
irb(main):002:0> distance_of_time_in_words(Date.new(2025, 2, 28), Date.new(2024, 2, 29))
=> "almost 1 year"
irb(main):003:0> distance_of_time_in_words(Date.new(2025, 3, 1), Date.new(2024, 2, 29))
=> "about 1 year"
irb(main):004:0> distance_of_time_in_words(Date.new(2025, 2, 28), Date.new(2024, 2, 28))
=> "about 1 year"
- DinaCoder99 3y ago> distance_of_time_in_words(Date.new(2025, 2, 28), Date.new(2024, 2, 28)) Curiously, this is also not quite a year, even though the days and months are the same.
- mr_toad 3y agoIt’s probably comparing the number of days between the dates with the number of days in a year, which would usually work.
- EnigmaticLion 3y agoSource is a lot more complicated than i thought it will be: https://github.com/rails/rails/blob/v6.1.7.7/actionview/lib/action_view/helpers/date_helper.rb#L95 https://github.com/rails/rails/blob/v6.1.7.7/actionview/lib/...