[Datetime-SIG] Calendar vs timespan calculations...

Alexander Belopolsky alexander.belopolsky at gmail.com
Mon Aug 3 20:29:26 CEST 2015


On Mon, Aug 3, 2015 at 11:32 AM, Chris Barker - NOAA Federal
<chris.barker at noaa.gov> wrote:
> I'm a bit confused about what the strict tzinfo object has to do with
> leap seconds.

Here is another way to explain it.  Suppose I want to know how many
seconds passed from 2011-07-15T12:00 to (and not including)
2012-07-15T12:00.   Well, simple: it's one year, which is 365 days
which is 365*24 hours, which is ...

>>> 365*24*60*60
31536000

Simple, but wrong - we forgot that 2012 is a leap year.  Try again

>>> from datetime import *
>>> SECOND = timedelta(seconds=1)
>>> (datetime(2012, 7, 15, 12) - datetime(2011, 7, 15, 12)) / SECOND
31622400.0

This is better, but what if we are in a location that stopped
observing DST in 2012?  This is what we want tzstrict to account for,
but if we just account for DST, we will still be wrong g because there
was
a leap second added at the end of June 2012.  Luckily the same
mechanism can be used to account for that.

Note that in answering this question, we did not have to know how to
call the extra day in February, the extra hour in March or April or
the extra second in June.  All we needed was a mapping from the start
and end point to some scale that is considered linear (UTC for most
practical purposes or TAI for those who care.)


More information about the Datetime-SIG mailing list