need to print seconds from the epoch including the millisecond

M

matt.doolittle33

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. heres my latest:

from time import time, strftime
from datetime import datetime, time

# write date, time, then seconds from epoch
self.logfile.write('%s\t'%(strftime("%Y-%m-%d",)))
self.logfile.write('%s\t'%(now.strftime("%H:%M:%S",)))
self.logfile.write('%s\t'%(now.time()))


what am i doing wrong? what should i be doing here? Thanks!
 
D

Dan Stromberg

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. heres my latest:

from time import time, strftime
from datetime import datetime, time

# write date, time, then seconds from epoch
self.logfile.write('%s\t'%(strftime("%Y-%m-%d",)))
self.logfile.write('%s\t'%(now.strftime("%H:%M:%S",)))
self.logfile.write('%s\t'%(now.time()))

In [1]: import time

In [2]: time.time()
Out[2]: 1388085670.1567955

HTH
 
M

matt.doolittle33

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. heres my latest:

from time import time, strftime
from datetime import datetime, time

# write date, time, then seconds from epoch


self.logfile.write('%s\t'%(now.time()))



In [1]: import time



In [2]: time.time()

Out[2]: 1388085670.1567955



HTH

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?
 
S

Steven D'Aprano

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

Terry Reedy

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.

You're probably on Windows, which does time differently.

With 3.3 and 3.4 on Windows 7, time.time() gives 6 fractional digits.
1388105935.971099

Specify your os version and python version and somebody will probably
know.

With 2.7, same machine, I only get 3.
 
R

Roy Smith

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?[/QUOTE]

What operating system are you on? The Python time routines can only
return as much precision as the operating system makes available.
 
D

Dave Angel

With 3.3 and 3.4 on Windows 7, time.time() gives 6 fractional digits.

With 2.7, same machine, I only get 3.

The way I recall it, Windows time is a mess. To get better than 10
ms resolution you needed to use time.clock, but that isn't epoch
time. Trickier solutions existed, depending on exactly what the
problem was. But judging from your test, 3.3 built those gyrations
into the stdlib. I dunno, I pretty much stopped using Windows 4
years ago.
 
M

matt.doolittle33

The way I recall it, Windows time is a mess. To get better than 10

ms resolution you needed to use time.clock, but that isn't epoch

time. Trickier solutions existed, depending on exactly what the

problem was. But judging from your test, 3.3 built those gyrations

into the stdlib. I dunno, I pretty much stopped using Windows 4

years ago.

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!
 
M

matt.doolittle33

(e-mail address removed) wrote:


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?



What operating system are you on? The Python time routines can only

return as much precision as the operating system makes available.

I use Ubuntu 12.10. Thanks!
 
D

Dave Angel

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!

Now I'm stumped. 2.7.3 on Ubuntu 12.04 and time.time gives me 6
decimals. Of course it's a float, so you could get more or fewer. But
if you're only seeing 2, something else is different.
 
R

Roy Smith

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:

cat /etc/lsb-release

uname -a

python --version

cat prog.py

python prog.py
 
M

matt.doolittle33

(e-mail address removed) wrote:








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
self.logfile.write('\n')
# write date, time, and seconds from the epoch
self.logfile.write('%s\t'%(strftime("%Y-%m-%d",)))
self.logfile.write('%s\t'%(now.strftime("%H:%M:%S",)))
self.logfile.write('%s\t'%(time.time()))
# 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...
if f:
# output the value with trailing tab
self.logfile.write('%s\t'%(str(f)))
# if data unit doesnt have this value print a tab
else:
self.logfile.write('\t')
#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:
v.SetValue(f)

#sample output in a .txt file:

2013-12-27 12:07:33 1388164053.18
2013-12-27 12:07:33 1388164053.36
2013-12-27 12:07:33 1388164053.54
2013-12-27 12:07:33 1388164053.73
2013-12-27 12:07:33 1388164053.91
2013-12-27 12:07:34 1388164054.11
2013-12-27 12:07:34 1388164054.28
2013-12-27 12:07:34 1388164054.48
2013-12-27 12:07:34 1388164054.66
2013-12-27 12:07:34 1388164054.84
2013-12-27 12:07:37 1388164057.62
2013-12-27 12:07:37 1388164057.81
2013-12-27 12:07:37 1388164057.99
2013-12-27 12:07:38 1388164058.18
2013-12-27 12:07:38 1388164058.37
2013-12-27 12:07:38 1388164058.54
2013-12-27 12:07:38 1388164058.73
2013-12-27 12:07:38 1388164058.92

Thanks!
 
D

Dennis Lee Bieber

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!

Are you "print"ing the value?
1388167967.23 1388167967.231


Note: this is on Windows, with its small fractional amount. Note that
printing the value rounds to 2 places.
 
N

Ned Batchelder

(e-mail address removed) wrote:








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
self.logfile.write('\n')
# write date, time, and seconds from the epoch
self.logfile.write('%s\t'%(strftime("%Y-%m-%d",)))
self.logfile.write('%s\t'%(now.strftime("%H:%M:%S",)))
self.logfile.write('%s\t'%(time.time()))
# 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...
if f:
# output the value with trailing tab
self.logfile.write('%s\t'%(str(f)))
# if data unit doesnt have this value print a tab
else:
self.logfile.write('\t')
#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:
v.SetValue(f)

#sample output in a .txt file:

2013-12-27 12:07:33 1388164053.18
2013-12-27 12:07:33 1388164053.36
2013-12-27 12:07:33 1388164053.54
2013-12-27 12:07:33 1388164053.73
2013-12-27 12:07:33 1388164053.91
2013-12-27 12:07:34 1388164054.11
2013-12-27 12:07:34 1388164054.28
2013-12-27 12:07:34 1388164054.48
2013-12-27 12:07:34 1388164054.66
2013-12-27 12:07:34 1388164054.84
2013-12-27 12:07:37 1388164057.62
2013-12-27 12:07:37 1388164057.81
2013-12-27 12:07:37 1388164057.99
2013-12-27 12:07:38 1388164058.18
2013-12-27 12:07:38 1388164058.37
2013-12-27 12:07:38 1388164058.54
2013-12-27 12:07:38 1388164058.73
2013-12-27 12:07:38 1388164058.92

Thanks!

Instead of:

"%s" % time.time()

try:

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

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

Roy Smith

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.[/QUOTE]

Why can't you run it that way? What does "got nothing" mean? Did you
just get another shell prompt back with no output? Did your shell
window close? Did the machine crash?

I asked you to run these commands:
cat /etc/lsb-release

uname -a

python --version

Did you run them? What output did you get? I know it seems silly, but
it really is important that people know exactly what your environment
is. The less information we have, the harder it is to figure out what's
going on.
but here is all the code relevant to this issue:

Well, you've got a lot of code there. What you want to do is reduce
this down to the smallest possible amount of code which demonstrates the
problem.

I can't even begin to run your code here because I don't have gnuradio
installed. It's almost certainly not necessary to demonstrate the
problem (nor are posixpath, cPickle, wx, etc), but I can already see
that as soon as I delete those, I'll run up against the next problem,
which is that update() is a method of a class and I don't have the rest
of that class.

Let's take this one step at a time. You've got:

self.logfile.write('%s\t'%(time.time()))

which is apparently causing, "1388164053.18", to end up in your output
file. The question is, why are there only two digits after the decimal
place? Possible causes:

1) Your version of time.time() is returning a float which is only
precise to the centisecond.

2) Your version of string's %s operator is only converting floats to two
decimal places.

3) Your self.logfile.write() method is taking the string it was given
and stripping off all the digits beyond two after the decimal point.

All of those seem about equally unlikely, so just start to eliminate
them one by one. What happens if you do:

self.logfile.write("1388164053.183454")

What happens if you do:

t = time.time()
self.logfile.write("str=%s, repr=%s", (str(t), repr(t)))

what happens if you get rid of the whole self.logfile.write() thing and
just use print? If you're working in some environment where stdout gets
redirected somewhere that you can't find, bypass stdout completely:

my_file = open("/tmp/foo", "w")
print >> my_file, time.time()

and then go look and see what got dropped into /tmp/foo.
 
C

Cameron Simpson

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.
Please show us _exactly_ what you're doing. I'm guessing that print
is confusing you.

Cheers,
--
Cameron Simpson <[email protected]>

Try moving off NT easily. You can move from Solaris to HP/UX to AIX or
DEC easily-- relative to moving off of NT, which is like a Roach
Motel. Once you check in, you never check out.
- Scott McNealy, Sun Microsystems
 
R

Roy Smith

I need to print the time in seconds from the epoch with
millisecond precision.
What happens if you do:

t = time.time()
self.logfile.write("str=%s, repr=%s", (str(t), repr(t)))

At the time I originally posted that, I was baffled as to what was going
on and was simply feeding you suggestions for how to go about debugging
the problem logically.

However, I have since figured out exactly what's going on. I'm going to
do you a favor and NOT tell you what I've figured out (because you won't
learn anything that way), but I will give you a hint. The hint is that
if you run the two lines of code I suggested above, the answer should be
obvious.

And, once you do that, please report your findings back to us, because
it's a fun little quirk of Python and one that I suspect has tripped up
more than a few people over time. In fact, I seem to recall being
mystified by this myself once.
 

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
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top