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

Alexander Belopolsky alexander.belopolsky at gmail.com
Tue Aug 18 22:46:24 CEST 2015


On Tue, Aug 18, 2015 at 4:25 PM, Carl Meyer <carl at oddbird.net> wrote:
>
> But in my case, I knew precisely what UX I wanted to provide in case of
> ambiguous/missing times (and in the case of missing times, it did not
> involve guessing in either direction), and I wanted the underlying
> library to just clearly tell me about missing/ambiguous times so I could
> handle them in my preferred way. I wish PEP 495 provided a simpler
> (opt-in) way to do this.


Implementation of any specific behavior is 8 lines of code:

def local_to_utc(t):
    t0 = t.replace(first=True).astimezone(timezone.utc)
    t1 = t.replace(first=False).astimezone(timezone.utc)
    if t0 == t1:
        return t0
    if t0 < t1:
        return t0  # or return t1 or raise AmbiguousTimeError
    if t1 > t0:
        raise InvalidTimeError  # or return t0 or return t1

but in these 8 lines of code, one can implement 9 different behaviors, each
suitable for some particular business situation.  We cannot know in advance
which of the 9 behaviors the users will want, so instead of making a choice
for them, we give them the tools to implement any of the 9 choices and
possibly more.  (The last conditional is redundant, but left for clarity.)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/datetime-sig/attachments/20150818/4321d304/attachment.html>


More information about the Datetime-SIG mailing list