This project is archived and is in readonly mode.

#5358 ✓stale
Lailson Bandeira

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 abstraction perfect.
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 only seconds_since_midnight to consider only the time of day. And it's painful in both ways.
To solve this, I see three alternatives:

  1. Make the user conscious of the epoch date and provide a method to_dummy_time, that transports a Time instance to 2000-01-01.
  2. Mark the Time instances that refers to time-only and compare them ignoring the date.
  3. Create a TimeOfDay type 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 Date, DateTime and 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

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:33 PM

    • 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.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:33 PM

    • State changed from “open” to “stale”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages