[Datetime-SIG] Calendar vs timespan calculations...
Alexander Belopolsky
alexander.belopolsky at gmail.com
Sun Aug 2 01:02:40 CEST 2015
On Sat, Aug 1, 2015 at 6:35 PM, Ethan Furman <ethan at stoneleaf.us> wrote:
> So really my involvement here is as a user of these things -- and as a user
> I would be greatly frustrated with being able to hand an
> ambiguous/non-existent date/time to a strict dt/tzinfo and not get an error
> because sooner or later the guess would be wrong and I'd be wondering what
> happened.
Luckily, tzinfo does not currently have a "to_utc()" method, so we can
implement it in tzstrict as
def to_utc(self, dt):
o0, o1 = self.utcoffset(dt), self.utcoffset(dt.replace(first=not dt.first))
if o0 == o1:
return dt + o0
if (o0 < o1) == dt.first:
raise AmbiguousLocalTimeError
else:
raise InvalidLocalTimeError
However, some programers may prefer to get more than one bit of
information whenever they query the timezone database. The problem
with an InvalidLocalTimeError is that it does not help the user to
correct it. Rather than saying "invalid time - try again", it would
be more helpful to say something like: "02:30 is invalid, would you
like to schedule your event for [ ] 01:30; [ ] 03:30; or other: ____".
This will require knowledge that 01:30 and 03:30 are valid, which you
will have after you called self.utcoffset(dt) and
self.utcoffset(dt.replace(first=not dt.first)), but the hypothetical
to_utc() function will discard that knowledge.
More information about the Datetime-SIG
mailing list