[Python-Dev] Store timestamps as decimal.Decimal objects (original) (raw)
Victor Stinner victor.stinner at haypocalc.com
Tue Jan 31 00:31:13 CET 2012
- Previous message: [Python-Dev] PEP 3144 ipaddr module (was Re: PEP 408 -- Standard library __preview__ package)
- Next message: [Python-Dev] Store timestamps as decimal.Decimal objects
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.clock_gettime() 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.clock_gettime(time.CLOCK_REALTIME, 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.clock_gettime() have for example different resolution (sec, ms, us for time.time() and ns for clock_gettime()).
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
- Previous message: [Python-Dev] PEP 3144 ipaddr module (was Re: PEP 408 -- Standard library __preview__ package)
- Next message: [Python-Dev] Store timestamps as decimal.Decimal objects
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]