[Datetime-SIG] PEP-495 - Strict Invalid Time Checking

Alexander Belopolsky alexander.belopolsky at gmail.com
Tue Aug 25 22:10:08 CEST 2015

On Tue, Aug 25, 2015 at 3:36 PM, Carl Meyer <carl at oddbird.net> wrote:

> Given the unlikelihood of someone trying to schedule an appointment at
> 2am, I didn't care much about the specific choice of user experience for
> the DST gaps and folds; my overriding concerns were to a) not crash the
> application when displaying a DST-transition day, and b) minimize the
> harm done to my codebase (in fragility or added complexity) for the sake
> of handling this rare edge case.

Here is my recommendation for your case.  Assuming that taking user entry
is equivalent to parsing date and time string in some format and scheduling
is equivalent to writing a seconds since epoch timestamp into some
database, do the following post-PEP 495:

dt = datetime.strptime(input, format)
s = dt.timestamp()
# optionally
sdt = datetime.fromtimestamp(s)
if dt != sdt:
    warn("You specified an invalid time %s, we scheduled your appointment
for %s", dt, sdt)

Instead of a warning, your program may highlight the changed hour in read.

This program will have no means of scheduling in the "fold=1" hour and will
correct you if you enter time that falls in a gap.  The PEP 495 design is
motivated by precisely this type of application.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/datetime-sig/attachments/20150825/b0f773e3/attachment.html>

More information about the Datetime-SIG mailing list