Simberon Design Minute
 

Dealing With Time

I'm currently working on implementing a scheduler that can run jobs at specific times. For some jobs, you want them to run every 10 minutes and for other jobs you want to run them at 1:30 am each day. Others may need to run on the first day of the month or the last day of the month. When you implement something like this, you realize that dealing with time is tricky. First, if you just use Time itself instead of a timestamp, you have to worry about times going over midnight and messing up the calculations. If you use timestamps, you must capture the date and the time in one atomic step to avoid getting today's date and tomorrow's time if you cross midnight between the two fetches. Then there's the problem of Daylight Savings Time. In the fall, 1:30am occurs twice - once on Daylight Time and once on Standard time when the clock turns back at 2:00 am. In the spring, a whole hour is skipped and something scheduled at 2:30am can't actually run at 2:30am. That rule is only for North America. The European Union shifts at 1:00 UTC everywhere. Some places only shift by half an hour and others don't shift at all. Add in leap years and time zones and you get a real mess. If possible, use libraries that already take care of all that stuff. If you can't, pay special attention to the exceptional cases and test carefully.

Download