Michael Buffington

I Heap Scorn Upon Ruby's Time Class

Tuesday, October 10 2006

So this is a pure out and out rant against Ruby, and it’s short, and when it comes down to it it’s the kind of rant that gets hard core developers to say “so just do it the right way yourself”. To them, I say “do it the right way yourself” preemptively and launch into my rant.

The Ruby Time class sucks. If I do Time.now, I get back a time object tied to my current time zone, which it determines by looking at what time zone my computer is set to. Not that big of a deal.

>> Time.now
=> Wed Oct 11 00:01:43 PDT 2006

The problem is, I can’t adjust that time zone at all, not without some pretty serious hacks, and that’s stupid. I can’t even create a UTC time (essentially Greenwich Mean Time) and say “Hey, this time here, let’s convert it to Mountain Standard Time”, or even GMT-7 for that matter. Once it’s UTC, always UTC. Once it’s PDT, always PDT.

Of course there are hacks (and truthfully, I’ve already come up with a not so elegant solution which I won’t yet share [can’t effectively rant and foam at the mouth with a solution in hand]) but Ruby should just make this easy. I should be able to do:

>>t = Time.now
=> Wed Oct 11 00:01:43 PDT 2006
>>t.convert(“GMT-6”)
=> Wed Oct 11 01:01:43 MDT 2006

But I can’t. I can’t even extend the Time class by making a Time#convert that will return the time as MDT without converting the original time to a string, individually poking at it’s parts, messing with system internals that may break other stuff, and hope that it’ll go quickly.

Ruby, why do you hurt me so?

Also, I know about TZinfo and it’s giganté sized pile of international time zones. It’s a good library, but not very fast, and converting one time to another time when you don’t know exactly what zone you’re converting too or from is nigh impossible.

Perhaps when Ruby is back in my good graces (tomorrow morning after a vigorous night of sleeping) I’ll write up my solution.