While Joda Time has undoubtedly made date/calendar/timezone manipulation a happier task for the JVM developer, I'm looking for something stronger. I can pass around a DateTime all day long (no pun intended) but until I inspect its TimeZone I can't be sure where it originated from, or if it is in fact the canonical UTC.
As a result, there is nothing at compile time to stop me doing something like:
def displayAllEventsBefore(allEvents:Seq[Event], threshold:DateTime) = { // allEvents have been normalized to UTC. But there's no way of knowing this: allEvents.filter(_.isBefore(threshold)).display } // ... much later, miles away val myTime = new DateTime() // Happens to be in "Europe/Paris" displayAllEventsBefore(events, myTime)Which will work just fine most of the time, except when there's been an event in the last hour, when we won't see it. Or is it the other way around? Tricky, isn't it?
There's nothing in the type system to prevent these kinds of runtime problems. It comes down to developer diligence in naming/commenting/testing all the things - literally, every thing that uses a representation of time - to ensure correctness.
But hang on, aren't compilers really, really good at ensuring correctness?