[rspec-users] Date comparisons

Dan North tastapod at gmail.com
Mon May 5 05:28:34 EDT 2008

The (pretty much universal) problem with dates and times is that people use
"date" and "time" to mean different things. There's a java library called
joda that provides a really clean vocabulary around this.

An *instant* is a point in time. You shouldn't be able to ask for two
instants and get the same answer however close together you ask. (There's
probably something philosophical in there somewhere.)

A *datetime* is a type of instant with millisecond precision that can tell
you the time and date it represents, based on a timezone. The Ruby Time
class represents an instant but renders itself as a datetime, hence the
confusion (it has microsecond precision that you only get to see if you know
to ask).

A *local date* is a day when something happens, say 4th May 2008. My
understanding of 4th May in the UK is bounded by different start and end
instants than, say, David's in the US, because of timezones, but we both
know what we "mean" by 4th May 2008.

A *duration* is a length of time in milliseconds.

Given these atoms you can have fairly sensible conversations about times and
dates. For instance, in the current example I might do this:

Instant = Time               # make it clear what I'm using it for
Instant.new == Instant.new   # false, but now I know why


ps. and as Aslak says, make sure you control "time" in your examples -
either by stubbing Time or by injecting your own Clock abstraction.

2008/5/5 Jarkko Laine <jarkko at jlaine.net>:

> On 5.5.2008, at 11.02, Pat Maddox wrote:
>  Yes, that was my first idea as well.  The Time class is a little
> > fucked up in that
> >
> > < a, b = Time.now, Time.now
> > < a == b     #=> false
> >
> That's definitely a gotcha but I wouldn't necessarily say it's fucked up.
> It's just that Time#now returns the current (exact) point of time, so
> running it successively on the same machine will by definition return
> different values (by a tiny margin but still).
> The fact that the textual representation of the two look exactly the same
> certainly makes it confusing the first time get bitten by it, though :-)
> Cheers,
> //jarkko
> --
> Jarkko Laine
> http://jlaine.net
> http://dotherightthing.com
> http://www.railsecommerce.com
> http://odesign.fi
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20080505/acc33871/attachment.html>

More information about the rspec-users mailing list