[Python-Dev] Store timestamps as decimal.Decimal objects

Victor Stinner victor.stinner at haypocalc.com
Tue Jan 31 13:08:21 CET 2012


2012/1/31 Matt Joiner <anacrolix at gmail.com>:
> Sounds good, but I also prefer Alexander's method. The type information is
> already encoded in the class object.

Ok, I posted a patch version 6 to use types instead of strings. I also
prefer types because it solves the "hidden import" issue.

> This way you don't need to maintain a
> mapping of strings to classes, and other functions/third party can join in
> the fun without needing access to the latest canonical mapping. Lastly there
> will be no confusion or contention for duplicate keys.

My patch checks isinstance(format, type), format.__module__ and
format.__name__ to do the "mapping". It is not a direct mapping
because I don't always call the same method, the implementation is
completly differenet for each type.

I don't think that we need user defined timestamp formats. My last
patch provides 5 formats:

- int
- float
- decimal.Decimal
- datetime.datetime
- datetime.timedelta

(I removed the timespec format, I consider that we don't need it.)


    >>> time.time()
    >>> time.time(format=int)
    >>> time.time(format=decimal.Decimal)
    >>> time.time(format=datetime.datetime)
    datetime.datetime(2012, 1, 31, 11, 49, 49, 409831)
    >>> print(time.time(format=datetime.timedelta))
    15370 days, 10:49:52.842116

If someone wants another format, he/she should pick up an existing
format to build his/her own format.

datetime.datetime and datetime.timedelta can be used on any function,
but datetime.datetime format gives surprising results on clocks using
an arbitrary start like time.clock() or time.wallclock(). We may raise
an error in these cases.

More information about the Python-Dev mailing list