Each Answer to this Q is separated by one/two green lines.
While reviewing my past answers, I noticed I’d proposed code such as this:
import time def dates_between(start, end): # muck around between the 9k+ time representation systems in Python # now start and end are seconds since epoch # return [start, start + 86400, start + 86400*2, ...] return range(start, end + 1, 86400)
When rereading this piece of code, I couldn’t help but feel the ghastly touch of Tony the Pony on my spine, gently murmuring “leap seconds” to my ears and other such terrible, terrible things.
When does the “a day is 86,400 seconds long” assumption break, for epoch definitions of ‘second’, if ever? (I assume functions such as Python’s
time.mktime already return DST-adjusted values, so the above snippet should also work on DST switching days… I hope?)
Whenever doing calendrical calculations, it is almost always better to use whatever API the platform provides, such as Python’s datetime and calendar modules, or a mature high-quality library, than it is to write “simpler” code yourself. Date and calendar APIs are ugly and complicated, but that’s because real-world calendars have a lot of weird behavior.
For example, if it is “10:00:00 AM” right now, then the number of seconds to “10:00:00 AM tomorrow” could be a few different things, depending on what timezone(s) you are using, whether DST is starting or ending tonight, and so on.
Any time the constant 86400 appears in your code, there is a good chance you’re doing something that’s not quite right.
And things get even more complicated when you need to determine the number of seconds in a week, a month, a year, a quarter, and so on. Learn to use those calendar libraries.
According to Wikipedia,
UTC days are almost always 86 400 s long, but due to “leap seconds”
are occasionally 86 401 s and could be 86 399 s long (though the
latter option has never been used as of December 2010); this keeps the
days synchronized with the rotation of the Earth (or Universal Time).
I expect that a double leap second could in fact make the day 86402s long, if that were to ever be used.
EDIT again: second guessed myself due to confusing python documentation.
time.mktime always returns UTC epoch seconds. There done. : )
Number of seconds in a day depends on time system that you use e.g., in POSIX, a day is exactly 86400 seconds by definition:
As represented in seconds since the Epoch, each and every day shall be
accounted for by exactly 86400 seconds.
In UTC, there could be a leap second included i.e., a day can be 86401 SI seconds (and theoretically 86399 SI seconds). As of Jun 30 2015, it has happened 26 times.
If we measure days by apparent motion of the Sun then the length of a (solar) day varies through the year by ~16 minutes from the mean.
In turn it is different from UT1 that is also based on rotation of the Earth (mean solar time). An apparent solar day can be 20 seconds shorter or 30 seconds longer than a mean solar day. UTC is kept within 0.9 seconds of UT1 by the introduction of occasional intercalary leap seconds.
If you define a day by local clock then it may be very chaotic due to bizarre political timezone changes. It is not correct to assume that a day may change only by an hour due to DST.
In all time zones that “support” daylight savings time, you’ll get two days a year that don’t have 24h. They’ll have 25h or 23h respectively. And don’t even think of hardcoding those dates. They change every year, and between time zones.
Oh, and here’s a list of 34 other reasons that you hadn’t thought about, and why you shouldn’t do what you’re doing.