Krista's Coding Corner

23.09.2012

Time goes by and so do the dates...

You might have noticed that I wrote dates in order "day, month, year". Like this day is written 23.09.2012. There are many fights over what is the correct way to present dates, and to me this is the most logical. It is also called Gregorian little-endian (don’t ask me why) and it is the most common way to present dates in the world.

If we have decided in which order we like to present dates the next question is what to use as a separator, do we write the whole year or only two last digits, are we going to write months with numbers or letters...

What my point is, that there are many different ways of presenting this and it causes a lot of problems. How can you know what date person means if he writes 10/12/11? This is a problem in a real world but also in coding. If you figure out how to create universal date parser, great! World really would need that. :)

With coding the best way to save dates is year, month, day like ISO 8601-standard suggests. Why? Because then it is easy to put them into order: 20120923 is bigger and later than 20120922, right?

Computers store dates internally by using Unix timestamp. It is not only for Unix operating system, all the other ones use it too. Unix time starts from 1.1.1970 00:00:00 and at that time point time is zero. This may sound weird and unfunctional but it isn’t. Unix timestamp doesn’t give a damn about how many days there are in a month because it works only with seconds. So, every moment after the 1.1.1970 has its own amount of seconds. No days or anything. With this every moment in time can be represented without any complex data structures, just by using a floating number.

Also remember that you can save time in some format (like Unix time), you can always print it out in different format. That only needs some code because it is just the appearance.

Time zones are the next troublemakers. In a global world we do have the question that how to take into count the fact that people live in various different places and time zones. Zulu-time could be the answer here. Its clock is always in UTC-time and there are no GMT+8hours there. These timestamps are easy to parse because you don’t have to first calculate what it would be in Greenwich Mean Time.

Oh, and these time thingies start to go berserk when we have the daylight savings and changing into daylight saving time at different times in different places, half-time zones and so on... Let’s just say that we really have created a mess here!

blog comments powered by Disqus