On Tue, Aug 18, 2015 at 4:25 PM, Carl Meyer <[email protected]> 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.)
_______________________________________________
Datetime-SIG mailing list
[email protected]
https://mail.python.org/mailman/listinfo/datetime-sig
The PSF Code of Conduct applies to this mailing list: 
https://www.python.org/psf/codeofconduct/

Reply via email to