2012/2/1 Nick Coghlan <ncogh...@gmail.com>:
> The secret to future-proofing such an API while only using integers
> lies in making the decimal exponent part of the conversion function
> signature:
>
>    def from_components(integer, fraction=0, exponent=-9):
>        return Decimal(integer) + Decimal(fraction) * Decimal((0,
> (1,), exponent))

The fractional part is not necessary related to a power of 10. An
earlier version of my patch used also powers of 10, but it didn't work
(loose precision) for QueryPerformanceCounter() and was more complex
than the new version. NTP timestamp uses a fraction of 2**32.
QueryPerformanceCounter() (used by time.clock() on Windows) uses the
CPU frequency.

We may need more information when adding a new timestamp formats
later. If we expose the "internal structure" used to compute any
timestamp format, we cannot change the internal structure later
without breaking (one more time) the API.

My patch uses the format (seconds: int, floatpart: int, divisor: int).
For example, I hesitate to add a field to specify the start of the
timestamp: undefined for time.wallclock(), time.clock(), and
time.clock_gettime(time.CLOCK_MONOTONIC), Epoch for other timestamps.

My patch is similar to your idea except that everything is done
internally to not have to expose internal structures, and it doesn't
touch decimal or datetime modules. It would be surprising to add a
method related to timestamp to the Decimal class.

> This strategy would have negligible performance impact

There is no such performance issue: time.time() performance is exactly
the same using my patch. Depending on the requested format, the
performance may be better or worse. But even for Decimal, I think that
the creation of Decimal is really "fast" (I should provide numbers
:-)).

Victor
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to