Monday, January 3, 2011

Parsing a time that doesn't exist

I've been working on an integration project recently, using TIBCO BusinessWorks, and encountered an amusing bug. The code was parsing a bunch of date/time strings, and would works correctly except for a very small number of cases where it would fail with some 'Unparseable date' error. It would work for '26/03/2010 03:20' or '26/03/2010 01:20' for example, but fail for '26/03/2010 02:20'.

After some trial and error, head-scratching and Googling the problem was found - the system function used was implemented internally with java.text.SimpleDateFormat, and since the time string didn't specify the timezone the code assumed some timezone (based on locale I guess) that took DST (Daylight Saving Time) into consideration. So in this timezone, when at 26/03/2010 02:00 the clock advanced to 26/03/2010 03:00, the entire hour in between simply didn't exists and this timestamp couldn't be parsed. The solution was to specify a timezone which ignores DST, such as GMT, and in this particular case was done by appending " GMT+02:00" to the string. 

So much for the time continuity...