2019-09-20

Countdown and clock changes

As I mentioned, for the Brexit clock, I had to allow for the clocks changing (something many such countdown clocks do not allow).

Basically, one of the days between now and (the current) Brexit is 25 hours long. This, in itself, is not complex.

I have tinkered with the way I do this a few times, and eventually came up with this, starting with how many whole days...

  • Work out a time for UTC midnight on target date
  • Work out a time for UTC midnight today
  • Divide time difference (as seconds) by 86400 (a day) for number of days
  • If the current local time (HH:MM:SS) is on or after the target local time (HH:MM:SS) take off one day from that total.

That is the easy bit, and means that the number of days changes at the local time (HH:MM:SS) of the target, so for Brexit, changes at 11pm local time exactly each day. What normally happens is the time part then counts down from 23:59:59 until 00:00:00 at 11pm local time the next day.

However, working out the time part is one area that took me a bit of thought. In the end I make use of the fact that linux mktime() function allows tm_mday to be any value and it adjusts.

So, what I do is :-
  • Create a date for the target date/time, local time.
  • Subtract the calculated number of days from the tm_mday part of that.
  • Work out time difference from that point to current time, and convert to hours, minutes and seconds.

The effect of that is that once the day changes, the time part goes down without a discontinuity. It means that on the day that has the clock change the clock will start counting down from 24:59:59 instead of 23:59:59.

For Brexit, that means that 11pm on Saturday 26th October 2019 it will go to from 5 days 00:00:00 to 4 days 24:59:59 and count down for 25 hours until 11pm on Sunday 27th October when it goes to 3 days 23:59:59.

Of all the ways to work this out, I think this is the best. It means days are counted to the local time on each day that matches the final target local time (so for Brexit, its counts a day at 11pm local time each day), and then that the time part counts without a discontinuity down to 00:00:00 at the same local time the next day.

Yes, I put far too much thought in to this.

No comments:

Post a Comment

Comments are moderated purely to filter out obvious spam, but it means they may not show immediately.