Sunday, July 10, 2011

Dec 31, 1969 Glitch!

Smart phone users might have noticed with variety of applications defaulting the date to Dec 31,1969 with current time quite often. I even noticed this on Facebook update date time.

This might not happen quite often on well tested applications but lot on sparsely tested mobile applications.

If you ever wonder why this may happen, we all know on computer, the system time counted from Jan 1, 1970.

Most of the programming languages have a interface to setup the time with number of milliseconds away from this start date [Jan 1, 1970]. if its a positive number it will be greater than the Jan 1,1970 if its a negative number it will be before that date. If its 0 [or more appropriately, less than 1 day = 24*60*60*1000] it falls to Dec 31,1969.

The number datatype mostly defaults to 0 on most of these programming languages. [Example, Java requires number of milliseconds to be setup with long data type which defaults to 0L].

So when anyone uses this mechanism to update the date and any conditions fails to set the date time, it turns out to be default value 0L and thus the underlying system sets as Dec 31,1969.

How to avoid it?

  • More Testing
  • Understand the default values of data types on your programming languages and its impact. 
  • Efficient Error handling 
  • Finally, if you are using this way to set the current time, handle it properly. To set the current time, you cannot set 0L, you should either calculate the time or use other sophisticated API to set the current date time

No comments: