[Datetime-SIG] PEP 495 (Local Time Disambiguation) is ready for pronouncement

Alexander Belopolsky alexander.belopolsky at gmail.com
Mon Aug 17 19:15:08 CEST 2015


On Mon, Aug 17, 2015 at 12:44 PM, Chris Barker <chris.barker at noaa.gov> wrote:
> I _think_ I recall form this discussion that this was done, rather than an
> error, because it was decided that such calls should never raise an
> exception (also, the replace() functionality makes it all too likely). So
> it's probably best to do it in the proposed way, but the PEP should make it
> clear that a prime motivation is to avoid Exceptions in such code.

There are many reasons why the proposed behavior is a good one, but
raising an exception from .timestamp() is not really a contender.  We
cannot make currently valid programs crash and there are many ways a
missing time can be produced by a valid program even without an
explicit .replace() call.  For example, adding timedelta(1) to a valid
datetime can produce a missing datetime.  It is also common in some
algorithms to reattach tzinfo without checking the other datetime
components.  For example, stdlib's own datetime.tzinfo.fromutc() does
that.

I am going to add an explanation of how to implement code that rejects
missing values to the PEP and this will probably address your concern.

(The code is just if dt.replace(first=True).timestamp() >
dt.replace(first=False).timestamp(): raise MissingTimeError.)


More information about the Datetime-SIG mailing list