Friday, November 2, 2007

Date Misunderstandings

One of the advantages of Open Source software is that you can look through the code source to figure out what is happening. The concomitant problem is that you often have to look through the source to figure out what it does because the documentation is rarely good enough. The only consolation is that the source code is probably in good shape because you can browse it. Even so, every so often you find a real doozy.

An important Open Source Java component is the Quartz Enterprise Job Scheduler. It is the only Java component that does real job scheduling, and it is both well written and well maintained. I was looking through some of their code the other day to check that it would do what I wanted it to do, when I found this function in the TriggerUtils class:

public static Date translateTime(Date date, TimeZone src, TimeZone dest)

The problem with this function is that the value in a Java Date object does not change when you go from one time zone to another as I explained in the previous post. Yet this function not only seems to suggest otherwise, it actually changes the the value in the Date object based on the time zone offsets, so it is obviously wrong. Fortunately the function is not actually used in any Quartz code, however its very presence suggests that the people who wrote the Quartz code do not fully understand the elements that they are working with.

It may be that the translateTime function was written before Java had the Calendar class, when Date had to do a lot more heavy lifting that it was designed for, but that time has passed and most of the methods in Date have been deprecated. This method should be deprecated as well.

No comments: