In many countries, the clocks go forwards and backwards at certain dates during the year, but what does this actually do ? This is the question that I found myself considering as the clocks were about to "fall back" in time and I was trying to figure out what needed to change in my code to permit this functionality.
For example, does the RTC chip in the computer move forwards automatically at that time and if it does, will it do that in hardware on its own, or do we have to code that up ?
Similarly, if its done in Linux on the Raspberry Pi, then I won't need to do anything in the Arduino code as that would be two changes on the RTC, rather than the expected one.
Then again, how does it know not to do the same thing more than once - for example when the clocks go back and it passes the same time more than once ?
There's also the larger puzzle of what happens if you aren't on GMT for your normal time zone and whilst were onto confusion, why is it called UTC, since Greenwich is the home of time, so why are there two terms ?
Then there's the logistical points, such as how do you actually calculate these dates that vary in each country and may be updated by governments when they feel like it. Do we have to resort to looking at a calendar or doing a Google search or is there some logic behind this ?
As with many of the puzzles this project has thrown up, this all starts from a simple requirement like "I need the clock to be accurate so that the opening time and closing time is correct and the log data shows correctly"
So what do I need to do to ensure that my times are right when date and time is used ?
Following a bit of research, I come across the Olson tables, which are so named as David Olson first looked at this problem back in 1970 when Unix was first created. Currently these tables are more commonly known as the timezone database, or tz database. This is a list all the events for all time zones in each country through time.
The step times are commonly applied at 2am, since 1 hour's offset is typical, therefore the time only moves within the same day, not to a different day and back again, which would further confuse things.
As to when the dates are, the tables express the entries in multiple ways, so some countries have absolute dates, whereas others have expressions like "the last Sunday in March", which is then fairly trivial to work out assuming you know the current day in the first place, or can calculate it from scratch.
It turns out that the time in the real-time-clock (RTC) are always kept the same and are generally set to Coordinated Universal Time (UTC), which by the way is different to GMT since GMT is a time zone (zone 0), whereas UTC is a time standard. Consider that Greenwich gets British Summer time too and is out from its own reference at that point in time, hence the need for the two terms.
All that happens when a time is calculated is:
- The time is obtained from the clock
- the time is adjusted by the timezone, which is constant and is taken from 1/24'th of a circle and adjusted a bit to make them more logical to whole countries where practical.
- The time is adjusted by the local timezone entry - the British Summer Time Offset for example.
This also means that its very simple for more than one person in different parts of the globe to refer to time in a format that is locally understandable, yet all tracked from a single reference clock or if you have a system with several controllers and one reference clock, then each can work from the same reference without worrying if another has made a change to it at a critical time. A good example of this is a date stamp on a file on Windows for example. This means no changes are necessary to existing dates, such as file creation dates during time zone changes.
The Linux manual on the clock is well worth a read, its available here
My question to you - did you notice that its called Coordinated Universal Time, yet it uses the abbreviation of UTC rather than CUT ? This is nearly as bad as the International Organisation for Standardisation, who's TLA is ISO. Surely the TLA should match the longer name -- or did I miss something again, other than its a french organisation and they like to play with their word order ;-)