Rails DateHelper translations
An interesting bug occured to one of our projects: today, on the 1st of March, some system tests started failing. The reason: difference in translation of the timestamp. What has been translated as:
The questionnaire was not completed yet. Proceed ... (2 months)
started to be translated as
The questionnaire was not completed yet. Proceed ... (about 2 months)
Tested objects were created with a timestamp
2.months.ago => today it means
Sat, 01 Jan 2022.
In a view this timestamp is translated with the date helper
time_ago_in_words. In the documentation I found that a threshold between
about 2 months and
2 month is 59 days.
Since February has 28 days and January 31 days , the timestamp in tests is equal to 59 days. It seems like documentation is not precise enough though:
44 days, 23 hrs, 59 mins, 30 secs <-> 59 days, 23 hrs, 59 mins, 29 secs # => about 2 months 59 days, 23 hrs, 59 mins, 30 secs <-> 1 yr minus 1 sec # => [2..12] months
>> 2.months.ago => Sat, 01 Jan 2022 10:25:48.330338000 CET +01:00 >> 31 + 28 => 59 >> time_ago_in_words(59.days.ago) => "about 2 months" >> time_ago_in_words(60.days.ago) => "2 months"
59 days is still translated as
about 2 months and everything above as
The fix is to change the timestamp in tests to not rely on this translation difference.