need to print seconds from the epoch including the millisecond

Ned Batchelder ned at nedbatchelder.com
Mon Dec 30 13:50:40 CET 2013


On 12/29/13 9:44 PM, matt.doolittle33 at gmail.com wrote:
> On Friday, December 27, 2013 7:25:42 PM UTC-5, Cameron Simpson wrote:
>> On 27Dec2013 07:40, matt.doolittle33 at gmail.com <matt.doolittle33 at gmail.com> wrote:
>>
>>> I am on Ubuntu 12.10.   I am still working with the 2 decimal
>>
>>> places. Sometime ago i had this issue and I forget how i solved it.
>>
>>> maybe i used datetime? thanks!
>>
>>
>>
>> Repeatedly people have asked you to show your exact code. Still nothing.
>>
>>
>>
>> Here's a clue, from a Gentoo box running kernel 3.2.1-gentoo-r2:
>>
>>
>>
>>    $ python
>>
>>    Python 2.7.2 (default, Feb  9 2012, 18:40:46)
>>
>>    [GCC 4.5.3] on linux2
>>
>>    Type "help", "copyright", "credits" or "license" for more information.
>>
>>    >>> import time; print time.time()
>>
>>    1388190100.44
>>
>>    >>> import time; time.time()
>>
>>    1388190102.795531
>>
>>    >>>
>>
>>
>>
>> Please show us _exactly_ what you're doing. I'm guessing that print
>>
>> is confusing you.
>>
>>
>>
> matt at matt-Inspiron-1525:~$ python
> Python 2.7.3 (default, Sep 26 2013, 16:38:10)
> [GCC 4.7.2] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import time; print time.time()
> 1388371148.39
>>>> import time; time.time()
> 1388371173.556624
>>>>
>
> i get the same result as you expect.  so its got to be the write statement that is truncated the decimal places right?
>

Objects in Python have two different ways to produce a string of 
themselves, known as the str() and the repr().  A float's str() includes 
two decimal points of precision, its repr() includes as many as you'd 
need to reproduce the float again.  The print statement implicitly uses 
the str(), the interactive interpreter uses the repr().

Luckily, you can decide how to format the float yourself:

     >>> import time
     >>> time.time()
     1388407706.617985
     >>> print time.time()
     1388407709.21
     >>> print "%.3f" % time.time()
     1388407716.377
     >>> print "%.4f" % time.time()
     1388407726.1001

BTW, I said something very similar in this thread 2.5 days ago: 
https://mail.python.org/pipermail/python-list/2013-December/663454.html
I get the feeling not all messages are flowing to all places.

-- 
Ned Batchelder, http://nedbatchelder.com




More information about the Python-list mailing list