This project is archived and is in readonly mode.
Comparison of Time attributes does not work as expected
Reported by Lailson Bandeira | August 11th, 2010 @ 03:10 PM
In SQL there is the type
time, that stores a given
time of day without being attached to any date. Since Ruby doesn't
have a class for handling only times, Active Record maps a
time column to a dummy
Time object, with
the date always set to
2000-01-01. This is useful when
we are comparing two
time attributes: since the date
is the same, the comparison considers only the time, making the
However, the abstraction leaks when we compare a
time attribute with a
Time instance from elsewhere. To make it work, the
user has to manually convert it to the epoch date or compare using
seconds_since_midnight to consider only the time
of day. And it's painful in both ways.
To solve this, I see three alternatives:
- Make the user conscious of the epoch date and provide a method
to_dummy_time, that transports a
- Mark the
Timeinstances that refers to time-only and compare them ignoring the date.
- Create a
TimeOfDaytype in Ruby to handle times properly.
Among these, I think the second makes the best compromise
between transparency and code (the third implementation requires a
lot of code to make it work with
Time safely). In fact, I
implemented it as a plugin some time ago: http://github.com/lailsonbm/time_of_day.
We're using it on a Rails 3 project with success since then. The
README shows the problem and describes how we handle it.
So, what do you think guys? It shouldn't be difficult to make a patch to Rails, I surely can do this.
Comments and changes to this ticket
- State changed from new to open
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.
Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.