Rails does weird rounding in comparisons between Date and ActiveSupport::TimeWithZone classes.

by Adam Dawkins

It’s probably because I’m (in GMT) within the boundary of the days in the two timezones, but:

monday = Date.new(2023, 9, 25)
# => Mon, 25 Sep 2023

ten_days_from_now = 10.days.from_now
# => Sun, 24 Sep 2023 20:28:02.217735000 EDT -04:00

monday.class
# => Date

ten_days_from_now.class
# => ActiveSupport::TimeWithZone

monday <= ten_days_from_now
# => true

This is incorrect, Monday is not less than or equal to Sunday.

But, if we make 10.days.from_now also a Date:

monday <= ten_days_from_now.to_date
# => false

We get the correct answer, where that is:

ten_days_from_now.to_date
# => Sun, 24 Sep 2023

So, Monday is before or equal to Sunday at 20:28, but after “Sunday”.

Thanks, Rails.

[dynamically types languages are a curse, sometimes]