tisdag, december 18, 2007

Joda Time

I spent a few hours this weekend converting RubyTime in JRuby to use Joda Time instead of Calendar or Date. That was a very nice experience actually. I'm incredibly impressed by Joda, and overall I think it was worth adding a new dependency to JRuby for this. The API is very nice, and immutability in these classes make things so much easier.

There were a few things I got a bit annoyed at though. First, that Joda is ISO 8601 compliant is a really good thing, but I missed the functionality to tune a few things. Stuff like saying which weekday a week should start on, for the calculation of current week would be very nice. As it is right now, that functionality has to use Calendar. It might be in Joda, but I couldn't find it.

The other thing I had a problem with - and this actually made me a bit annoyed - was how Joda handles GMT and UTC. Now, it says clearly in the documentation that Joda works with the UTC concept, and that GMT is not exactly the same thing. So why is it this code passes (if assertNotEquals is assumed):
    public void testJodaStrangeNess() {
assertEquals(DateTimeZone.UTC, DateTimeZone.forID("UTC"));
assertEquals(DateTimeZone.UTC, DateTimeZone.forID("GMT"));
assertEquals(DateTimeZone.UTC, DateTimeZone.forOffsetHours(0));
assertNotEquals(DateTimeZone.UTC, DateTimeZone.forID("Etc/GMT"));
assertNotEquals(DateTimeZone.forID("GMT"), DateTimeZone.forID("Etc/GMT"));

Yeah, you're reading it right - UTC and GMT is the same time zone. +00:00 is the same as UTC too. But Etc/GMT is not the same as UTC or GMT or +00:00. Isn't that a bit strange?

4 kommentarer:

JodaStephen sa...

The reason for the 'weirdness' is that UTC is a constant we define, whereas Etc/GMT comes from the Olson tz database. You may be right that they should be the same (which would simply require a little aliasing).

Ola Bini sa...

Stephen, you actually misunderstood me completely. The thing I find FUBAR is that UTC and GMT is the same thing. I believe that GMT and Etc/GMT and +00:00 should be the same thing, and that these should NOT be the same as UTC.

JodaStephen sa...

OK, but as far as Joda-Time is concerned, you can't actually tell any difference between GMT and UTC. Also, looking at backwards compatibility, we probably can fix Etc/GMT to become UTC, but we couldn't split GMT to be separate from UTC.

Ola Bini sa...

Well, they _should_ be different. You can't reproduce all timezones correctly without it. With that said, it would make it even worse to backport Etc/GMT to be the same as GMT. In that case there would be _no_ way of getting at the functionality of the "real" GMT.