On Tue, Sep 22, 2015 at 12:01 AM, Nick Coghlan <ncogh...@gmail.com> wrote:
> It turns out there's one aspect of the accepted proposal that I > *think* I understand, but want to confirm: the datetime -> POSIX > timestamp -> datetime roundtrip for missing times. > > If I'm reading the PEP correctly, the defined invariant for local > times that actually exist is: > > dt == datetime.fromtimestamp(dt.timestamp()) > Yup, except for floating point errors! Those have been fixed, finally: http://bugs.python.org/issue23517 is now closed (the fix didn't make 3.5.0; it will be in 3.5.1 though). > No confusion there for the unambiguous times, or for times in a fold. > In the latter case, the timestamps produced match the points where the > UTC times match the local times in the "In the Fold" UTC/local > diagram. > And this is where the fold flag is essential for the roundtripping. > The subtle part is the handling of the "timestamp()" method for the > "missing" times where the given time doesn't actually correspond to a > valid time in the applicable timezone (local time for a naive datetime > object). > > Based on the UTC/local diagram from the "Mind the Gap" section, am I > correct in thinking that the modified invariant that also covers times > in a gap is: > > dt == > datetime.fromtimestamp(dt.astimezone(utc).astimezone(dt.tzinfo).timestamp()) > > That is, for local times that exist, the invariant "dt == > dt.astimezone(utc).astimezone(dt.tzinfo)" holds, but for times that > don't exist, "dt.astimezone(utc).astimezone(dt.tzinfo)" will normalise > them to be a time that actually exists in the original time zone, and > that normalisation also effectively happens when calling > "dt.timestamp()". > That can't be right -- There is no way any fromtimestamp() call can return a time in the gap. I think about the only useful invariant here is dt.timestamp() == dt.astimezone(utc).timestamp() == dt.astimezone(<any other tz>).timestamp() > Regards, > Nick. > > P.S. Thanks to whoever drew the diagrams for the In the Fold/Mind the > Gap sections - I found them incredibly helpful in understanding the > change! > You're welcome. It was a collaboration by myself and Alexander. I drew the first version by hand because I couldn't follow the math without a visual aid. :-) -- --Guido van Rossum (python.org/~guido)
_______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com