[Python-Dev] Store timestamps as decimal.Decimal objects (original) (raw)
Matt Joiner anacrolix at gmail.com
Tue Jan 31 00:50:45 CET 2012
- Previous message: [Python-Dev] Store timestamps as decimal.Decimal objects
- Next message: [Python-Dev] Store timestamps as decimal.Decimal objects
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Sounds good, but I also prefer Alexander's method. The type information is already encoded in the class object. 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. On Jan 31, 2012 10:32 AM, "Victor Stinner" <victor.stinner at haypocalc.com> wrote:
Hi,
In issues #13882 and #11457, I propose to add an argument to functions returning timestamps to choose the timestamp format. Python uses float in most cases whereas float is not enough to store a timestamp with a resolution of 1 nanosecond. I added recently time.clockgettime() to Python 3.3 which has a resolution of a nanosecond. The (first?) new timestamp format will be decimal.Decimal because it is able to store any timestamp in any resolution without loosing bits. Instead of adding a boolean argument, I would prefer to support more formats. My last patch provides the following formats: - "float": float (used by default) - "decimal": decimal.Decimal - "datetime": datetime.datetime - "timespec": (sec, nsec) tuple # I don't think that we need it, it is just another example The proposed API is: time.time(format="datetime") time.clockgettime(time.CLOCKREALTIME, format="decimal") os.stat(path, timestamp="datetime) etc. This API has an issue: importing the datetime or decimal object is implicit, I don't know if it is really an issue. (In my last patch, the import is done too late, but it can be fixed, it is not really a matter.) Alexander Belopolsky proposed to use time.time(format=datetime.datetime) instead. -- The first step would be to add an argument to functions returning timestamps. The second step is to accept these new formats (Decimal?) as input, for datetime.datetime.fromtimestamp() and os.utime() for example. (Using decimal.Decimal, we may remove os.utimens() and use the right function depending on the timestamp resolution.) -- I prefer Decimal over a dummy tuple like (sec, nsec) because you can do arithmetic on it: t2-t1, a+b, t/k, etc. It stores also the resolution of the clock: time.time() and time.clockgettime() have for example different resolution (sec, ms, us for time.time() and ns for clockgettime()). The decimal module is still implemented in Python, but there is working implementation in C which is much faster. Store timestamps as Decimal can be a motivation to integrate the C implementation :-) -- Examples with the time module: $ ./python Python 3.3.0a0 (default:52f68c95e025+, Jan 26 2012, 21:54:31) >>> import time >>> time.time() 1327611705.948446 >>> time.time('decimal') Decimal('1327611708.988419') >>> t1=time.time('decimal'); t2=time.time('decimal'); t2-t1 Decimal('0.000550') >>> t1=time.time('float'); t2=time.time('float'); t2-t1 5.9604644775390625e-06 >>> time.clockgettime(time.CLOCKMONOTONIC, 'decimal') Decimal('1211833.389740312') >>> time.clockgetres(time.CLOCKMONOTONIC, 'decimal') Decimal('1E-9') >>> time.clock() 0.12 >>> time.clock('decimal') Decimal('0.120000') Examples with os.stat: $ ./python Python 3.3.0a0 (default:2914ce82bf89+, Jan 30 2012, 23:07:24) >>> import os >>> s=os.stat("setup.py", timestamp="datetime") >>> s.stmtime - s.stctime datetime.timedelta(0) >>> print(s.statime - s.stctime) 52 days, 1:44:06.191293 >>> os.stat("setup.py", timestamp="timespec").stctime (1323458640, 702327236) >>> os.stat("setup.py", timestamp="decimal").stctime Decimal('1323458640.702327236') Victor
Python-Dev mailing list Python-Dev at python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/anacrolix%40gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20120131/fd5e5dcc/attachment.html>
- Previous message: [Python-Dev] Store timestamps as decimal.Decimal objects
- Next message: [Python-Dev] Store timestamps as decimal.Decimal objects
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]