need to print seconds from the epoch including the millisecond

N

Ned Batchelder

Repeatedly people have asked you to show your exact code. Still nothing.

Is something wrong with the connectivity of this list? Matt posted his
code about six hours before your message.
 
S

Steven D'Aprano

Is something wrong with the connectivity of this list? Matt posted his
code about six hours before your message.

Methinks too many people have been hitting the Christmas eggnog a little
harder than is wise...

:)
 
M

matt.doolittle33

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.
1388190100.44

1388190102.795531



Please show us _exactly_ what you're doing. I'm guessing that print

is confusing you.
matt@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.
i get the same result as you expect. so its got to be the write statement that is truncated the decimal places right?
 
M

matt.doolittle33

i cant run it that way. i tried using the python prompt in terminal but got nothing. but here is all the code relevant to this issue:
#all the imports
import sys
import posixpath
import time
from time import strftime
from datetime import datetime
import os
import wx
import cPickle as pickle
import gnuradio.gr.gr_threading as _threading


#the function that writes the time values
def update(self, field_values):

now = datetime.now()

#logger ---------------
# new line to write on

# write date, time, and seconds from the epoch



# list to store dictionary keys in tis order
keys = ["duid", "nac", "tgid", "source", "algid", "kid"]
# loop through the keys in the right order
for k in keys:
# get the value of the current key
f = field_values.get(k, None)
# if data unit has value...
# output the value with trailing tab

# if data unit doesnt have this value print a tab


#end logger ----------------

#if the field 'duid' == 'hdu', then clear fields
if field_values['duid'] == 'hdu':
self.clear()

elif field_values['duid'] == 'ldu1':
self.clear()

elif field_values['duid'] == 'ldu2':
self.clear()

#elif field_values['duid'] == 'tdu':
# self.clear()
#loop through all TextCtrl fields storing the key/value pairs in k, v
for k,v in self.fields.items():
# get the dict value for this TextCtrl
f = field_values.get(k, None)
# if the value is empty then set the new value
if f:


#sample output in a .txt file:



Instead of:



"%s" % time.time()



try:



"%.6f" % time.time()



%.6f is a formatting code meaning, floating-point number, 6 decimal places.

thanks a bunch. the "%.6f" was the cure. can you please point me to the doc for formatting time? Thanks!
 
N

Ned Batchelder

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@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.
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:
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.
 
M

matt.doolittle33

Oops, and now Matt's reply to that message has just arrived! Sorry for

the noise.

the formatting:
self.logfile.write('%.6f\t'%(time.time()))

fixed it. thank you very much.
 
M

Mark Lawrence

(e-mail address removed) 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!



That's strange. Linux should give you time to the microsecond, or

something in that range.



Please post the *exact* code you're running. The code you posted

earlier is obviously only a fragment of some larger program, so we can

only guess what's happening. Assuming your program is in a file called

"prog.py", run the following commands and copy-paste the output:


i cant run it that way. i tried using the python prompt in terminal but got nothing. but here is all the code relevant to this issue:
#all the imports
import sys
import posixpath
import time
from time import strftime
from datetime import datetime
import os
import wx
import cPickle as pickle
import gnuradio.gr.gr_threading as _threading


#the function that writes the time values
def update(self, field_values):

now = datetime.now()

#logger ---------------
# new line to write on

# write date, time, and seconds from the epoch



# list to store dictionary keys in tis order
keys = ["duid", "nac", "tgid", "source", "algid", "kid"]
# loop through the keys in the right order
for k in keys:
# get the value of the current key
f = field_values.get(k, None)
# if data unit has value...
# output the value with trailing tab

# if data unit doesnt have this value print a tab


#end logger ----------------

#if the field 'duid' == 'hdu', then clear fields
if field_values['duid'] == 'hdu':
self.clear()

elif field_values['duid'] == 'ldu1':
self.clear()

elif field_values['duid'] == 'ldu2':
self.clear()

#elif field_values['duid'] == 'tdu':
# self.clear()
#loop through all TextCtrl fields storing the key/value pairs in k, v
for k,v in self.fields.items():
# get the dict value for this TextCtrl
f = field_values.get(k, None)
# if the value is empty then set the new value
if f:


#sample output in a .txt file:



Instead of:



"%s" % time.time()



try:



"%.6f" % time.time()



%.6f is a formatting code meaning, floating-point number, 6 decimal places.

thanks a bunch. the "%.6f" was the cure. can you please point me to the doc for formatting time? Thanks!

Would you please read and action this
https://wiki.python.org/moin/GoogleGroupsPython to prevent us seeing the
double line spacing above, thanks.
 
R

Roy Smith

Ned Batchelder said:
A float's str() includes two decimal points of precision

It's actually weirder than that. What str() appears to do is print some
variable number of digits after the decimal place, depending on the
magnitude of the number, and then flips over to exponential notation at
some point. All of this is in line with str()'s intent, which is to
produce a human-friendly string:

for i in range(15):
f = 10**i + 0.123456789
print "%-20s %-20s" % (str(f), repr(f))


$ python float.py
1.123456789 1.123456789
10.123456789 10.123456789
100.123456789 100.123456789
1000.12345679 1000.123456789
10000.1234568 10000.123456789
100000.123457 100000.123456789
1000000.12346 1000000.123456789
10000000.1235 10000000.12345679
100000000.123 100000000.12345679
1000000000.12 1000000000.1234568
10000000000.1 10000000000.123457
1e+11 100000000000.12346
1e+12 1000000000000.1234
1e+13 10000000000000.123
1e+14 100000000000000.12

It just happens that for the range of values time.time() is returning
these days, two decimal digits is what you get. Unix time rolled over
to this many digits on
'Sat Sep 8 21:46:39 2001'

so before then, str(time.time()) would have (presumably) generated 3
decimal digits.

Note that repr() also adjusts the number of digits after the decimal
place, but this is because it's run out of available hardware precision
(IEEE double precision is about 16 decimal digits).

Also note that while repr() is smart enough to stop when it runs out of
bits, the %f format specifier isn't:
'10000000000000.123047'

[Ned, again]
Luckily, you can decide how to format the float yourself:
[...]1388407726.1001

The problem here is that you have no guarantee here that all those
digits are meaningful. I'm not sure what would happen on a machine
where the system clock only gives centisecond precision.

I would like to think "%.4f" % time.time() would always produce a string
with 4 digits after the decimal point, the last two of which were
guaranteed to be "0". But not having such a box handy to test, that's
just a conjecture. A much more pathological case would be that it
produces random garbage for the extra digits, which would make it appear
that you were getting more time precision than you really were.

All of which is a good reason to avoid raw timestamps and use datetime.
With a datatime object, somebody else has already worried about these
things for you.

PS: all the above examples were done with Python 2.7.1 on OSX 10.7.
 
M

Mark Lawrence

You might consider either turning off an option
in your news client for including message in reply
and/or snipping all but a few lines for context
to prevent us from seeing the double line spacing
all over again .... :)

Great idea, but one slight snag is the poster then doesn't see how many
newlines they've managed to insert using their superb tool.
 
S

Steven D'Aprano

Mark said:
On 30/12/2013 17:07, Cousin Stanley wrote:
[...]
You might consider either turning off an option
in your news client for including message in reply
and/or snipping all but a few lines for context
to prevent us from seeing the double line spacing
all over again .... :)

Great idea,

Yes it is. It's a bloody brilliant idea. If only there were some sort of
delete or backspace key on the keyboard that would allow the person
replying to trim or snip excess quoting...

but one slight snag is the poster then doesn't see how many
newlines they've managed to insert using their superb tool.

So what? Chances are that Google Groups will cleverly hide the quoting from
them anyway, which means that you're not demonstrating the problem to them,
you're just spamming the group with double-spaced, excessively quoted,
irrelevant text. Even if they see the quoted text, half a dozen lines is
more than enough to demonstrate the problem to any reasonable person, they
can extrapolate from that. If half a dozen quoted lines isn't enough to
persuade them to read the link, a million lines won't be.
 
G

Grant Edwards

i am using 2.7. I need to print the time in seconds from the epoch
with millisecond precision. i have tried many things but have failed. [...]
In [1]: import time
In [2]: time.time()
Out[2]: 1388085670.1567955


OK i did what you said but I am only getting 2 decimal places.
Why is this and what can I do to get the millisecond?

Please show *exactly* what you did. Also please tell us what operating
system you are using. It may be that your operating system's clock doesn't
provide millisecond precision.

AFAIK, that's irrelevent. time.time() returns a float. On all the
CPython implementations I know of, that is a 64-bit IEEE format, which
provides 16 decimal digits of precision regardless of the granularity
of the system time value. At this point in time, that means 10 digits
left of the decimal point and 6 to the right.
 
D

Dave Angel

AFAIK, that's irrelevent. time.time() returns a float. On all the
CPython implementations I know of, that is a 64-bit IEEE format, which
provides 16 decimal digits of precision regardless of the granularity
of the system time value. At this point in time, that means 10 digits
left of the decimal point and 6 to the right.

Correction: no more than about 6 to the right. You can certainly get
less, from an os with a smaller resolution. Or you can lose some of
what you do get by printing in a sub-optimal way.
 
G

Grant Edwards

Correction: no more than about 6 to the right. You can certainly get
less, from an os with a smaller resolution.

time.time() returns a Python float. A Python float will have 16 digits
of precision. Perhaps the OS always sets some of those digits to 0 (or
even random values), but they're still there. Perhaps the accuracy or
granularity of the values returned is problematic on some OSes, but
the precision of the value doesn't change: there's no way he's "only
getting 2 decimal places" from time.time() unless (as you mention
below) he's printing them using a method that truncates/rounds.
Or you can lose some of what you do get by printing in a sub-optimal
way.

Yes, depending on how you print the value, you can hide some of the
digits. But, there's no way for time.time() to return a value with
less than ~16 decimal digits of precicsion.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,575
Members
47,207
Latest member
HelenaCani

Latest Threads

Top