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

Carl Meyer carl at oddbird.net
Tue Aug 25 22:48:25 CEST 2015


On 08/25/2015 02:42 PM, Alexander Belopolsky wrote:
> 
> On Tue, Aug 25, 2015 at 4:30 PM, Carl Meyer <carl at oddbird.net
> <mailto:carl at oddbird.net>> wrote:
> 
>     > start = datetime.combine(date, time(0)).astimezone()
>     > while True:
>     >     end = (start + timedelta(hours=0.5)).astimezone()
>     >     print(start, end)
>     >     start = end
>     >     if end.time() == time(0):
>     >         break
> 
>     On DST transition days, this code does not generate exactly 48 slots,
>     displayable to the user in a schedule that includes all hours from 0 to
>     23 labeled exactly once.
> 
>     That introduces an unacceptable level of additional display-layer
>     complexity, and remains inferior to the "strict checking" solution I
>     chose.
> 
> OK.  So you just want [datetime.combine(date, time(0)) +
> timedelta(hours=0.5*i)) for i in range(48)] but detect those that are
> either in the fold or in the gap?  Just check for dt.astimezone() !=
> dt.replace(fold=1).astimezone() instead of catching the strict exceptions.

Yes, I know :-) If you'd read my email to the end, you'd have seen that
I already know that that's the PEP 495 solution to my problem, and that
it's probably fine if wrapped up in a utility function, though the
ugliness of having to do all timezone localizations twice might have
caused me to spend another couple hours looking for a better solution,
whereas the convenience, availability, and discoverability of pytz's
`is_dst=None` option made things simple and easy for me.

I'm glad you've at least reached the point of acknowledging that valid
use cases for strict checking exist; this seems to be a new development
:-) That was really my only intent here. If you accept this, and still
think that the best API datetime can provide is "localize twice and
figure it out yourself", I'm not interested in arguing the point.

Carl

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/datetime-sig/attachments/20150825/ea63161f/attachment.sig>


More information about the Datetime-SIG mailing list