<p>Sounds good, but I also prefer Alexander&#39;s method. The type information is already encoded in the class object. This way you don&#39;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. </p>

<div class="gmail_quote">On Jan 31, 2012 10:32 AM, &quot;Victor Stinner&quot; &lt;<a href="mailto:victor.stinner@haypocalc.com">victor.stinner@haypocalc.com</a>&gt; wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi,<br>
<br>
In issues #13882 and #11457, I propose to add an argument to functions<br>
returning timestamps to choose the timestamp format. Python uses float<br>
in most cases whereas float is not enough to store a timestamp with a<br>
resolution of 1 nanosecond. I added recently time.clock_gettime() to<br>
Python 3.3 which has a resolution of a nanosecond. The (first?) new<br>
timestamp format will be decimal.Decimal because it is able to store<br>
any timestamp in any resolution without loosing bits. Instead of<br>
adding a boolean argument, I would prefer to support more formats. My<br>
last patch provides the following formats:<br>
<br>
 - &quot;float&quot;: float (used by default)<br>
 - &quot;decimal&quot;: decimal.Decimal<br>
 - &quot;datetime&quot;: datetime.datetime<br>
 - &quot;timespec&quot;: (sec, nsec) tuple # I don&#39;t think that we need it, it<br>
is just another example<br>
<br>
The proposed API is:<br>
<br>
  time.time(format=&quot;datetime&quot;)<br>
  time.clock_gettime(time.CLOCK_REALTIME, format=&quot;decimal&quot;)<br>
  os.stat(path, timestamp=&quot;datetime)<br>
  etc.<br>
<br>
This API has an issue: importing the datetime or decimal object is<br>
implicit, I don&#39;t know if it is really an issue. (In my last patch,<br>
the import is done too late, but it can be fixed, it is not really a<br>
matter.)<br>
<br>
Alexander Belopolsky proposed to use<br>
time.time(format=datetime.datetime) instead.<br>
<br>
--<br>
<br>
The first step would be to add an argument to functions returning<br>
timestamps. The second step is to accept these new formats (Decimal?)<br>
as input, for datetime.datetime.fromtimestamp() and os.utime() for<br>
example.<br>
<br>
(Using decimal.Decimal, we may remove os.utimens() and use the right<br>
function depending on the timestamp resolution.)<br>
<br>
--<br>
<br>
I prefer Decimal over a dummy tuple like (sec, nsec) because you can<br>
do arithmetic on it: t2-t1, a+b, t/k, etc. It stores also the<br>
resolution of the clock: time.time() and time.clock_gettime() have for<br>
example different resolution (sec, ms, us for time.time() and ns for<br>
clock_gettime()).<br>
<br>
The decimal module is still implemented in Python, but there is<br>
working implementation in C which is much faster. Store timestamps as<br>
Decimal can be a motivation to integrate the C implementation :-)<br>
<br>
--<br>
<br>
Examples with the time module:<br>
<br>
$ ./python<br>
Python 3.3.0a0 (default:52f68c95e025+, Jan 26 2012, 21:54:31)<br>
&gt;&gt;&gt; import time<br>
&gt;&gt;&gt; time.time()<br>
1327611705.948446<br>
&gt;&gt;&gt; time.time(&#39;decimal&#39;)<br>
Decimal(&#39;1327611708.988419&#39;)<br>
&gt;&gt;&gt; t1=time.time(&#39;decimal&#39;); t2=time.time(&#39;decimal&#39;); t2-t1<br>
Decimal(&#39;0.000550&#39;)<br>
&gt;&gt;&gt; t1=time.time(&#39;float&#39;); t2=time.time(&#39;float&#39;); t2-t1<br>
5.9604644775390625e-06<br>
&gt;&gt;&gt; time.clock_gettime(time.CLOCK_MONOTONIC, &#39;decimal&#39;)<br>
Decimal(&#39;1211833.389740312&#39;)<br>
&gt;&gt;&gt; time.clock_getres(time.CLOCK_MONOTONIC, &#39;decimal&#39;)<br>
Decimal(&#39;1E-9&#39;)<br>
&gt;&gt;&gt; time.clock()<br>
0.12<br>
&gt;&gt;&gt; time.clock(&#39;decimal&#39;)<br>
Decimal(&#39;0.120000&#39;)<br>
<br>
Examples with os.stat:<br>
<br>
$ ./python<br>
Python 3.3.0a0 (default:2914ce82bf89+, Jan 30 2012, 23:07:24)<br>
&gt;&gt;&gt; import os<br>
&gt;&gt;&gt; s=os.stat(&quot;setup.py&quot;, timestamp=&quot;datetime&quot;)<br>
&gt;&gt;&gt; s.st_mtime - s.st_ctime<br>
datetime.timedelta(0)<br>
&gt;&gt;&gt; print(s.st_atime - s.st_ctime)<br>
52 days, 1:44:06.191293<br>
&gt;&gt;&gt; os.stat(&quot;setup.py&quot;, timestamp=&quot;timespec&quot;).st_ctime<br>
(1323458640, 702327236)<br>
&gt;&gt;&gt; os.stat(&quot;setup.py&quot;, timestamp=&quot;decimal&quot;).st_ctime<br>
Decimal(&#39;1323458640.702327236&#39;)<br>
<br>
Victor<br>
_______________________________________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org">Python-Dev@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-dev" target="_blank">http://mail.python.org/mailman/listinfo/python-dev</a><br>
Unsubscribe: <a href="http://mail.python.org/mailman/options/python-dev/anacrolix%40gmail.com" target="_blank">http://mail.python.org/mailman/options/python-dev/anacrolix%40gmail.com</a><br>
</blockquote></div>