Note that this can go wrong at DST changes, when a day is shorter or longer than 24 hours. If you want it to work on those days to, either use gmtime instead of localtime, run the script during daytime (or at least, not between 23:00 and 1:00), or use a module (such as
DateTime).