dimanche, octobre 31, 2010

Bye bye summer time...

So we switched (in Europe) from summer time to winter time. At 3am, it's 2 am again. So what ?

Well, I was running some tests on my computer before crashing, and I was quite surprised to get an error in a part I didn't modified today and which was running fine this afternoon. What was wrong ?

We use some class to generate CSN (Change Sequence Number), and obviously we have some tests for this class. One of them failed for one hour...

Here is the test :

public class CsnTest
{
private SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmss.123456'Z'" );

@Test
public void testCSN()
{
long ts = System.currentTimeMillis();

Csn csn = new Csn( sdf.format( new Date( ts ) ) + "#123456#abc#654321" );

assertEquals( ts/1000, csn.getTimestamp()/1000 ); <<---- This assert fails.

Why did I get an error ? Because the way we create the CSN is simply wrong : we don't take into account the fact that the computer is not necessarily always using the same time zone, and that some operation assumes that it got a GMT based time, when other uses the Locale.

Be extremely cautious when dealing with dates and time zones :
you may get a very bad surprise in production, instead of experimenting those errors by chance, just because you are running tests at 2:30 am a Saturday before going to bed !