Talk About a Head Wrecker
So here's the deal ... and it's a head wrecker.
Over on KiwiWriters we're releasing Member Challenges onto the site on the 15th September. So for example anyone who has signed up to the site can create a challenge that anyone else can join. It's going to be very cool.
There will also be other general improvements such as the one where we'll automatically track your daily progress for any of the challenges you're signed up for ... and this is where it starts to get complicated.
I need to know:
- when any timezone in the world passes midnight
- the list of 'active today' challenges at each timezone that has just passed midnight
- the progress of each member within that timezone partaking in any of the active challenges
- then finally to save their progress
This is kinda weird in two respects.
The first is that timezones are weird - we already know this, but I've just figured out something else a little strange. At 11pm tonight for example, the Pacific/Pago_Pago timezone just crossed into the 3rd September but the Pacific/Tongatapu timezone just crossed into the 4th September - at exactly the same time.
So, the list of active challenges is any challenge that was open for even just 1 minute today. This is the 2nd strange thing and it isn't as easy as it sounds. The logic is a little weird. You'd think it would be about checking the start and end datetimes but you actually want to know if a particular challenge was open at any stage of today - and as you've seen above, today might mean yesterday, also remembering that you've already crossed over into tomorrow.
Let's take midnight tonight for example. Once we cross midnight then the CURRENT_DATE is 2007-09-04. This means that the list of active challenges for today (the 3rd) are those who's start_date <= '2007-09-03' AND end_date >= '2007-09-02'. It seems a little strange at first and it took me ages to figure it out. I don't consider it to be straightforward logic since there was a lot of thinking time but it definitely works.
So as an example for my test data. For Pacific/Pago_Pago my test data said that there were two challenges active yesterday. But for Pacific/Tongatapu it said there were four because it was for today. Yet all of these are processed for different days at exactly the same time.
As we speak, Pacific/Chatham has just passed midnight and is therefore offset by 45 mins from mainland New Zealand so again, something different the fact that I have to check every 15 mins!
So tonight has been a bit of a head wrecker trying to get all these things in place. As it turns out, I think I've got it sussed but wow, it's amazing how much time can disappear on what appears to be a simple problem.
This post originated on http://chilts.org/.
Email me on andychilton -at- gmail -dot- com.
