So I was finishing up the time ranges for charging reminders. I wanted to share a little bit about how complex it is to make things simple, because I constantly run into this at work and on my side projects, and I'm always fascinated by it!
Let's take charing reminders. We can describe the requirement in a single sentence: "When I forget to plug my car in, email me a reminder".
Easy, right?
First we need to know where the car is supposed to be plugged in so we don't remind you every time you park at the grocery store. So we need a list of locations from the user. Simple enough.
Now whenever we park, we do a series of checks:
1) Is the car parked.
2) Is it within 30 meters of one of the charging locations stored for this user.
3) Schedule a task for five minutes from now that:
3a) Checks if the car is still parked
3b) Checks if it's still within 30 meters of the location
3c) Checks if we're within the time range that we want a reminder
3d) Checks if it's plugged in
3e) Sends the email reminder
Simple enough, right? Let's zoom in on 3c.
If you only want to get reminders between 6pm and 10pm, we let you specify that. The hard part about that is timezones. The server runs on UTC time (i.e. London). If you're in California, 6pm for you is 1am for the server (GMT -7). And remember, it's the server that's emailing you.
One might think that you can just do the math and tell the server that you really want reminders when the server get to 1am. The problem is that next weekend, California will be 8 hours behind GMT because daylight savings time is ending. Ugh.
Add to that the complexity that some timezones only move up 30 minutes during daylight savings time. Some are 15 minutes ahead. Some places, like Arizona, don't have daylight savings time AT ALL!
So to make this "simple" check work, we actually need to send the local time AND the name of the timezone to the server. It's not enough to send the timezone offset in minutes - we actually need to know the NAME of the timezone. Then on the server we can calculate (every single time) what local time you wanted to be alerted, look at the timezone we have stored, and do the math.
There are some libraries that make this a little bit easier, but nothing that works together very well between a web browser (or all six web browsers that are popular) and a server.
Timezones are a programmer's worst nightmare!