Topic: The Perfect Timestamp

Hi all,

I just wrote a little piece where I provide some Rails helpers that construct what I believe to be the Holy Grail of timestamps smile

http://www.almosteffortless.com/2007/07 … timestamp/

Perhaps you'll find it informative and useful. Let me know if you have any feedback!

Re: The Perfect Timestamp

That's definetely the most interesting article on timestamps I've read. I've never heard anyone choosing which timestamp they'd take on a desert island with them wink.

Just a couple of suggestions. First, a lot the magic you're doing with strftime can be done more easily if you use DateTime's strftime (which is for some reason a lot more fully featured):

time.to_datetime.strftime "%a, %d %b %Y, %l:%M%P"

Also, it seems odd to reinvent the wheel and emulate the behaviour of distance_of_time_in_words, why not just call it directly (it would mean much shorter code):
def time_ago_or_time_stamp(from_time, to_time = Time.now, include_seconds = true, detail = false)
  [from_time, to_time].each {|t| t = t.to_time if t.respond_to?(:to_time)}
  if (((to_time - from_time).abs)/60).round > 2880 && detail
    return timestamp(from_time)
  else
    return distance_of_time_in_words(from_time, to_time, include_seconds)
  end
end

Alex

Last edited by alexpt (2007-07-29 12:30:07)

Re: The Perfect Timestamp

Oooh. I'm going to switch over to that to_datetime - awesome. I had no idea there was a difference. How very odd.

Also, the distance_of_time_in_words helper wasn't quite the way I wanted it to be, so I decided to use that one instead. Honestly, I can't remember why I didn't like it now, though. I might give that a try as well.

Thanks for your response - very helpful!

[edit]

Alex, the strftime example you posted gives me a weird space when there's only 1 digit in the hour:

Sun, 29 Jul 2007, 11:52am
Sun, 29 Jul 2007,  7:52am

Do you have any idea why that is?

Last edited by trevorturk (2007-07-29 13:03:45)

Re: The Perfect Timestamp

You're right, that is funny. I don't know why that would be. You can always add:

.squeeze(" ")

to get rid of the problem though.

Alex

Re: The Perfect Timestamp

I like it!

* goes to update article *

[edit]

Oh, yeah - I remember why I didn't like the distance_of_time_in_words (http://api.rubyonrails.org/classes/Acti … ml#M000574) helper. It returns pretty long strings in some cases, like "about 1 hour" or "less than a minute," which rubbed me the wrong way for some reason. I prefer a specific time, like "15 seconds" - but that's just me.

I've linked to this thread for other people who happen to come across my article, because I'm sure somebody will find your alternative using the distance_of_time_in_words alternative really useful. Thanks again for the help!

Last edited by trevorturk (2007-07-29 13:18:04)

Re: The Perfect Timestamp

5 freaking stars

I love it!

Re: The Perfect Timestamp

You could update:

'1 day ago' > 'yesterday'
'1 hour ago' > 'an hour ago'
'1 minute ago' > 'a minute ago'

wink

Re: The Perfect Timestamp

AdamC, I think I might just do that!

Re: The Perfect Timestamp

Hi all,

I took this and expanded on it a little bit by teaching it to understand timestamps in the future, and in the past.

So you can get back timestamps like:

One day ago

or

One day from now

I took out the TimeZone ability since I am waiting for the official new Time zone code to move from edge rails to released.  I might add it back in then, or please feel free to add it and send me a copy!

And I simplified it so that you only have to pass in your timestamp, and then there is an optional options hash where you can pass in a new base time to measure from (although I am not clear on a use case for this since measuring between two arbitrary dates, and then returning something like 'one day ago' doesn't really make sense.  No?

Current options are:

:base_time
:show_time (whether to display the time in the result or not)

Oh, I also added a full set of Rspec tests for future and past times.

It can be all be found here for the grand total price of free:

http://pastie.caboo.se/170083

Cheers,

Glenn