The Perfect Timestamp

Almost every web application deals with and displays timestamps. Searching for the perfect one can be a bit like searching for the Holy Grail. How can something so small, so seemingly innocuous, cause so much heartache; so much pain? I think the problem is that too many choices leads to suffering when it comes to software development.
A timestamp needs to be short and sweet, easy to read, and yet highly informative. There’s a lot of information that you might include, and options too numerous to mention for displaying the various bits of data. But even strftime doesn’t cover all of the bases. For example, AM and PM always come out in ALL CAPS. What’s the deal with that? [edit] Code updated to use the Date strftime method, which is much better for some odd reason. [/edit]
Well, I’m all for less choice and smart defaults, but I don’t think even the date/time helpers in Rails get it quite right. Hell, I don’t blame them – I think this has got to be one of the most perplexing and difficult problems in software development, second only to coming up with good names for stuff.
I’m sure I must have gone through at least 50 iterations, looking for the perfect solution. Now, I’m pleased to say that I think I’ve finally found it.
Sat, 28 Jul 2007, 3:15pm
If I had to pick one timestamp to take with me on a deserted island, this would be the one. It’s short and sweet, but it still packs a lot of punch.
Here’s the helper I made to accomplish this spectacular feat:
def time_stamp(time)
time.to_datetime.strftime("%a, %d %b %Y, %l:%M%P").squeeze(' ')
end
Easy as pie. But…
There’s still something missing. What about that “2 hours ago” kind of timestamp. You know, something like the distance_of_time_in_words helper? I think people like to see if some item was created “2 hours ago,” sure. But at a certain point, saying “22 days” ago just doesn’t make sense.
So, we need to find a middle ground. Ideally, a time_ago helper that automatically adjusts to display a proper timestamp after a certain amount of time…
def time_ago_or_time_stamp(from_time, to_time = Time.now, include_seconds = true, detail = false)
from_time = from_time.to_time if from_time.respond_to?(:to_time)
to_time = to_time.to_time if to_time.respond_to?(:to_time)
distance_in_minutes = (((to_time - from_time).abs)/60).round
distance_in_seconds = ((to_time - from_time).abs).round
case distance_in_minutes
when 0..1 then time = (distance_in_seconds 2880)
return time
end
That little beauty also has the ability to leave out the hours/minutes part of the timestamp, for those cases where you just don’t have space for that kind of detail. It’s also a fair bit less verbose than the built-in Rails helper, which has always rubbed me the wrong way.
All of this work with timestamps was done on behalf of El Dorado, an open-source Rails forum I’ve been hacking away at for the last few months. I’ll post more about that soon, but you can start playing with the beta version now. The relevant helpers around timestamps can be found in the application_helper.
Update: Alex over at Rails Forum posted a couple of helpful suggestions about this. Thanks!